/
useCodeMirror.ts
142 lines (130 loc) · 3.58 KB
/
useCodeMirror.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { useEffect, useState } from 'react';
import { basicSetup as defaultBasicSetup } from '@codemirror/basic-setup';
import { EditorState, StateEffect } from '@codemirror/state';
import { indentWithTab as defaultIndentWithTab } from '@codemirror/commands';
import { EditorView, keymap, ViewUpdate, placeholder as extendPlaceholder } from '@codemirror/view';
import { oneDarkTheme } from '@codemirror/theme-one-dark';
import { ReactCodeMirrorProps } from './';
import { defaultLightThemeOption } from './theme/light';
export interface UseCodeMirror extends ReactCodeMirrorProps {
container?: HTMLDivElement | null;
}
export function useCodeMirror(props: UseCodeMirror) {
const {
value,
selection,
onChange,
onUpdate,
extensions = [],
autoFocus,
theme = 'light',
height = '',
minHeight = '',
maxHeight = '',
placeholder = '',
width = '',
minWidth = '',
maxWidth = '',
editable = true,
indentWithTab = true,
basicSetup = true,
} = props;
const [container, setContainer] = useState(props.container);
const [view, setView] = useState<EditorView>();
const [state, setState] = useState<EditorState>();
const defaultThemeOption = EditorView.theme({
'&': {
height,
minHeight,
maxHeight,
width,
minWidth,
maxWidth,
},
});
const updateListener = EditorView.updateListener.of((vu: ViewUpdate) => {
if (vu.docChanged && typeof onChange === 'function') {
const doc = vu.state.doc;
const value = doc.toString();
onChange(value, vu);
}
});
let getExtensions = [updateListener, defaultThemeOption];
if (indentWithTab) {
getExtensions.unshift(keymap.of([defaultIndentWithTab]));
}
if (basicSetup) {
getExtensions.unshift(defaultBasicSetup);
}
if (placeholder) {
getExtensions.unshift(extendPlaceholder(placeholder));
}
theme === 'light' ? getExtensions.push(defaultLightThemeOption) : getExtensions.push(oneDarkTheme);
if (editable === false) {
getExtensions.push(EditorView.editable.of(false));
}
if (onUpdate && typeof onUpdate === 'function') {
getExtensions.push(EditorView.updateListener.of(onUpdate));
}
getExtensions = getExtensions.concat(extensions);
useEffect(() => {
if (container && !state) {
const stateCurrent = EditorState.create({
doc: value,
selection,
extensions: getExtensions,
});
setState(stateCurrent);
if (!view) {
const viewCurrent = new EditorView({
state: stateCurrent,
parent: container as any,
});
setView(viewCurrent);
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [container, state]);
useEffect(() => {
return () => {
if (view) {
view.destroy();
}
};
}, [view]);
useEffect(() => {
if (view) {
const currentValue = view.state.doc.toString();
if (value !== currentValue) {
view.dispatch({
changes: { from: 0, to: currentValue.length, insert: value || '' },
});
}
}
}, [value, view]);
useEffect(() => {
if (view) {
view.dispatch({ effects: StateEffect.reconfigure.of(getExtensions) });
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
theme,
extensions,
placeholder,
height,
minHeight,
maxHeight,
width,
minWidth,
maxWidth,
editable,
indentWithTab,
basicSetup,
]);
useEffect(() => {
if (autoFocus && view) {
view.focus();
}
}, [autoFocus, view]);
return { state, setState, view, setView, container, setContainer };
}