diff options
Diffstat (limited to 'language-web/src/main/js/editor/Editor.jsx')
-rw-r--r-- | language-web/src/main/js/editor/Editor.jsx | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/language-web/src/main/js/editor/Editor.jsx b/language-web/src/main/js/editor/Editor.jsx new file mode 100644 index 00000000..c4b2e183 --- /dev/null +++ b/language-web/src/main/js/editor/Editor.jsx | |||
@@ -0,0 +1,50 @@ | |||
1 | import { observer } from 'mobx-react-lite'; | ||
2 | import 'mode-problem'; | ||
3 | import React, { useCallback } from 'react'; | ||
4 | import { Controlled as CodeMirror } from 'react-codemirror2'; | ||
5 | import { createServices, removeServices } from 'xtext/xtext-codemirror'; | ||
6 | |||
7 | import { useRootStore } from '../RootStore'; | ||
8 | |||
9 | export default observer(() => { | ||
10 | const editorStore = useRootStore().editorStore; | ||
11 | |||
12 | const codeMirrorOptions = { | ||
13 | mode: 'xtext/problem', | ||
14 | indentUnit: 2, | ||
15 | }; | ||
16 | |||
17 | const xtextOptions = { | ||
18 | xtextLang: 'problem', | ||
19 | enableFormattingAction: true, | ||
20 | } | ||
21 | |||
22 | const editorDidMount = useCallback((editor) => { | ||
23 | createServices(editor, xtextOptions); | ||
24 | editorStore.updateEditor(editor); | ||
25 | }, [editorStore]); | ||
26 | |||
27 | const editorWillUnmount = useCallback((editor) => { | ||
28 | editorStore.editor = null; | ||
29 | removeServices(editor); | ||
30 | }, [editorStore]); | ||
31 | |||
32 | const onBeforeChange = useCallback((_editor, _data, value) => { | ||
33 | editorStore.updateValue(value); | ||
34 | }, [editorStore]); | ||
35 | |||
36 | const onChange = useCallback((_editor, _data, _value) => { | ||
37 | editorStore.reportChanged(); | ||
38 | }, [editorStore]); | ||
39 | |||
40 | return ( | ||
41 | <CodeMirror | ||
42 | value={editorStore.value} | ||
43 | options={codeMirrorOptions} | ||
44 | editorDidMount={editorDidMount} | ||
45 | editorWillUnmount={editorWillUnmount} | ||
46 | onBeforeChange={onBeforeChange} | ||
47 | onChange={onChange} | ||
48 | /> | ||
49 | ); | ||
50 | }); | ||