浏览代码

Move all command handling to extensions

Steven Silvester 8 年之前
父节点
当前提交
a948cabcfe

+ 62 - 1
packages/imageviewer-extension/src/index.ts

@@ -17,6 +17,10 @@ import {
   ImageViewer, ImageViewerFactory, IImageTracker
 } from '@jupyterlab/imageviewer';
 
+import {
+  CommandRegistry
+} from '@phosphor/commands';
+
 
 /**
  * The command IDs used by the image widget plugin.
@@ -73,7 +77,7 @@ function activate(app: JupyterLab, registry: IDocumentRegistry, palette: IComman
     fileExtensions: EXTENSIONS,
     defaultFor: EXTENSIONS
   });
-  const { shell } = app;
+  const { shell, commands } = app;
   const tracker = new InstanceTracker<ImageViewer>({ namespace, shell });
 
   // Handle state restoration.
@@ -91,9 +95,66 @@ function activate(app: JupyterLab, registry: IDocumentRegistry, palette: IComman
     tracker.add(widget);
   });
 
+  addCommands(tracker, commands);
+
   let category = 'Image Widget';
   [CommandIDs.zoomIn, CommandIDs.zoomOut, CommandIDs.resetZoom]
     .forEach(command => { palette.addItem({ command, category }); });
 
   return tracker;
 }
+
+
+/**
+ * Add the commands for the image widget.
+ */
+export
+function addCommands(tracker: IImageTracker, commands: CommandRegistry) {
+  commands.addCommand('imageviewer:zoom-in', {
+    execute: zoomIn,
+    label: 'Zoom In'
+  });
+
+  commands.addCommand('imageviewer:zoom-out', {
+    execute: zoomOut,
+    label: 'Zoom Out'
+  });
+
+  commands.addCommand('imageviewer:reset-zoom', {
+    execute: resetZoom,
+    label: 'Reset Zoom'
+  });
+
+  function zoomIn(): void {
+    let widget = tracker.currentWidget;
+    if (!widget) {
+      return;
+    }
+    if (widget.scale > 1) {
+      widget.scale += .5;
+    } else {
+      widget.scale *= 2;
+    }
+  }
+
+  function zoomOut(): void {
+    let widget = tracker.currentWidget;
+    if (!widget) {
+      return;
+    }
+    if (widget.scale > 1) {
+      widget.scale -= .5;
+    } else {
+      widget.scale /= 2;
+    }
+  }
+
+  function resetZoom(): void {
+    let widget = tracker.currentWidget;
+    if (!widget) {
+      return;
+    }
+    widget.scale = 1;
+  }
+}
+

+ 0 - 58
packages/imageviewer/src/index.ts

@@ -1,10 +1,6 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import {
-  CommandRegistry
-} from '@phosphor/commands';
-
 import {
   Token
 } from '@phosphor/coreutils';
@@ -34,57 +30,3 @@ interface IImageTracker extends IInstanceTracker<ImageViewer> {}
 export
 const IImageTracker = new Token<IImageTracker>('jupyter.services.image-tracker');
 /* tslint:enable */
-
-
-/**
- * Add the default commands for the image widget.
- */
-export
-function addDefaultCommands(tracker: IImageTracker, commands: CommandRegistry) {
-  commands.addCommand('imageviewer:zoom-in', {
-    execute: zoomIn,
-    label: 'Zoom In'
-  });
-
-  commands.addCommand('imageviewer:zoom-out', {
-    execute: zoomOut,
-    label: 'Zoom Out'
-  });
-
-  commands.addCommand('imageviewer:reset-zoom', {
-    execute: resetZoom,
-    label: 'Reset Zoom'
-  });
-
-  function zoomIn(): void {
-    let widget = tracker.currentWidget;
-    if (!widget) {
-      return;
-    }
-    if (widget.scale > 1) {
-      widget.scale += .5;
-    } else {
-      widget.scale *= 2;
-    }
-  }
-
-  function zoomOut(): void {
-    let widget = tracker.currentWidget;
-    if (!widget) {
-      return;
-    }
-    if (widget.scale > 1) {
-      widget.scale -= .5;
-    } else {
-      widget.scale /= 2;
-    }
-  }
-
-  function resetZoom(): void {
-    let widget = tracker.currentWidget;
-    if (!widget) {
-      return;
-    }
-    widget.scale = 1;
-  }
-}

+ 93 - 33
packages/terminal-extension/src/index.ts

@@ -1,14 +1,6 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import {
-  IServiceManager
-} from '@jupyterlab/services';
-
-import {
-  Menu
-} from '@phosphor/widgets';
-
 import {
   JupyterLab, JupyterLabPlugin
 } from '@jupyterlab/application';
@@ -22,9 +14,16 @@ import {
 } from '@jupyterlab/launcher';
 
 import {
-  Terminal, ITerminalTracker, addDefaultCommands
+  IServiceManager
+} from '@jupyterlab/services';
+
+import {
+  Terminal, ITerminalTracker
 } from '@jupyterlab/terminal';
 
+import {
+  Menu
+} from '@phosphor/widgets';
 
 
 /**
@@ -101,7 +100,50 @@ function activate(app: JupyterLab, services: IServiceManager, mainMenu: IMainMen
     name: widget => widget.session && widget.session.name
   });
 
-  addDefaultCommands(tracker, commands);
+  addCommands(app, services, tracker);
+
+  // Add command palette and menu items.
+  let menu = new Menu({ commands });
+  menu.title.label = category;
+  [
+    CommandIDs.createNew,
+    CommandIDs.refresh,
+    CommandIDs.increaseFont,
+    CommandIDs.decreaseFont,
+    CommandIDs.toggleTheme
+  ].forEach(command => {
+    palette.addItem({ command, category });
+    menu.addItem({ command });
+  });
+  mainMenu.addMenu(menu, {rank: 40});
+
+  // Add a launcher item if the launcher is available.
+  if (launcher) {
+    launcher.add({
+      name: 'Terminal',
+      command: CommandIDs.createNew
+    });
+  }
+
+  app.contextMenu.addItem({command: CommandIDs.refresh, selector: '.jp-Terminal', rank: 1});
+
+  return tracker;
+}
+
+
+/**
+ * Add the commands for the terminal.
+ */
+export
+function addCommands(app: JupyterLab, services: IServiceManager, tracker: InstanceTracker<Terminal>) {
+  let { commands, shell } = app;
+
+  /**
+   * Whether there is an active terminal.
+   */
+  function hasWidget(): boolean {
+    return tracker.currentWidget !== null;
+  }
 
   // Add terminal commands.
   commands.addCommand(CommandIDs.createNew, {
@@ -157,30 +199,48 @@ function activate(app: JupyterLab, services: IServiceManager, mainMenu: IMainMen
     isEnabled: () => { return tracker.currentWidget !== null; }
   });
 
-  // Add command palette and menu items.
-  let menu = new Menu({ commands });
-  menu.title.label = category;
-  [
-    CommandIDs.createNew,
-    CommandIDs.refresh,
-    CommandIDs.increaseFont,
-    CommandIDs.decreaseFont,
-    CommandIDs.toggleTheme
-  ].forEach(command => {
-    palette.addItem({ command, category });
-    menu.addItem({ command });
+  commands.addCommand('terminal:increase-font', {
+    label: 'Increase Terminal Font Size',
+    execute: () => {
+      let options = Terminal.defaultOptions;
+      if (options.fontSize < 72) {
+        options.fontSize++;
+        tracker.forEach(widget => { widget.fontSize = options.fontSize; });
+      }
+    },
+    isEnabled: hasWidget
   });
-  mainMenu.addMenu(menu, {rank: 40});
 
-  // Add a launcher item if the launcher is available.
-  if (launcher) {
-    launcher.add({
-      name: 'Terminal',
-      command: CommandIDs.createNew
-    });
-  }
-
-  app.contextMenu.addItem({command: CommandIDs.refresh, selector: '.jp-Terminal', rank: 1});
+  commands.addCommand('terminal:decrease-font', {
+    label: 'Decrease Terminal Font Size',
+    execute: () => {
+      let options = Terminal.defaultOptions;
+      if (options.fontSize > 9) {
+        options.fontSize--;
+        tracker.forEach(widget => { widget.fontSize = options.fontSize; });
+      }
+    },
+    isEnabled: hasWidget
+  });
 
-  return tracker;
+  commands.addCommand('terminal:toggle-theme', {
+    label: 'Toggle Terminal Theme',
+    caption: 'Switch Terminal Background and Font Colors',
+    execute: () => {
+      let options = Terminal.defaultOptions;
+      if (options.background === 'black') {
+        options.background = 'white';
+        options.color = 'black';
+      } else {
+        options.background = 'black';
+        options.color = 'white';
+      }
+      tracker.forEach(widget => {
+        widget.background = options.background;
+        widget.color = options.color;
+      });
+    },
+    isEnabled: hasWidget
+  });
 }
+

+ 0 - 63
packages/terminal/src/index.ts

@@ -1,10 +1,6 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import {
-  CommandRegistry
-} from '@phosphor/commands';
-
 import {
   Token
 } from '@phosphor/coreutils';
@@ -33,62 +29,3 @@ interface ITerminalTracker extends IInstanceTracker<Terminal> {}
 export
 const ITerminalTracker = new Token<ITerminalTracker>('jupyter.services.terminal-tracker');
 /* tslint:enable */
-
-
-/**
- * Add the default commands for the editor.
- */
-export
-function addDefaultCommands(tracker: ITerminalTracker, commands: CommandRegistry) {
-
-  /**
-   * Whether there is an active terminal.
-   */
-  function hasWidget(): boolean {
-    return tracker.currentWidget !== null;
-  }
-
-  commands.addCommand('terminal:increase-font', {
-    label: 'Increase Terminal Font Size',
-    execute: () => {
-      let options = Terminal.defaultOptions;
-      if (options.fontSize < 72) {
-        options.fontSize++;
-        tracker.forEach(widget => { widget.fontSize = options.fontSize; });
-      }
-    },
-    isEnabled: hasWidget
-  });
-
-  commands.addCommand('terminal:decrease-font', {
-    label: 'Decrease Terminal Font Size',
-    execute: () => {
-      let options = Terminal.defaultOptions;
-      if (options.fontSize > 9) {
-        options.fontSize--;
-        tracker.forEach(widget => { widget.fontSize = options.fontSize; });
-      }
-    },
-    isEnabled: hasWidget
-  });
-
-  commands.addCommand('terminal:toggle-theme', {
-    label: 'Toggle Terminal Theme',
-    caption: 'Switch Terminal Background and Font Colors',
-    execute: () => {
-      let options = Terminal.defaultOptions;
-      if (options.background === 'black') {
-        options.background = 'white';
-        options.color = 'black';
-      } else {
-        options.background = 'black';
-        options.color = 'white';
-      }
-      tracker.forEach(widget => {
-        widget.background = options.background;
-        widget.color = options.color;
-      });
-    },
-    isEnabled: hasWidget
-  });
-}