Browse Source

Finish implementation of open with

Steven Silvester 8 years ago
parent
commit
692480e81a

+ 1 - 0
src/dialog/theme.css

@@ -16,6 +16,7 @@
   width: 300px;
   box-sizing: border-box;
   box-shadow: 0px 2px 2px 0px rgba(0,0,0,0.5);
+  word-wrap: break-word;
 }
 
 

+ 16 - 0
src/docmanager/context.ts

@@ -20,6 +20,10 @@ import {
   Widget
 } from 'phosphor-widget';
 
+import {
+  showDialog, okButton
+} from '../dialog';
+
 import {
   IDocumentContext, IDocumentModel, IModelFactory
 } from '../docregistry';
@@ -435,6 +439,12 @@ class ContextManager implements IDisposable {
     return this._contentsManager.save(path, contents).then(newContents => {
       contextEx.contentsModel = this._copyContentsModel(newContents);
       model.dirty = false;
+    }).catch(err => {
+      showDialog({
+        title: 'File Save Error',
+        body: err.xhr.responseText,
+        buttons: [okButton]
+      });
     });
   }
 
@@ -482,6 +492,12 @@ class ContextManager implements IDisposable {
       contextEx.contentsModel = contentsModel;
       contextEx.context.contentsModelChanged.emit(contentsModel);
       model.dirty = false;
+    }).catch(err => {
+      showDialog({
+        title: 'File Load Error',
+        body: err.xhr.responseText,
+        buttons: [okButton]
+      });
     });
   }
 

+ 2 - 2
src/filebrowser/browser.ts

@@ -182,11 +182,11 @@ class FileBrowserWidget extends Widget {
   /**
    * Open a file by path.
    */
-  openPath(path: string): Widget {
+  openPath(path: string, widgetName='default'): Widget {
     let model = this.model;
     let widget = this._manager.findWidget(path);
     if (!widget) {
-      widget = this._manager.open(path);
+      widget = this._manager.open(path, widgetName);
       let context = this._manager.contextForWidget(widget);
       context.populated.connect(() => model.refresh() );
       context.kernelChanged.connect(() => model.refresh() );

+ 3 - 0
src/filebrowser/listing.ts

@@ -496,6 +496,9 @@ class DirListing extends Widget {
    * Get whether an item is selected by name.
    */
   isSelected(name: string): boolean {
+    if (this._softSelection) {
+      return name === this._softSelection;
+    }
     return this._selection[name] === true;
   }
 

+ 27 - 6
src/filebrowser/plugin.ts

@@ -115,8 +115,20 @@ function activateFileBrowser(app: Application, provider: ServiceManager, registr
     let x = event.clientX;
     let y = event.clientY;
     let path = fbWidget.pathForClick(event);
-    let ext = path.split('.').pop();
-    let widgets = registry.listWidgetFactories(ext);
+    let ext = '.' + path.split('.').pop();
+    let widgetNames = registry.listWidgetFactories(ext);
+    let items: MenuItem[] = [];
+    if (widgetNames.length > 1) {
+      for (let widgetName of widgetNames) {
+        items.push(new MenuItem({
+          text: widgetName,
+          handler: () => {
+            fbWidget.openPath(path, widgetName);
+          }
+        }));
+      }
+    }
+    let menu = createMenu(fbWidget, items);
     menu.popup(x, y);
   });
 
@@ -295,14 +307,22 @@ function activateFileBrowser(app: Application, provider: ServiceManager, registr
 /**
  * Create a context menu for the file browser listing.
  */
-function createMenu(fbWidget: FileBrowserWidget):  Menu {
-  return new Menu([
+function createMenu(fbWidget: FileBrowserWidget, openWith: MenuItem[]):  Menu {
+  let items = [
     new MenuItem({
       text: '&Open',
       icon: 'fa fa-folder-open-o',
       shortcut: 'Ctrl+O',
       handler: () => { fbWidget.open(); }
-    }),
+    })
+  ];
+  if (openWith.length) {
+    items.push(new MenuItem({
+      text: 'Open With...',
+      submenu: new Menu(openWith)
+    }));
+  }
+  items.push(
     new MenuItem({
       text: '&Rename',
       icon: 'fa fa-edit',
@@ -348,5 +368,6 @@ function createMenu(fbWidget: FileBrowserWidget):  Menu {
       icon: 'fa fa-stop-circle-o',
       handler: () => { fbWidget.shutdownKernels(); }
     })
-  ]);
+  );
+  return new Menu(items);
 }