Explorar el Código

Clean up context menu for directories and allow cut-copy-paste

Steven Silvester hace 7 años
padre
commit
53b7c1da92

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

@@ -36,6 +36,7 @@
     "@jupyterlab/docregistry": "^0.13.0",
     "@jupyterlab/filebrowser": "^0.13.2",
     "@jupyterlab/launcher": "^0.13.2",
+    "@jupyterlab/services": "^0.52.0",
     "@phosphor/algorithm": "^1.1.2",
     "@phosphor/commands": "^1.4.0",
     "@phosphor/widgets": "^1.5.0"

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

@@ -29,6 +29,10 @@ import {
   Launcher
 } from '@jupyterlab/launcher';
 
+import {
+  Contents
+} from '@jupyterlab/services';
+
 import {
   each
 } from '@phosphor/algorithm';
@@ -150,9 +154,11 @@ function activateFactory(app: JupyterLab, docManager: IDocumentManager, state: I
     let node = widget.node.getElementsByClassName('jp-DirListing-content')[0];
     node.addEventListener('contextmenu', (event: MouseEvent) => {
       event.preventDefault();
-      const path = widget.pathForClick(event) || '';
-      const menu = createContextMenu(path, commands, registry);
-      menu.open(event.clientX, event.clientY);
+      const model = widget.modelForClick(event);
+      if (model) {
+        const menu = createContextMenu(model, commands, registry);
+        menu.open(event.clientX, event.clientY);
+      }
     });
 
     // Track the newly created file browser.
@@ -380,7 +386,8 @@ function addCommands(app: JupyterLab, tracker: InstanceTracker<FileBrowser>, bro
  * This function generates temporary commands with an incremented name. These
  * commands are disposed when the menu itself is disposed.
  */
-function createContextMenu(path: string, commands: CommandRegistry, registry: DocumentRegistry): Menu {
+function createContextMenu(model: Contents.IModel, commands: CommandRegistry, registry: DocumentRegistry): Menu {
+  const path = model.path;
   const menu = new Menu({ commands });
 
   menu.addItem({ command: CommandIDs.open });
@@ -398,12 +405,15 @@ function createContextMenu(path: string, commands: CommandRegistry, registry: Do
 
   menu.addItem({ command: CommandIDs.rename });
   menu.addItem({ command: CommandIDs.del });
-  menu.addItem({ command: CommandIDs.duplicate });
   menu.addItem({ command: CommandIDs.cut });
   menu.addItem({ command: CommandIDs.copy });
   menu.addItem({ command: CommandIDs.paste });
-  menu.addItem({ command: CommandIDs.download });
-  menu.addItem({ command: CommandIDs.shutdown });
+
+  if (model.type !== 'directory') {
+    menu.addItem({ command: CommandIDs.duplicate });
+    menu.addItem({ command: CommandIDs.download });
+    menu.addItem({ command: CommandIDs.shutdown });
+  }
 
   return menu;
 }

+ 4 - 4
packages/filebrowser/src/browser.ts

@@ -252,14 +252,14 @@ class FileBrowser extends Widget {
   }
 
   /**
-   * Find a path given a click.
+   * Find a model given a click.
    *
    * @param event - The mouse event.
    *
-   * @returns The path to the selected file.
+   * @returns The model for the selected file.
    */
-  pathForClick(event: MouseEvent): string | undefined {
-    return this._listing.pathForClick(event);
+  modelForClick(event: MouseEvent): Contents.IModel | undefined {
+    return this._listing.modelForClick(event);
   }
 
   /**

+ 5 - 7
packages/filebrowser/src/listing.ts

@@ -540,17 +540,17 @@ class DirListing extends Widget {
   }
 
   /**
-   * Find a path given a click.
+   * Find a model given a click.
    *
    * @param event - The mouse event.
    *
-   * @returns The path to the selected file.
+   * @returns The model for the selected file.
    */
-  pathForClick(event: MouseEvent): string | undefined {
+  modelForClick(event: MouseEvent): Contents.IModel | undefined {
     let items = this._sortedItems;
     let index = Private.hitTestNodes(this._items, event.clientX, event.clientY);
     if (index !== -1) {
-      return items[index].path;
+      return items[index];
     }
     return undefined;
   }
@@ -1265,9 +1265,7 @@ class DirListing extends Widget {
   private _copy(): void {
     this._clipboard.length = 0;
     each(this.selectedItems(), item => {
-      if (item.type !== 'directory') {
-        this._clipboard.push(item.path);
-      }
+      this._clipboard.push(item.path);
     });
   }