-
Notifications
You must be signed in to change notification settings - Fork 218
/
CodeMirrorEditorMain.tsx
75 lines (61 loc) · 1.93 KB
/
CodeMirrorEditorMain.tsx
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
import { useEffect } from 'react';
import { type Extension } from '@codemirror/state';
import { keymap, scrollPastEnd } from '@codemirror/view';
import type { IUserHasId } from '@growi/core/dist/interfaces';
import { GlobalCodeMirrorEditorKey } from '../consts';
import { setDataLine } from '../services/extensions/setDataLine';
import { useCodeMirrorEditorIsolated, useCollaborativeEditorMode } from '../stores';
import { CodeMirrorEditor, type CodeMirrorEditorProps } from '.';
const additionalExtensions: Extension[] = [
[
scrollPastEnd(),
setDataLine,
],
];
type Props = CodeMirrorEditorProps & {
user?: IUserHasId,
pageId?: string,
initialValue?: string,
isEditorMode: boolean,
onEditorsUpdated?: (userList: IUserHasId[]) => void,
}
export const CodeMirrorEditorMain = (props: Props): JSX.Element => {
const {
user, pageId, initialValue, isEditorMode,
onSave, onEditorsUpdated, ...otherProps
} = props;
const { data: codeMirrorEditor } = useCodeMirrorEditorIsolated(GlobalCodeMirrorEditorKey.MAIN);
useCollaborativeEditorMode(isEditorMode, user, pageId, initialValue, onEditorsUpdated, codeMirrorEditor);
// setup additional extensions
useEffect(() => {
return codeMirrorEditor?.appendExtensions?.(additionalExtensions);
}, [codeMirrorEditor]);
// set handler to save with shortcut key
useEffect(() => {
if (onSave == null) {
return;
}
const extension = keymap.of([
{
key: 'Mod-s',
preventDefault: true,
run: () => {
const doc = codeMirrorEditor?.getDoc();
if (doc != null) {
onSave();
}
return true;
},
},
]);
const cleanupFunction = codeMirrorEditor?.appendExtensions?.(extension);
return cleanupFunction;
}, [codeMirrorEditor, onSave]);
return (
<CodeMirrorEditor
editorKey={GlobalCodeMirrorEditorKey.MAIN}
onSave={onSave}
{...otherProps}
/>
);
};