diff options
Diffstat (limited to 'language-web/src/main/js/editor/EditorStore.jsx')
-rw-r--r-- | language-web/src/main/js/editor/EditorStore.jsx | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/language-web/src/main/js/editor/EditorStore.jsx b/language-web/src/main/js/editor/EditorStore.jsx new file mode 100644 index 00000000..9c286c28 --- /dev/null +++ b/language-web/src/main/js/editor/EditorStore.jsx | |||
@@ -0,0 +1,75 @@ | |||
1 | import CodeMirror from 'codemirror'; | ||
2 | import { createAtom, makeAutoObservable, observable } from 'mobx'; | ||
3 | |||
4 | export default class EditorStore { | ||
5 | atom; | ||
6 | /** @type {CodeMirror} */ | ||
7 | editor = null; | ||
8 | /** @type {string} */ | ||
9 | value = ''; | ||
10 | |||
11 | constructor() { | ||
12 | this.atom = createAtom('EditorStore'); | ||
13 | makeAutoObservable(this, { | ||
14 | atom: false, | ||
15 | editor: observable.ref, | ||
16 | }); | ||
17 | } | ||
18 | |||
19 | /** | ||
20 | * Attaches a new CodeMirror instance. | ||
21 | * | ||
22 | * The store will node subscribe to any CodeMirror events. Instead, | ||
23 | * the editor component should subscribe to them and relay them to the store. | ||
24 | * | ||
25 | * @param {CodeMirror} newEditor The new CodeMirror instance | ||
26 | */ | ||
27 | updateEditor(newEditor) { | ||
28 | this.editor = newEditor; | ||
29 | } | ||
30 | |||
31 | /** | ||
32 | * Updates the contents of the editor. | ||
33 | * | ||
34 | * @param {string} newValue The new contents of the editor | ||
35 | */ | ||
36 | updateValue(newValue) { | ||
37 | this.value = newValue; | ||
38 | } | ||
39 | |||
40 | reportChanged() { | ||
41 | this.atom.reportChanged(); | ||
42 | } | ||
43 | |||
44 | /** | ||
45 | * @returns {boolean} `true` if there is history to undo | ||
46 | */ | ||
47 | get canUndo() { | ||
48 | this.atom.reportObserved(); | ||
49 | if (!this.editor) { | ||
50 | return false; | ||
51 | } | ||
52 | const { undo: undoSize } = this.editor.historySize(); | ||
53 | return undoSize > 0; | ||
54 | } | ||
55 | |||
56 | undo() { | ||
57 | this.editor.undo(); | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * @returns {boolean} `true` if there is history to redo | ||
62 | */ | ||
63 | get canRedo() { | ||
64 | this.atom.reportObserved(); | ||
65 | if (!this.editor) { | ||
66 | return false; | ||
67 | } | ||
68 | const { redo: redoSize } = this.editor.historySize(); | ||
69 | return redoSize > 0; | ||
70 | } | ||
71 | |||
72 | redo() { | ||
73 | this.editor.redo(); | ||
74 | } | ||
75 | } | ||