浏览代码

[codemirror/factory] Applying extra options

akosyakov 8 年之前
父节点
当前提交
6dadf8b7d4
共有 4 个文件被更改,包括 45 次插入19 次删除
  1. 2 2
      src/codeeditor/editor.ts
  2. 33 11
      src/codemirror/factory.ts
  3. 6 4
      src/console/codemirror/plugin.ts
  4. 4 2
      src/notebook/codemirror/plugin.ts

+ 2 - 2
src/codeeditor/editor.ts

@@ -485,9 +485,9 @@ namespace CodeEditor {
     readOnly?: boolean;
 
     /**
-     * Other options.
+     * Extra options.
      */
-    [key: string]: any;
+    extra?: { [key: string]: any };
   }
 
   /**

+ 33 - 11
src/codemirror/factory.ts

@@ -1,6 +1,10 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import {
+  utils
+} from '@jupyterlab/services';
+
 import {
   CodeEditor, IEditorFactory
 } from '../codeeditor';
@@ -9,17 +13,19 @@ import {
   CodeMirrorEditor, DEFAULT_CODEMIRROR_THEME
 } from './editor';
 
+/**
+ * CodeMirror editor factory.
+ */
 export
 class CodeMirrorEditorFactory implements IEditorFactory {
 
   /**
    * Create a new editor for inline code.
    */
-  newInlineEditor(host: HTMLElement, option: CodeEditor.IOptions): CodeEditor.IEditor {
-    // FIXME: merge given options
-    return new CodeMirrorEditor(host, {
+  newInlineEditor(host: HTMLElement, options: CodeEditor.IOptions): CodeEditor.IEditor {
+    return this.newEditor(host, {
+      uuid: utils.uuid(),
       indentUnit: 4,
-      readOnly: false,
       extraKeys: {
         'Cmd-Right': 'goLineRight',
         'End': 'goLineRight',
@@ -30,17 +36,16 @@ class CodeMirrorEditorFactory implements IEditorFactory {
         'Ctrl-Alt-[': 'indentAuto',
         'Cmd-/': 'toggleComment',
         'Ctrl-/': 'toggleComment',
-      },
-      lineNumbers: true,
-      lineWrapping: true,
-    });
+      }
+    }, options);
   }
 
   /**
    * Create a new editor for a full document.
    */
   newDocumentEditor(host: HTMLElement, options: CodeEditor.IOptions): CodeEditor.IEditor {
-    return new CodeMirrorEditor(host, {
+    return this.newEditor(host, {
+      uuid: utils.uuid(),
       extraKeys: {
         'Tab': 'indentMore',
         'Shift-Enter': () => { /* no-op */ }
@@ -48,8 +53,25 @@ class CodeMirrorEditorFactory implements IEditorFactory {
       indentUnit: 4,
       theme: DEFAULT_CODEMIRROR_THEME,
       lineNumbers: true,
-      lineWrapping: true,
-    });
+      lineWrapping: true
+    }, options);
+  }
+
+  /**
+   * Creates an editor and applies extra options.
+   */
+  protected newEditor(host: HTMLElement, editorOptions: CodeMirrorEditor.IOptions, options: CodeEditor.IOptions) {
+    editorOptions.readOnly = (options.readOnly !== undefined) ? options.readOnly : false;
+    editorOptions.lineNumbers = (options.lineNumbers !== undefined) ? options.lineNumbers : true;
+    editorOptions.lineWrapping = (options.wordWrap !== undefined) ? options.wordWrap : true;
+    const editor = new CodeMirrorEditor(host, editorOptions);
+    const extra = options.extra;
+    if (extra) {
+      for (const option in extra) {
+        editor.editor.setOption(option, extra[option]);
+      }
+    }
+    return editor;
   }
 
 }

+ 6 - 4
src/console/codemirror/plugin.ts

@@ -40,10 +40,12 @@ function activateRendererProvider(app: JupyterLab, editorServices: IEditorServic
   });
   const promptRenderer = new CodeCellWidget.Renderer({
     editorFactory: host => editorServices.factory.newInlineEditor(host.node, {
-      matchBrackets: false,
-      autoCloseBrackets: false,
-      extraKeys: {
-        Enter: function () { /* no-op */ }
+      extra: {
+        matchBrackets: false,
+        autoCloseBrackets: false,
+        extraKeys: {
+          Enter: function () { /* no-op */ }
+        }
       }
     })
   });

+ 4 - 2
src/notebook/codemirror/plugin.ts

@@ -34,8 +34,10 @@ const plugin: JupyterLabPlugin<NotebookPanel.IRenderer> = {
 function activateRendererProvider(app: JupyterLab, editorServices: IEditorServices): NotebookPanel.IRenderer {
   const codeCellRenderer = new CodeCellWidget.Renderer({
     editorFactory: host => editorServices.factory.newInlineEditor(host.node, {
-      matchBrackets: true,
-      autoCloseBrackets: true
+      extra: {
+        matchBrackets: true,
+        autoCloseBrackets: true
+      }
     })
   });
   const rawCellRenderer = new CodeCellWidget.Renderer({