|
@@ -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;
|
|
|
}
|
|
|
|
|
|
}
|