瀏覽代碼

pull out download + copy download link into separate plugin (#10066)

Diane Hu 4 年之前
父節點
當前提交
8296ffc971
共有 1 個文件被更改,包括 62 次插入40 次删除
  1. 62 40
      packages/filebrowser-extension/src/index.ts

+ 62 - 40
packages/filebrowser-extension/src/index.ts

@@ -299,6 +299,67 @@ const factory: JupyterFrontEndPlugin<IFileBrowserFactory> = {
   }
 };
 
+/**
+ * A plugin providing download + copy download link commands in the context menu
+ */
+const downloadPlugin: JupyterFrontEndPlugin<void> = {
+  id: '@jupyterlab/filebrowser-extension:download',
+  requires: [IFileBrowserFactory, ITranslator],
+  autoStart: true,
+  activate: (
+    app: JupyterFrontEnd,
+    factory: IFileBrowserFactory,
+    translator: ITranslator
+  ): void => {
+    const trans = translator.load('jupyterlab');
+    const { commands } = app;
+    const { tracker } = factory;
+    // matches only non-directory items
+    const selectorNotDir = '.jp-DirListing-item[data-isdir="false"]';
+
+    commands.addCommand(CommandIDs.download, {
+      execute: () => {
+        const widget = tracker.currentWidget;
+
+        if (widget) {
+          return widget.download();
+        }
+      },
+      icon: downloadIcon.bindprops({ stylesheet: 'menuItem' }),
+      label: trans.__('Download')
+    });
+
+    commands.addCommand(CommandIDs.copyDownloadLink, {
+      execute: () => {
+        const widget = tracker.currentWidget;
+        if (!widget) {
+          return;
+        }
+
+        return widget.model.manager.services.contents
+          .getDownloadUrl(widget.selectedItems().next()!.path)
+          .then(url => {
+            Clipboard.copyToSystem(url);
+          });
+      },
+      icon: copyIcon.bindprops({ stylesheet: 'menuItem' }),
+      label: trans.__('Copy Download Link'),
+      mnemonic: 0
+    });
+
+    app.contextMenu.addItem({
+      command: CommandIDs.download,
+      selector: selectorNotDir,
+      rank: 9
+    });
+    app.contextMenu.addItem({
+      command: CommandIDs.copyDownloadLink,
+      selector: selectorNotDir,
+      rank: 13
+    });
+  }
+};
+
 /**
  * A plugin to add the file browser widget to an ILabShell
  */
@@ -576,18 +637,6 @@ function addCommands(
     label: trans.__('Cut')
   });
 
-  commands.addCommand(CommandIDs.download, {
-    execute: () => {
-      const widget = tracker.currentWidget;
-
-      if (widget) {
-        return widget.download();
-      }
-    },
-    icon: downloadIcon.bindprops({ stylesheet: 'menuItem' }),
-    label: trans.__('Download')
-  });
-
   commands.addCommand(CommandIDs.duplicate, {
     execute: () => {
       const widget = tracker.currentWidget;
@@ -754,24 +803,6 @@ function addCommands(
     mnemonic: 0
   });
 
-  commands.addCommand(CommandIDs.copyDownloadLink, {
-    execute: () => {
-      const widget = tracker.currentWidget;
-      if (!widget) {
-        return;
-      }
-
-      return widget.model.manager.services.contents
-        .getDownloadUrl(widget.selectedItems().next()!.path)
-        .then(url => {
-          Clipboard.copyToSystem(url);
-        });
-    },
-    icon: copyIcon.bindprops({ stylesheet: 'menuItem' }),
-    label: trans.__('Copy Download Link'),
-    mnemonic: 0
-  });
-
   commands.addCommand(CommandIDs.paste, {
     execute: () => {
       const widget = tracker.currentWidget;
@@ -1096,11 +1127,6 @@ function addCommands(
     selector: selectorNotDir,
     rank: 8
   });
-  app.contextMenu.addItem({
-    command: CommandIDs.download,
-    selector: selectorNotDir,
-    rank: 9
-  });
   app.contextMenu.addItem({
     command: CommandIDs.shutdown,
     selector: selectorNotDir,
@@ -1117,11 +1143,6 @@ function addCommands(
     selector: selectorItem,
     rank: 12
   });
-  app.contextMenu.addItem({
-    command: CommandIDs.copyDownloadLink,
-    selector: selectorNotDir,
-    rank: 13
-  });
   app.contextMenu.addItem({
     command: CommandIDs.toggleLastModified,
     selector: '.jp-DirListing-header',
@@ -1269,6 +1290,7 @@ const plugins: JupyterFrontEndPlugin<any>[] = [
   browser,
   shareFile,
   fileUploadStatus,
+  downloadPlugin,
   browserWidget,
   launcherToolbarButton
 ];