Переглянути джерело

CodeMirrorEditorFactory takes optional CM options

Vidar Tonaas Fauske 8 роки тому
батько
коміт
b18ca0b11f
1 змінених файлів з 49 додано та 12 видалено
  1. 49 12
      src/codemirror/factory.ts

+ 49 - 12
src/codemirror/factory.ts

@@ -1,6 +1,9 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
+import * as CodeMirror
+  from 'codemirror';
+
 import {
   CodeEditor, IEditorFactoryService
 } from '../codeeditor';
@@ -17,10 +20,10 @@ export
 class CodeMirrorEditorFactory implements IEditorFactoryService {
 
   /**
-   * Create a new editor for inline code.
+   * Construct an IEditorFactoryService for CodeMirrorEditors
    */
-  newInlineEditor(options: CodeEditor.IOptions): CodeEditor.IEditor {
-    return new CodeMirrorEditor(options, {
+  constructor(codeMirrorOptions?: CodeMirror.EditorConfiguration) {
+    this.inlineCodeMirrorOptions = {
       extraKeys: {
         'Cmd-Right': 'goLineRight',
         'End': 'goLineRight',
@@ -32,21 +35,55 @@ class CodeMirrorEditorFactory implements IEditorFactoryService {
         'Cmd-/': 'toggleComment',
         'Ctrl-/': 'toggleComment',
       }
-    });
-  }
-
-  /**
-   * Create a new editor for a full document.
-   */
-  newDocumentEditor(options: CodeEditor.IOptions): CodeEditor.IEditor {
-    return new CodeMirrorEditor(options, {
+    };
+    this.documentCodeMirrorOptions = {
       extraKeys: {
         'Tab': 'indentMore',
         'Shift-Enter': () => { /* no-op */ }
       },
       lineNumbers: true,
       lineWrapping: true
-    });
+    };
+    if (codeMirrorOptions !== undefined) {
+      // Note: If codeMirrorOptions include `extraKeys`,
+      // existing option will be overwritten.
+      Private.assign(this.inlineCodeMirrorOptions, codeMirrorOptions);
+      Private.assign(this.documentCodeMirrorOptions, codeMirrorOptions);
+    }
+  }
+
+  /**
+   * Create a new editor for inline code.
+   */
+  newInlineEditor(options: CodeEditor.IOptions): CodeEditor.IEditor {
+    return new CodeMirrorEditor(options, this.inlineCodeMirrorOptions);
+  }
+
+  /**
+   * Create a new editor for a full document.
+   */
+  newDocumentEditor(options: CodeEditor.IOptions): CodeEditor.IEditor {
+    return new CodeMirrorEditor(options, this.documentCodeMirrorOptions);
   }
 
+  protected inlineCodeMirrorOptions: CodeMirror.EditorConfiguration;
+  protected documentCodeMirrorOptions: CodeMirror.EditorConfiguration;
+
+}
+
+
+namespace Private {
+  // Replace with Object.assign when available
+  export
+  function assign<T>(target: T, ...configs: any[]): T {
+    for (const source of configs) {
+      if (source) {
+        Object.keys(source).forEach(key => {
+          (target as any)[key] = (source as any)[key];
+        });
+      }
+    }
+
+    return target;
+  }
 }