Skip to content

Commit ccc59b4

Browse files
committed
fix: Fix mode options errors.
1 parent 00627a4 commit ccc59b4

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

src/index.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ function ReactCodeMirror(props = {}, ref) {
3535
return eventDict;
3636
}
3737

38+
// http://codemirror.net/doc/manual.html#config
39+
async function setOptions(instance, opt = {}) {
40+
if (typeof opt === 'object') {
41+
const mode = CodeMirror.findModeByName(opt.mode);
42+
if (mode && mode.mode) {
43+
await import(`codemirror/mode/${mode.mode}/${mode.mode}.js`);
44+
}
45+
if (mode) {
46+
opt.mode = mode.mime;
47+
}
48+
Object.keys(opt).forEach((name) => {
49+
if (opt[name] && JSON.stringify(opt[name])) {
50+
instance.setOption(name, opt[name]);
51+
}
52+
});
53+
}
54+
}
55+
3856
useEffect(() => {
3957
if (!editor) {
4058
// 生成codemirror实例
@@ -48,13 +66,14 @@ function ReactCodeMirror(props = {}, ref) {
4866
if (width || height) {
4967
// 设置尺寸
5068
instance.setSize(width, height);
51-
setEditor(undefined);
5269
}
5370
setEditor(instance);
71+
setOptions(instance, {...defaultOptions, ...options});
5472
}
5573
return () => {
5674
if (editor) {
5775
editor.toTextArea();
76+
setEditor(undefined);
5877
}
5978
}
6079
}, []);
@@ -73,27 +92,9 @@ function ReactCodeMirror(props = {}, ref) {
7392
}, [width, height]);
7493

7594

76-
// http://codemirror.net/doc/manual.html#config
77-
async function setOptions(opt = {}) {
78-
if (typeof opt === 'object') {
79-
const mode = CodeMirror.findModeByName(opt.mode);
80-
if (mode && mode.mode) {
81-
await import(`codemirror/mode/${mode.mode}/${mode.mode}.js`);
82-
}
83-
if (mode) {
84-
opt.mode = mode.mime;
85-
}
86-
Object.keys(opt).forEach((name) => {
87-
if (opt[name] && JSON.stringify(opt[name])) {
88-
editor.setOption(name, opt[name]);
89-
}
90-
});
91-
}
92-
}
93-
9495
useMemo(() => {
9596
if (!editor) return;
96-
setOptions({...defaultOptions, ...options});
97+
setOptions(editor, {...defaultOptions, ...options});
9798
}, [options]);
9899

99100
return (

0 commit comments

Comments
 (0)