浏览代码

Move printing to apputils

Saul Shanabrook 6 年之前
父节点
当前提交
959268f2b1

+ 11 - 0
packages/apputils-extension/schema/print.json

@@ -0,0 +1,11 @@
+{
+  "title": "Print",
+  "description": "Print settings.",
+  "jupyter.lab.shortcuts": [
+    {
+      "command": "apputils:print",
+      "keys": ["Accel P"],
+      "selector": "body"
+    }
+  ]
+}

+ 55 - 4
packages/apputils-extension/src/index.ts

@@ -7,7 +7,8 @@ import {
   ILayoutRestorer,
   IRouter,
   JupyterFrontEnd,
-  JupyterFrontEndPlugin
+  JupyterFrontEndPlugin,
+  ILabShell
 } from '@jupyterlab/application';
 
 import {
@@ -17,7 +18,8 @@ import {
   IThemeManager,
   IWindowResolver,
   ThemeManager,
-  WindowResolver
+  WindowResolver,
+  Printing
 } from '@jupyterlab/apputils';
 
 import {
@@ -36,11 +38,12 @@ import { PromiseDelegate } from '@phosphor/coreutils';
 
 import { DisposableDelegate, IDisposable } from '@phosphor/disposable';
 
-import { Menu } from '@phosphor/widgets';
+import { Menu, Widget } from '@phosphor/widgets';
 
 import { Palette } from './palette';
 
 import '../style/index.css';
+import { toArray } from '@phosphor/algorithm';
 
 /**
  * The interval in milliseconds that calls to save a workspace are debounced
@@ -69,6 +72,8 @@ namespace CommandIDs {
   export const resetOnLoad = 'apputils:reset-on-load';
 
   export const saveState = 'apputils:save-statedb';
+
+  export const print = 'apputils:print';
 }
 
 /**
@@ -298,6 +303,51 @@ const splash: JupyterFrontEndPlugin<ISplashScreen> = {
   }
 };
 
+const print: JupyterFrontEndPlugin<void> = {
+  id: '@jupyterlab/apputils-extension:print',
+  autoStart: true,
+  requires: [ILabShell],
+  activate: (app: JupyterFrontEnd, labShell: ILabShell) => {
+    // TODO: This was copied out of application-extension/src/index.tsx
+    // This should be refactored to not duplicate the code.
+    // Returns the widget associated with the most recent contextmenu event.
+    const contextMenuWidget = (): Widget => {
+      const test = (node: HTMLElement) => !!node.dataset.id;
+      const node = app.contextMenuHitTest(test);
+
+      if (!node) {
+        // Fall back to active widget if path cannot be obtained from event.
+        return app.shell.currentWidget;
+      }
+
+      const matches = toArray(app.shell.widgets('main')).filter(
+        widget => widget.id === node.dataset.id
+      );
+
+      if (matches.length < 1) {
+        return app.shell.currentWidget;
+      }
+
+      return matches[0];
+    };
+
+    app.commands.addCommand(CommandIDs.print, {
+      label: 'Print...',
+      isEnabled: () => {
+        const { currentWidget } = labShell;
+        return Printing.getPrintFunction(currentWidget) !== null;
+      },
+      execute: async () => {
+        const widget = contextMenuWidget();
+        const printFunction = Printing.getPrintFunction(widget);
+        if (printFunction) {
+          await printFunction();
+        }
+      }
+    });
+  }
+};
+
 /**
  * The default state database for storing application state.
  */
@@ -567,7 +617,8 @@ const plugins: JupyterFrontEndPlugin<any>[] = [
   state,
   splash,
   themes,
-  themesPaletteMenu
+  themesPaletteMenu,
+  print
 ];
 export default plugins;
 

+ 2 - 2
packages/apputils/src/printing.ts

@@ -134,10 +134,10 @@ export namespace Printing {
    * Prints a content window.
    */
   function launchPrint(contentWindow: Window) {
-    // execCommand works on all but firefox
-    //  https://github.com/joseluisq/printd/blob/eb7948d602583c055ab6dee3ee294b6a421da4b6/src/index.ts#L148
     const result = contentWindow.document.execCommand('print', false, null);
 
+    // exeCommand won't work in firefox so we call the `print` method instead if it fails
+    // https://github.com/joseluisq/printd/blob/eb7948d602583c055ab6dee3ee294b6a421da4b6/src/index.ts#L148
     if (!result) {
       contentWindow.print();
     }

+ 0 - 5
packages/docmanager-extension/schema/plugin.json

@@ -13,11 +13,6 @@
       "command": "docmanager:save-as",
       "keys": ["Accel Shift S"],
       "selector": "body"
-    },
-    {
-      "command": "docmanager:print",
-      "keys": ["Accel P"],
-      "selector": "body"
     }
   ],
   "properties": {

+ 1 - 19
packages/docmanager-extension/src/index.ts

@@ -16,8 +16,7 @@ import {
   showDialog,
   showErrorMessage,
   Dialog,
-  ICommandPalette,
-  Printing
+  ICommandPalette
 } from '@jupyterlab/apputils';
 
 import { IChangedArgs, ISettingRegistry, Time } from '@jupyterlab/coreutils';
@@ -72,8 +71,6 @@ namespace CommandIDs {
   export const toggleAutosave = 'docmanager:toggle-autosave';
 
   export const showInFileBrowser = 'docmanager:show-in-file-browser';
-
-  export const print = 'docmanager:print';
 }
 
 const pluginId = '@jupyterlab/docmanager-extension:plugin';
@@ -643,21 +640,6 @@ function addLabCommands(
     }
   });
 
-  commands.addCommand(CommandIDs.print, {
-    label: 'Print...',
-    isEnabled: () => {
-      const { currentWidget } = labShell;
-      return Printing.getPrintFunction(currentWidget) !== null;
-    },
-    execute: async () => {
-      const widget = contextMenuWidget();
-      const printFunction = Printing.getPrintFunction(widget);
-      if (printFunction) {
-        await printFunction();
-      }
-    }
-  });
-
   app.contextMenu.addItem({
     command: CommandIDs.rename,
     selector: '[data-type="document-title"]',

+ 3 - 2
packages/mainmenu-extension/src/index.ts

@@ -390,10 +390,10 @@ export function createFileMenu(
             .catch(data => {
               throw new ServerConnection.NetworkError(data);
             });
-        }
-            });
         }
       });
+    }
+  });
 
   commands.addCommand(CommandIDs.logout, {
     label: 'Log Out',
@@ -448,6 +448,7 @@ export function createFileMenu(
     { command: 'filemenu:logout' },
     { command: 'filemenu:shutdown' }
   ];
+  const printGroup = [{ command: 'apputils:print' }];
 
   menu.addGroup(newGroup, 0);
   menu.addGroup(newViewGroup, 1);