Browse Source

Pull docmanager download UI into separate plugin so that it can be disabled easily (#10065)

* start pulling out download functions into separate plugin

* Finish pulling download UI into separate plugin

* lint fixes

* Add comment explaining plugin

* move things into optional
Diane Hu 4 years ago
parent
commit
d143131083
1 changed files with 53 additions and 23 deletions
  1. 53 23
      packages/docmanager-extension/src/index.ts

+ 53 - 23
packages/docmanager-extension/src/index.ts

@@ -336,13 +336,65 @@ export const pathStatusPlugin: JupyterFrontEndPlugin<void> = {
   }
 };
 
+/**
+ * A plugin providing download commands in the file menu and command palette.
+ */
+export const downloadPlugin: JupyterFrontEndPlugin<void> = {
+  id: '@jupyterlab/docmanager-extension:download',
+  autoStart: true,
+  requires: [ITranslator, IDocumentManager],
+  optional: [ICommandPalette, IMainMenu],
+  activate: (
+    app: JupyterFrontEnd,
+    translator: ITranslator,
+    docManager: IDocumentManager,
+    palette: ICommandPalette | null,
+    mainMenu: IMainMenu | null
+  ) => {
+    const trans = translator.load('jupyterlab');
+    const { commands, shell } = app;
+    const isEnabled = () => {
+      const { currentWidget } = shell;
+      return !!(currentWidget && docManager.contextForWidget(currentWidget));
+    };
+    commands.addCommand(CommandIDs.download, {
+      label: trans.__('Download'),
+      caption: trans.__('Download the file to your computer'),
+      isEnabled,
+      execute: () => {
+        // Checks that shell.currentWidget is valid:
+        if (isEnabled()) {
+          const context = docManager.contextForWidget(shell.currentWidget!);
+          if (!context) {
+            return showDialog({
+              title: trans.__('Cannot Download'),
+              body: trans.__('No context found for current widget!'),
+              buttons: [Dialog.okButton({ label: trans.__('OK') })]
+            });
+          }
+          return context.download();
+        }
+      }
+    });
+
+    const category = trans.__('File Operations');
+    if (palette) {
+      palette.addItem({ command: CommandIDs.download, category });
+    }
+    if (mainMenu) {
+      mainMenu.fileMenu.addGroup([{ command: CommandIDs.download }], 6);
+    }
+  }
+};
+
 /**
  * Export the plugins as default.
  */
 const plugins: JupyterFrontEndPlugin<any>[] = [
   docManagerPlugin,
   pathStatusPlugin,
-  savingStatusPlugin
+  savingStatusPlugin,
+  downloadPlugin
 ];
 export default plugins;
 
@@ -669,26 +721,6 @@ function addCommands(
     }
   });
 
-  commands.addCommand(CommandIDs.download, {
-    label: trans.__('Download'),
-    caption: trans.__('Download the file to your computer'),
-    isEnabled,
-    execute: () => {
-      // Checks that shell.currentWidget is valid:
-      if (isEnabled()) {
-        const context = docManager.contextForWidget(shell.currentWidget!);
-        if (!context) {
-          return showDialog({
-            title: trans.__('Cannot Download'),
-            body: trans.__('No context found for current widget!'),
-            buttons: [Dialog.okButton({ label: trans.__('OK') })]
-          });
-        }
-        return context.download();
-      }
-    }
-  });
-
   commands.addCommand(CommandIDs.toggleAutosave, {
     label: trans.__('Autosave Documents'),
     isToggled: () => docManager.autosave,
@@ -718,7 +750,6 @@ function addCommands(
       CommandIDs.restoreCheckpoint,
       CommandIDs.save,
       CommandIDs.saveAs,
-      CommandIDs.download,
       CommandIDs.toggleAutosave
     ].forEach(command => {
       palette.addItem({ command, category });
@@ -727,7 +758,6 @@ function addCommands(
 
   if (mainMenu) {
     mainMenu.settingsMenu.addGroup([{ command: CommandIDs.toggleAutosave }], 5);
-    mainMenu.fileMenu.addGroup([{ command: CommandIDs.download }], 6);
   }
 }