Pārlūkot izejas kodu

Clean up context menu creation.

Afshin Darian 8 gadi atpakaļ
vecāks
revīzija
a36e184c56

+ 1 - 0
packages/filebrowser-extension/package.json

@@ -20,6 +20,7 @@
     "@jupyterlab/filebrowser": "^0.3.1",
     "@jupyterlab/services": "^0.42.1",
     "@phosphor/algorithm": "^1.0.0",
+    "@phosphor/commands": "^1.0.0",
     "@phosphor/disposable": "^1.0.0",
     "@phosphor/widgets": "^1.0.0"
   },

+ 17 - 19
packages/filebrowser-extension/src/index.ts

@@ -18,13 +18,17 @@ import {
 } from '@jupyterlab/docmanager';
 
 import {
-  DocumentRegistry
+  DocumentRegistry, IDocumentRegistry
 } from '@jupyterlab/docregistry';
 
 import {
   FileBrowserModel, FileBrowser, IFileBrowserFactory
 } from '@jupyterlab/filebrowser';
 
+import {
+  CommandRegistry
+} from '@phosphor/commands';
+
 import {
   Menu
 } from '@phosphor/widgets';
@@ -133,21 +137,8 @@ function activateFactory(app: JupyterLab, docManager: IDocumentManager, state: I
       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...';
-          factories.forEach(factory => {
-            openWith.addItem({ args: { factory, path }, command });
-          });
-        }
-
-        const menu = createContextMenu(widget, openWith);
+        const path = widget.pathForClick(event) || '';
+        const menu = createContextMenu(path, commands, registry);
         menu.open(event.clientX, event.clientY);
       });
 
@@ -356,13 +347,20 @@ function addCommands(app: JupyterLab, tracker: InstanceTracker<FileBrowser>, mai
  * This function generates temporary commands with an incremented name. These
  * commands are disposed when the menu itself is disposed.
  */
-function createContextMenu(fbWidget: FileBrowser, openWith: Menu):  Menu {
-  const { commands } = fbWidget;
+function createContextMenu(path: string, commands: CommandRegistry, registry: IDocumentRegistry):  Menu {
   const menu = new Menu({ commands });
 
   menu.addItem({ command: CommandIDs.open });
 
-  if (openWith) {
+  const ext = DocumentRegistry.extname(path);
+  const factories = registry.preferredWidgetFactories(ext).map(f => f.name);
+  if (path && factories.length > 1) {
+    const command =  'file-operations:open';
+    const openWith = new Menu({ commands });
+    openWith.title.label = 'Open With...';
+    factories.forEach(factory => {
+      openWith.addItem({ args: { factory, path }, command });
+    });
     menu.addItem({ type: 'submenu', submenu: openWith });
   }