Explorar o código

Reinstate editor commands and menu items

Steven Silvester %!s(int64=8) %!d(string=hai) anos
pai
achega
dd8571757d
Modificáronse 3 ficheiros con 167 adicións e 137 borrados
  1. 2 1
      jupyterlab/src/extensions.ts
  2. 156 3
      src/codemirror/plugin.ts
  3. 9 133
      src/editorwidget/plugin.ts

+ 2 - 1
jupyterlab/src/extensions.ts

@@ -5,7 +5,8 @@ module.exports = [
   require('../../lib/about/plugin').plugin,
   require('../../lib/application/plugin').plugin,
   require('../../lib/clipboard/plugin').plugin,
-  require('../../lib/codemirror/plugin').plugin,
+  require('../../lib/codemirror/plugin').editorServices,
+  require('../../lib/codemirror/plugin').editorCommands,
   require('../../lib/commandlinker/plugin').plugin,
   require('../../lib/commandpalette/plugin').plugin,
   require('../../lib/console/plugin').plugin,

+ 156 - 3
src/codemirror/plugin.ts

@@ -1,5 +1,17 @@
+// Copyright (c) Jupyter Development Team.
+// Distributed under the terms of the Modified BSD License.
+
+import 'codemirror/addon/edit/matchbrackets.js';
+import 'codemirror/addon/edit/closebrackets.js';
+import 'codemirror/addon/comment/comment.js';
+import 'codemirror/keymap/vim.js';
+
+import {
+  Menu
+} from 'phosphor/lib/ui/menu';
+
 import {
-  JupyterLabPlugin
+  JupyterLab, JupyterLabPlugin
 } from '../application';
 
 import {
@@ -7,7 +19,19 @@ import {
 } from '../codeeditor';
 
 import {
-  CodeMirrorEditorFactory, CodeMirrorMimeTypeService
+  ICommandPalette
+} from '../commandpalette';
+
+import {
+  IEditorTracker
+} from '../editorwidget';
+
+import {
+  IMainMenu
+} from '../mainmenu';
+
+import {
+  CodeMirrorEditorFactory, CodeMirrorMimeTypeService, CodeMirrorEditor, DEFAULT_CODEMIRROR_THEME
 } from '.';
 
 
@@ -15,7 +39,7 @@ import {
  * The editor services.
  */
 export
-const plugin: JupyterLabPlugin<IEditorServices> = {
+const editorServices: JupyterLabPlugin<IEditorServices> = {
   id: IEditorServices.name,
   provides: IEditorServices,
   activate: (): IEditorServices => {
@@ -24,3 +48,132 @@ const plugin: JupyterLabPlugin<IEditorServices> = {
     return { factory, mimeTypeService };
   }
 };
+
+
+/**
+ * The editor commands.
+ */
+export
+const editorCommands: JupyterLabPlugin<void> = {
+  id: 'jupyter.services.editor-commands',
+  requires: [IEditorTracker, IMainMenu, ICommandPalette],
+  activate: activateEditorCommands,
+  autoStart: true
+};
+
+
+/**
+ * The map of command ids used by the editor.
+ */
+const cmdIds = {
+  lineNumbers: 'editor:line-numbers',
+  lineWrap: 'editor:line-wrap',
+  matchBrackets: 'editor:match-brackets',
+  vimMode: 'editor:vim-mode',
+  changeTheme: 'editor:change-theme',
+  createConsole: 'editor:create-console',
+  runCode: 'editor:run-code'
+};
+
+
+/**
+ * Set up the editor widget menu and commands.
+ */
+function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMenu: IMainMenu, palette: ICommandPalette): void {
+  let { commands, keymap } = app;
+
+  /**
+   * Toggle editor matching brackets
+   */
+  function toggleMatchBrackets(): void {
+    if (tracker.currentWidget) {
+      let editor = tracker.currentWidget.editor;
+      if (editor instanceof CodeMirrorEditor) {
+        let cm = editor.editor;
+        cm.setOption('matchBrackets', !cm.getOption('matchBrackets'));
+      }
+    }
+  }
+
+  /**
+   * Toggle the editor's vim mode
+   */
+  function toggleVim(): void {
+    tracker.forEach(widget => {
+      if (widget.editor instanceof CodeMirrorEditor) {
+        let cm = widget.editor.editor;
+        let keymap = cm.getOption('keyMap') === 'vim' ? 'default'
+        : 'vim';
+        cm.setOption('keyMap', keymap);
+      }
+    });
+  }
+
+  /**
+   * Create a menu for the editor.
+   */
+  function createMenu(): Menu {
+    let settings = new Menu({ commands, keymap });
+    let theme = new Menu({ commands, keymap });
+    let menu = new Menu({ commands, keymap });
+
+    menu.title.label = 'Editor';
+    settings.title.label = 'Settings';
+    theme.title.label = 'Theme';
+
+    settings.addItem({ command: cmdIds.lineNumbers });
+    settings.addItem({ command: cmdIds.lineWrap });
+    settings.addItem({ command: cmdIds.matchBrackets });
+    settings.addItem({ command: cmdIds.vimMode });
+
+    commands.addCommand(cmdIds.changeTheme, {
+      label: args => args['theme'] as string,
+      execute: args => {
+        let name: string = args['theme'] as string || DEFAULT_CODEMIRROR_THEME;
+        tracker.forEach(widget => {
+          if (widget.editor instanceof CodeMirrorEditor) {
+            let cm = widget.editor.editor;
+            cm.setOption('theme', name);
+          }
+        });
+      }
+    });
+
+    [
+     'jupyter', 'default', 'abcdef', 'base16-dark', 'base16-light',
+     'hopscotch', 'material', 'mbo', 'mdn-like', 'seti', 'the-matrix',
+     'xq-light', 'zenburn'
+    ].forEach(name => theme.addItem({
+      command: 'editor:change-theme',
+      args: { theme: name }
+    }));
+
+    menu.addItem({ type: 'separator' });
+    menu.addItem({ type: 'submenu', menu: settings });
+    menu.addItem({ type: 'submenu', menu: theme });
+
+    return menu;
+  }
+
+  mainMenu.addMenu(createMenu(), { rank: 30 });
+
+  commands.addCommand(cmdIds.matchBrackets, {
+    execute: () => { toggleMatchBrackets(); },
+    label: 'Toggle Match Brackets',
+  });
+
+  commands.addCommand(cmdIds.vimMode, {
+    execute: () => { toggleVim(); },
+    label: 'Toggle Vim Mode'
+  });
+
+  [
+    cmdIds.lineNumbers,
+    cmdIds.lineWrap,
+    cmdIds.matchBrackets,
+    cmdIds.vimMode,
+    cmdIds.createConsole,
+    cmdIds.runCode,
+  ].forEach(command => palette.addItem({ command, category: 'Editor' }));
+
+}

+ 9 - 133
src/editorwidget/plugin.ts

@@ -1,27 +1,14 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import 'codemirror/addon/edit/matchbrackets.js';
-import 'codemirror/addon/edit/closebrackets.js';
-import 'codemirror/addon/comment/comment.js';
-import 'codemirror/keymap/vim.js';
-
 import {
   AttachedProperty
 } from 'phosphor/lib/core/properties';
 
-import {
-  Menu
-} from 'phosphor/lib/ui/menu';
-
 import {
   JupyterLab, JupyterLabPlugin
 } from '../application';
 
-import {
-  ICommandPalette
-} from '../commandpalette';
-
 import {
   InstanceTracker
 } from '../common/instancetracker';
@@ -34,10 +21,6 @@ import {
   ILayoutRestorer
 } from '../layoutrestorer';
 
-import {
-  IMainMenu
-} from '../mainmenu';
-
 import {
   IStateDB
 } from '../statedb';
@@ -65,16 +48,13 @@ const EDITOR_ICON_CLASS = 'jp-ImageTextEditor';
  */
 const FACTORY = 'Editor';
 
+
 /**
  * The map of command ids used by the editor.
  */
 const cmdIds = {
   lineNumbers: 'editor:line-numbers',
   lineWrap: 'editor:line-wrap',
-  matchBrackets: 'editor:match-brackets',
-  vimMode: 'editor:vim-mode',
-  closeAll: 'editor:close-all',
-  changeTheme: 'editor:change-theme',
   createConsole: 'editor:create-console',
   runCode: 'editor:run-code'
 };
@@ -87,8 +67,7 @@ export
 const plugin: JupyterLabPlugin<IEditorTracker> = {
   id: 'jupyter.services.editor-handler',
   requires: [
-    IDocumentRegistry, IMainMenu, ICommandPalette, IStateDB, ILayoutRestorer,
-    IEditorServices
+    IDocumentRegistry, IStateDB, ILayoutRestorer, IEditorServices
   ],
   provides: IEditorTracker,
   activate: activateEditorHandler,
@@ -99,7 +78,7 @@ const plugin: JupyterLabPlugin<IEditorTracker> = {
 /**
  * Sets up the editor widget
  */
-function activateEditorHandler(app: JupyterLab, registry: IDocumentRegistry, mainMenu: IMainMenu, palette: ICommandPalette, state: IStateDB, layout: ILayoutRestorer, editorServices: IEditorServices): IEditorTracker {
+function activateEditorHandler(app: JupyterLab, registry: IDocumentRegistry, state: IStateDB, layout: ILayoutRestorer, editorServices: IEditorServices): IEditorTracker {
   const factory = new EditorWidgetFactory(editorServices, {
     name: FACTORY,
     fileExtensions: ['*'],
@@ -130,21 +109,13 @@ function activateEditorHandler(app: JupyterLab, registry: IDocumentRegistry, mai
   });
   registry.addWidgetFactory(factory);
 
-  /**
-   * An attached property for the session id associated with an editor widget.
-   */
-  const sessionIdProperty = new AttachedProperty<EditorWidget, string>({
-    name: 'sessionId'
-  });
-
   /**
    * Toggle editor line numbers
    */
   function toggleLineNums() {
     if (tracker.currentWidget) {
       let editor = tracker.currentWidget.editor;
-      // TODO: move to Codemirror
-      // editor.setOption('lineNumbers', !editor.getOption('lineNumbers'));
+      editor.lineNumbers = !editor.lineNumbers;
     }
   }
 
@@ -154,86 +125,16 @@ function activateEditorHandler(app: JupyterLab, registry: IDocumentRegistry, mai
   function toggleLineWrap() {
     if (tracker.currentWidget) {
       let editor = tracker.currentWidget.editor;
-      // TODO: move to Codemirror
-      // editor.setOption('lineWrapping', !editor.getOption('lineWrapping'));
-    }
-  }
-
-  /**
-   * Toggle editor matching brackets
-   */
-  function toggleMatchBrackets() {
-    if (tracker.currentWidget) {
-      let editor = tracker.currentWidget.editor;
-      // TODO: move to Codemirror
-      // editor.setOption('matchBrackets', !editor.getOption('matchBrackets'));
+      editor.wordWrap = !editor.wordWrap;
     }
   }
 
   /**
-   * Toggle the editor's vim mode
-   */
-  function toggleVim() {
-    tracker.forEach(widget => {
-      // TODO: move to Codemirror
-      // let keymap = widget.editor.getOption('keyMap') === 'vim' ? 'default'
-      //   : 'vim';
-      // widget.editor.setOption('keyMap', keymap);
-    });
-  }
-
-  /**
-   * Close all currently open text editor files
-   */
-  function closeAllFiles() {
-    tracker.forEach(widget => { widget.close(); });
-  }
-
-  /**
-   * Create a menu for the editor.
+   * An attached property for the session id associated with an editor widget.
    */
-  function createMenu(app: JupyterLab): Menu {
-    let { commands, keymap } = app;
-    let settings = new Menu({ commands, keymap });
-    let theme = new Menu({ commands, keymap });
-    let menu = new Menu({ commands, keymap });
-
-    menu.title.label = 'Editor';
-    settings.title.label = 'Settings';
-    theme.title.label = 'Theme';
-
-    settings.addItem({ command: cmdIds.lineNumbers });
-    settings.addItem({ command: cmdIds.lineWrap });
-    settings.addItem({ command: cmdIds.matchBrackets });
-    settings.addItem({ command: cmdIds.vimMode });
-
-    // TODO: move to codemirror
-    // commands.addCommand(cmdIds.changeTheme, {
-    //   label: args => args['theme'] as string,
-    //   execute: args => {
-    //     let name: string = args['theme'] as string || DEFAULT_CODEMIRROR_THEME;
-    //     tracker.forEach(widget => { widget.editor.setOption('theme', name); });
-    //   }
-    // });
-
-    [
-     'jupyter', 'default', 'abcdef', 'base16-dark', 'base16-light',
-     'hopscotch', 'material', 'mbo', 'mdn-like', 'seti', 'the-matrix',
-     'xq-light', 'zenburn'
-    ].forEach(name => theme.addItem({
-      command: 'editor:change-theme',
-      args: { theme: name }
-    }));
-
-    menu.addItem({ command: cmdIds.closeAll });
-    menu.addItem({ type: 'separator' });
-    menu.addItem({ type: 'submenu', menu: settings });
-    menu.addItem({ type: 'submenu', menu: theme });
-
-    return menu;
-  }
-
-  mainMenu.addMenu(createMenu(app), { rank: 30 });
+  const sessionIdProperty = new AttachedProperty<EditorWidget, string>({
+    name: 'sessionId'
+  });
 
   let commands = app.commands;
 
@@ -247,21 +148,6 @@ function activateEditorHandler(app: JupyterLab, registry: IDocumentRegistry, mai
     label: 'Toggle Line Wrap',
   });
 
-  commands.addCommand(cmdIds.matchBrackets, {
-    execute: () => { toggleMatchBrackets(); },
-    label: 'Toggle Match Brackets',
-  });
-
-  commands.addCommand(cmdIds.vimMode, {
-    execute: () => { toggleVim(); },
-    label: 'Toggle Vim Mode'
-  });
-
-  commands.addCommand(cmdIds.closeAll, {
-    execute: () => { closeAllFiles(); },
-    label: 'Close all files'
-  });
-
   commands.addCommand(cmdIds.createConsole, {
     execute: () => {
       let widget = tracker.currentWidget;
@@ -301,15 +187,5 @@ function activateEditorHandler(app: JupyterLab, registry: IDocumentRegistry, mai
     label: 'Run Code',
   });
 
-  [
-    cmdIds.lineNumbers,
-    cmdIds.lineWrap,
-    cmdIds.matchBrackets,
-    cmdIds.vimMode,
-    cmdIds.closeAll,
-    cmdIds.createConsole,
-    cmdIds.runCode,
-  ].forEach(command => palette.addItem({ command, category: 'Editor' }));
-
   return tracker;
 }