浏览代码

Merge pull request #38 from dwillmer/new-api

Update for new phosphide APIs
Steven Silvester 9 年之前
父节点
当前提交
2f065930f8
共有 4 个文件被更改,包括 114 次插入52 次删除
  1. 1 0
      examples/lab/index.js
  2. 2 1
      package.json
  3. 67 25
      src/fileopener/plugin.ts
  4. 44 26
      src/terminal/plugin.ts

+ 1 - 0
examples/lab/index.js

@@ -11,6 +11,7 @@ function main() {
     require('phosphide/lib/appshell/plugin'),
     require('phosphide/lib/commandregistry/plugin'),
     require('phosphide/lib/commandpalette/plugin'),
+    require('phosphide/lib/shortcutmanager/plugin'),
     require('../lib/terminal/plugin'),
     require('../lib/fileopener/plugin'),
     require('../lib/filehandler/plugin'),

+ 2 - 1
package.json

@@ -11,8 +11,9 @@
     "jupyter-js-services": "^0.4.1",
     "jupyter-js-terminal": "^0.1.12",
     "jupyter-js-utils": "^0.2.7",
-    "phosphide": "^0.2.0",
+    "phosphide": "^0.3.0",
     "phosphor-codemirror": "^0.0.1",
+    "phosphor-command": "^0.6.0",
     "phosphor-di": "^0.9.0",
     "phosphor-properties": "^2.0.0",
     "phosphor-tabs": "^1.0.0-rc.2"

+ 67 - 25
src/fileopener/plugin.ts

@@ -7,9 +7,13 @@ import {
 } from 'jupyter-js-filebrowser';
 
 import {
-  IAppShell, ICommandPalette, ICommandRegistry
+  IAppShell, ICommandPalette, ICommandRegistry, IShortcutManager
 } from 'phosphide';
 
+import {
+  SimpleCommand
+} from 'phosphor-command';
+
 import {
   Container, Token
 } from 'phosphor-di';
@@ -41,33 +45,71 @@ import {
 export
 function resolve(container: Container): Promise<void> {
   return container.resolve({
-    requires: [IAppShell, IFileOpener, IFileBrowserWidget, ICommandPalette, ICommandRegistry],
-    create: (appShell: IAppShell, opener: IFileOpener, browser: IFileBrowserWidget, palette: ICommandPalette, registry: ICommandRegistry): void => {
-      registry.add('jupyter-plugins:new:text-file', () => {
-        browser.newUntitled('file', '.txt').then(
-          contents => opener.open(contents.path)
-        );
+    requires: [IAppShell, IFileOpener, IFileBrowserWidget, ICommandPalette, ICommandRegistry, IShortcutManager],
+    create: (appShell: IAppShell, opener: IFileOpener, browser: IFileBrowserWidget, palette: ICommandPalette, registry: ICommandRegistry, shortcuts: IShortcutManager): void => {
+
+      // Create a command to add a new empty text file.
+      // This requires an id and an instance of a command object.
+      let newTextFileId = 'file-operations:new-text-file';
+      let newTextFileCommand = new SimpleCommand({
+        handler: () => {
+          browser.newUntitled('file', '.txt').then(
+            contents => opener.open(contents.path)
+          );
+        }
       });
 
-      registry.add('jupyter-plugins:new:notebook', () => {
-        browser.newUntitled('notebook').then(
-          contents => opener.open(contents.path)
-        );
+      // Add the command to the command registry, shortcut manager
+      // and command palette plugins.
+      registry.add([
+        {
+          id: newTextFileId,
+          command: newTextFileCommand
+        }
+      ]);
+      shortcuts.add([
+        {
+          sequence: ['Ctrl O'],
+          selector: '*',
+          command: newTextFileId
+        }
+      ]);
+      palette.add([
+        {
+          id: newTextFileId,
+          args: void 0
+        }
+      ]);
+
+      // Add the command for a new notebook.
+      let newNotebookId = 'file-operations:new-notebook';
+      let newNotebookCommand = new SimpleCommand({
+        handler: () => {
+          browser.newUntitled('notebook').then(
+            contents => opener.open(contents.path)
+          );
+        }
       });
-      let paletteItems = [{
-        id: 'jupyter-plugins:new:text-file',
-        title: 'Text File',
-        caption: ''
-      }, {
-        id: 'jupyter-plugins:new:notebook',
-        title: 'Notebook',
-        caption: ''
-      }];
-      let section = {
-        text: 'New...',
-        items: paletteItems
-      }
-      palette.add([section]);
+
+      registry.add([
+        {
+          id: newNotebookId,
+          command: newNotebookCommand
+        }
+      ]);
+      shortcuts.add([
+        {
+          sequence: ['Ctrl Shift N'],
+          selector: '*',
+          command: newNotebookId
+        }
+      ]);
+      palette.add([
+        {
+          id: newNotebookId,
+          args: void 0
+        }
+      ]);
 
       browser.widgetFactory = path => {
         return opener.open(path);

+ 44 - 26
src/terminal/plugin.ts

@@ -7,9 +7,13 @@ import {
 } from 'jupyter-js-terminal';
 
 import {
-  IAppShell, ICommandPalette, ICommandRegistry
+  IAppShell, ICommandPalette, ICommandRegistry, IShortcutManager
 } from 'phosphide';
 
+import {
+  SimpleCommand
+} from 'phosphor-command';
+
 import {
   Container, Token
 } from 'phosphor-di';
@@ -24,33 +28,47 @@ import './plugin.css';
 export
 function resolve(container: Container): Promise<void> {
   return container.resolve({
-    requires: [IAppShell, ICommandPalette, ICommandRegistry],
-    create: (shell: IAppShell, palette: ICommandPalette, registry: ICommandRegistry) => {
-      registry.add('jupyter-plugins:new:terminal', () => {
-        let term = new TerminalWidget();
-        term.color = 'black';
-        term.background = 'white';
-        term.title.closable = true;
-        shell.addToMainArea(term);
-        let stack = term.parent;
-        if (!stack) {
-          return;
-        }
-        let tabs = stack.parent;
-        if (tabs instanceof TabPanel) {
-          tabs.currentWidget = term;
+    requires: [IAppShell, ICommandPalette, ICommandRegistry, IShortcutManager],
+    create: (shell: IAppShell, palette: ICommandPalette, registry: ICommandRegistry, shortcuts: IShortcutManager) => {
+
+      let newTerminalId = 'terminal:new';
+      let newTerminalCommand = new SimpleCommand({
+        handler: () => {
+          let term = new TerminalWidget();
+          term.color = 'black';
+          term.background = 'white';
+          term.title.closable = true;
+          shell.addToMainArea(term);
+          let stack = term.parent;
+          if (!stack) {
+            return;
+          }
+          let tabs = stack.parent;
+          if (tabs instanceof TabPanel) {
+            tabs.currentWidget = term;
+          }
         }
       });
-      let paletteItem = {
-        id: 'jupyter-plugins:new:terminal',
-        title: 'Terminal',
-        caption: ''
-      };
-      let section = {
-        text: 'New...',
-        items: [paletteItem]
-      }
-      palette.add([section]);
+
+      registry.add([
+        {
+          id: newTerminalId,
+          command: newTerminalCommand
+        }
+      ]);
+      shortcuts.add([
+        {
+          sequence: ['Ctrl T'],
+          selector: '*',
+          command: newTerminalId
+        }
+      ]);
+      palette.add([
+        {
+          id: newTerminalId,
+          args: void 0
+        }
+      ]);
     }
   });
 }