aboutsummaryrefslogtreecommitdiffstats
path: root/language-web/src/main/js/editor/EditorStore.ts
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <marussy@mit.bme.hu>2021-08-22 19:54:51 +0200
committerLibravatar Kristóf Marussy <marussy@mit.bme.hu>2021-08-22 19:54:51 +0200
commit8cbf8fdcfdceab8a330bdc82e4260a55c125c37d (patch)
tree0354dcc6ce0704fc953e7665ecfcc700609549a2 /language-web/src/main/js/editor/EditorStore.ts
parentBump Material-UI version (diff)
downloadrefinery-8cbf8fdcfdceab8a330bdc82e4260a55c125c37d.tar.gz
refinery-8cbf8fdcfdceab8a330bdc82e4260a55c125c37d.tar.zst
refinery-8cbf8fdcfdceab8a330bdc82e4260a55c125c37d.zip
Covert language-web to TypeScript
Diffstat (limited to 'language-web/src/main/js/editor/EditorStore.ts')
-rw-r--r--language-web/src/main/js/editor/EditorStore.ts127
1 files changed, 127 insertions, 0 deletions
diff --git a/language-web/src/main/js/editor/EditorStore.ts b/language-web/src/main/js/editor/EditorStore.ts
new file mode 100644
index 00000000..167e1ade
--- /dev/null
+++ b/language-web/src/main/js/editor/EditorStore.ts
@@ -0,0 +1,127 @@
1import { Editor, EditorConfiguration } from 'codemirror';
2import {
3 createAtom,
4 makeAutoObservable,
5 observable,
6} from 'mobx';
7import 'mode-problem';
8import {
9 IXtextOptions,
10 IXtextServices,
11 createServices,
12 removeServices,
13} from 'xtext/xtext-codemirror';
14
15const xtextLang = 'problem';
16
17const xtextOptions: IXtextOptions = {
18 xtextLang,
19 enableFormattingAction: true,
20};
21
22const codeMirrorGlobalOptions: EditorConfiguration = {
23 mode: `xtext/${xtextLang}`,
24 indentUnit: 2,
25 theme: 'material-darker',
26};
27
28export default class EditorStore {
29 _atom;
30 editor?: Editor;
31 xtextServices?: IXtextServices;
32 value = '';
33 showLineNumbers = false;
34
35 constructor() {
36 this._atom = createAtom('EditorStore');
37 makeAutoObservable(this, {
38 _atom: false,
39 editor: observable.ref,
40 xtextServices: observable.ref,
41 });
42 }
43
44 /**
45 * Attaches a new CodeMirror instance and creates Xtext services.
46 *
47 * The store will not subscribe to any CodeMirror events. Instead,
48 * the editor component should subscribe to them and relay them to the store.
49 *
50 * @param newEditor The new CodeMirror instance
51 */
52 editorDidMount(newEditor: Editor) {
53 if (this.editor) {
54 throw new Error('CoreMirror editor mounted before unmounting');
55 }
56 this.editor = newEditor;
57 this.xtextServices = createServices(newEditor, xtextOptions);
58 }
59
60 editorWillUnmount() {
61 if (this.editor) {
62 removeServices(this.editor);
63 }
64 this.editor = undefined;
65 this.xtextServices = undefined;
66 }
67
68 /**
69 * Updates the contents of the editor.
70 *
71 * @param newValue The new contents of the editor
72 */
73 updateValue(newValue: string) {
74 this.value = newValue;
75 }
76
77 reportChanged() {
78 this._atom.reportChanged();
79 }
80
81 _observeEditorChanges() {
82 this._atom.reportObserved();
83 }
84
85 get codeMirrorOptions(): EditorConfiguration {
86 return {
87 ...codeMirrorGlobalOptions,
88 lineNumbers: this.showLineNumbers,
89 };
90 }
91
92 /**
93 * @returns `true` if there is history to undo
94 */
95 get canUndo() {
96 this._observeEditorChanges();
97 if (!this.editor) {
98 return false;
99 }
100 const { undo: undoSize } = this.editor.historySize();
101 return undoSize > 0;
102 }
103
104 undo() {
105 this.editor?.undo();
106 }
107
108 /**
109 * @returns `true` if there is history to redo
110 */
111 get canRedo() {
112 this._observeEditorChanges();
113 if (!this.editor) {
114 return false;
115 }
116 const { redo: redoSize } = this.editor.historySize();
117 return redoSize > 0;
118 }
119
120 redo() {
121 this.editor?.redo();
122 }
123
124 toggleLineNumbers() {
125 this.showLineNumbers = !this.showLineNumbers;
126 }
127}