Преглед на файлове

Remove "open with..." anonymous command and make file browser context menu universal.

Afshin Darian преди 8 години
родител
ревизия
3e487b9c85
променени са 2 файла, в които са добавени 44 реда и са изтрити 50 реда
  1. 10 9
      packages/docmanager-extension/src/index.ts
  2. 34 41
      packages/filebrowser-extension/src/index.ts

+ 10 - 9
packages/docmanager-extension/src/index.ts

@@ -183,15 +183,6 @@ function addCommands(app: JupyterLab, docManager: IDocumentManager, registry: ID
     }
   });
 
-  commands.addCommand(CommandIDs.open, {
-    execute: args => {
-      let path = args['path'] as string;
-      let factory = args['factory'] as string || void 0;
-      return docManager.services.contents.get(path)
-        .then(() => docManager.openOrReveal(path, factory));
-    }
-  });
-
   commands.addCommand(CommandIDs.newUntitled, {
     execute: args => {
       const errorTitle = args['error'] as string || 'Error';
@@ -210,6 +201,16 @@ function addCommands(app: JupyterLab, docManager: IDocumentManager, registry: ID
     label: args => args['label'] as string || `New ${args['type'] as string}`
   });
 
+  commands.addCommand(CommandIDs.open, {
+    execute: args => {
+      let path = args['path'] as string;
+      let factory = args['factory'] as string || void 0;
+      return docManager.services.contents.get(path)
+        .then(() => docManager.openOrReveal(path, factory));
+    },
+    label: args => (args['label'] || args['factory']) as string
+  });
+
   commands.addCommand(CommandIDs.restoreCheckpoint, {
     label: 'Revert to Checkpoint',
     caption: 'Revert contents to previous checkpoint',

+ 34 - 41
packages/filebrowser-extension/src/index.ts

@@ -14,17 +14,13 @@ import {
 } from '@jupyterlab/docmanager';
 
 import {
-  IDocumentRegistry, DocumentRegistry
+  DocumentRegistry
 } from '@jupyterlab/docregistry';
 
 import {
   FileBrowserModel, FileBrowser, IFileBrowserFactory
 } from '@jupyterlab/filebrowser';
 
-import {
-  map, toArray
-} from '@phosphor/algorithm';
-
 import {
   DisposableSet
 } from '@phosphor/disposable';
@@ -57,7 +53,6 @@ const fileBrowserPlugin: JupyterLabPlugin<void> = {
   requires: [
     IFileBrowserFactory,
     IDocumentManager,
-    IDocumentRegistry,
     IMainMenu,
     ICommandPalette,
     ILayoutRestorer
@@ -92,21 +87,48 @@ export default plugins;
 /**
  * Activate the file browser factory provider.
  */
-function activateFactory(app: JupyterLab, documentManager: IDocumentManager, state: IStateDB): IFileBrowserFactory {
+function activateFactory(app: JupyterLab, docManager: IDocumentManager, state: IStateDB): IFileBrowserFactory {
   const { commands, shell } = app;
   const tracker = new InstanceTracker<FileBrowser>({ namespace, shell });
 
   return {
     createFileBrowser(id: string, options: IFileBrowserFactory.IOptions = {}): FileBrowser {
       const model = new FileBrowserModel({
-        manager: options.documentManager || documentManager,
+        manager: options.documentManager || docManager,
         state: options.state === null ? null : options.state || state
       });
       const widget = new FileBrowser({
         id, model, commands: options.commands || commands
       });
+      const { registry } = docManager;
+
+      // Add a context menu handler to the file browser's directory listing.
+      let node = widget.node.getElementsByClassName('jp-DirListing-content')[0];
+      node.addEventListener('contextmenu', (event: MouseEvent) => {
+        event.preventDefault();
+        let command =  'file-operations:open';
+        let path = widget.pathForClick(event) || '';
+        let ext = DocumentRegistry.extname(path);
+        let factories = registry.preferredWidgetFactories(ext).map(f => f.name);
+        let openWith: Menu = null;
+
+        if (path && factories.length > 1) {
+          openWith = new Menu({ commands });
+          openWith.title.label = 'Open With...';
+
+          for (let factory of factories) {
+            const args = { factory, path };
+            openWith.addItem({ args, command });
+          }
+        }
+
+        const menu = createContextMenu(widget, openWith);
+        menu.open(event.clientX, event.clientY);
+      });
 
+      // Track the newly created file browser.
       tracker.add(widget);
+
       return widget;
     },
     tracker
@@ -117,10 +139,11 @@ function activateFactory(app: JupyterLab, documentManager: IDocumentManager, sta
 /**
  * Activate the file browser in the sidebar.
  */
-function activateFileBrowser(app: JupyterLab, factory: IFileBrowserFactory, documentManager: IDocumentManager, registry: IDocumentRegistry, mainMenu: IMainMenu, palette: ICommandPalette, restorer: ILayoutRestorer): void {
+function activateFileBrowser(app: JupyterLab, factory: IFileBrowserFactory, docManager: IDocumentManager, mainMenu: IMainMenu, palette: ICommandPalette, restorer: ILayoutRestorer): void {
   const { commands } = app;
   const fbWidget = factory.createFileBrowser('filebrowser', {
-    commands, documentManager
+    commands,
+    documentManager: docManager
   });
 
   // Let the application restorer track the primary file browser (that is
@@ -131,36 +154,6 @@ function activateFileBrowser(app: JupyterLab, factory: IFileBrowserFactory, docu
   // responsible for their own restoration behavior, if any.
   restorer.add(fbWidget, namespace);
 
-  // Add a context menu to the dir listing.
-  let node = fbWidget.node.getElementsByClassName('jp-DirListing-content')[0];
-  node.addEventListener('contextmenu', (event: MouseEvent) => {
-    event.preventDefault();
-    let path = fbWidget.pathForClick(event) || '';
-    let ext = DocumentRegistry.extname(path);
-    let factories = registry.preferredWidgetFactories(ext);
-    let widgetNames = toArray(map(factories, factory => factory.name));
-    let prefix = `${namespace}-contextmenu-${++Private.id}`;
-    let openWith: Menu = null;
-    if (path && widgetNames.length > 1) {
-      let command: string;
-
-      openWith = new Menu({ commands });
-      openWith.title.label = 'Open With...';
-
-      for (let widgetName of widgetNames) {
-        command = `${prefix}:${widgetName}`;
-        commands.addCommand(command, {
-          execute: () => fbWidget.openPath(path, widgetName),
-          label: widgetName
-        });
-        openWith.addItem({ command });
-      }
-    }
-
-    let menu = createContextMenu(fbWidget, openWith);
-    menu.open(event.clientX, event.clientY);
-  });
-
   addCommands(app, fbWidget);
 
   fbWidget.title.label = 'Files';
@@ -176,7 +169,7 @@ function activateFileBrowser(app: JupyterLab, factory: IFileBrowserFactory, docu
 
 
 /**
- * Add the filebrowser commands to the application's command registry.
+ * Add the main file browser commands to the application's command registry.
  */
 function addCommands(app: JupyterLab, fbWidget: FileBrowser): void {
   const { commands } = app;