-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
factory.ts
99 lines (92 loc) · 2.74 KB
/
factory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { CodeEditor, IEditorFactoryService } from '@jupyterlab/codeeditor';
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
import { EditorView, keymap } from '@codemirror/view';
import { EditorExtensionRegistry } from './extension';
import { CodeMirrorEditor } from './editor';
import { EditorLanguageRegistry } from './language';
import {
IEditorExtensionFactory,
IEditorExtensionRegistry,
IEditorFactoryOptions,
IEditorLanguageRegistry
} from './token';
/**
* CodeMirror editor factory.
*/
export class CodeMirrorEditorFactory implements IEditorFactoryService {
/**
* Construct an IEditorFactoryService for CodeMirrorEditors.
*/
constructor(options: IEditorFactoryOptions = {}) {
this.languages = options.languages ?? new EditorLanguageRegistry();
this.extensions = options.extensions ?? new EditorExtensionRegistry();
this.translator = options.translator ?? nullTranslator;
this.inlineCodeMirrorConfig = {
searchWithCM: true
};
this.documentCodeMirrorConfig = {
lineNumbers: true,
scrollPastEnd: true
};
}
/**
* Create a new editor for inline code.
*/
readonly newInlineEditor = (
options: CodeEditor.IOptions
): CodeMirrorEditor => {
options.host.dataset.type = 'inline';
return this.newEditor({
...options,
config: { ...this.inlineCodeMirrorConfig, ...(options.config || {}) },
inline: true
});
};
/**
* Create a new editor for a full document.
*/
readonly newDocumentEditor = (
options: CodeEditor.IOptions
): CodeMirrorEditor => {
options.host.dataset.type = 'document';
return this.newEditor({
...options,
config: { ...this.documentCodeMirrorConfig, ...(options.config ?? {}) },
inline: false,
extensions: [
keymap.of([
{
key: 'Shift-Enter',
run: (target: EditorView) => {
return true;
}
}
])
].concat(options.extensions ?? [])
});
};
/**
* Create a new editor
*
* @param options Editor options
* @returns The editor
*/
protected newEditor(
options: CodeEditor.IOptions & IEditorExtensionFactory.IOptions
): CodeMirrorEditor {
const editor = new CodeMirrorEditor({
extensionsRegistry: this.extensions,
languages: this.languages,
translator: this.translator,
...options
});
return editor;
}
protected extensions: IEditorExtensionRegistry;
protected languages: IEditorLanguageRegistry;
protected translator: ITranslator;
protected inlineCodeMirrorConfig: Record<string, any>;
protected documentCodeMirrorConfig: Record<string, any>;
}