@uiw/react-codemirror 4.3.3 → 4.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -0
- package/cjs/index.js +1 -6
- package/cjs/index.js.map +2 -3
- package/cjs/useCodeMirror.js +24 -25
- package/cjs/useCodeMirror.js.map +8 -6
- package/dist/codemirror.js +8133 -0
- package/dist/codemirror.min.js +2 -0
- package/dist/codemirror.min.js.LICENSE.txt +14 -0
- package/esm/index.js +1 -6
- package/esm/index.js.map +2 -3
- package/esm/useCodeMirror.js +32 -35
- package/esm/useCodeMirror.js.map +8 -7
- package/package.json +15 -16
- package/src/__tests__/index.test.tsx +87 -0
- package/src/index.tsx +0 -5
- package/src/useCodeMirror.ts +33 -33
package/src/useCodeMirror.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { useEffect, useState } from 'react';
|
|
2
|
-
import { basicSetup
|
|
2
|
+
import { basicSetup } from '@codemirror/basic-setup';
|
|
3
3
|
import { EditorState, StateEffect } from '@codemirror/state';
|
|
4
|
-
import { indentWithTab
|
|
5
|
-
import { EditorView, keymap, ViewUpdate, placeholder
|
|
4
|
+
import { indentWithTab } from '@codemirror/commands';
|
|
5
|
+
import { EditorView, keymap, ViewUpdate, placeholder } from '@codemirror/view';
|
|
6
6
|
import { oneDark } from '@codemirror/theme-one-dark';
|
|
7
7
|
import { ReactCodeMirrorProps } from './';
|
|
8
8
|
import { defaultLightThemeOption } from './theme/light';
|
|
@@ -23,13 +23,13 @@ export function useCodeMirror(props: UseCodeMirror) {
|
|
|
23
23
|
height = '',
|
|
24
24
|
minHeight = '',
|
|
25
25
|
maxHeight = '',
|
|
26
|
-
placeholder = '',
|
|
26
|
+
placeholder: placeholderStr = '',
|
|
27
27
|
width = '',
|
|
28
28
|
minWidth = '',
|
|
29
29
|
maxWidth = '',
|
|
30
30
|
editable = true,
|
|
31
|
-
indentWithTab = true,
|
|
32
|
-
basicSetup = true,
|
|
31
|
+
indentWithTab: defaultIndentWithTab = true,
|
|
32
|
+
basicSetup: defaultBasicSetup = true,
|
|
33
33
|
root,
|
|
34
34
|
} = props;
|
|
35
35
|
const [container, setContainer] = useState(props.container);
|
|
@@ -53,15 +53,15 @@ export function useCodeMirror(props: UseCodeMirror) {
|
|
|
53
53
|
}
|
|
54
54
|
});
|
|
55
55
|
let getExtensions = [updateListener, defaultThemeOption];
|
|
56
|
-
if (
|
|
57
|
-
getExtensions.unshift(keymap.of([
|
|
56
|
+
if (defaultIndentWithTab) {
|
|
57
|
+
getExtensions.unshift(keymap.of([indentWithTab]));
|
|
58
58
|
}
|
|
59
|
-
if (
|
|
60
|
-
getExtensions.unshift(
|
|
59
|
+
if (defaultBasicSetup) {
|
|
60
|
+
getExtensions.unshift(basicSetup);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
if (
|
|
64
|
-
getExtensions.unshift(
|
|
63
|
+
if (placeholderStr) {
|
|
64
|
+
getExtensions.unshift(placeholder(placeholderStr));
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
switch (theme) {
|
|
@@ -96,7 +96,7 @@ export function useCodeMirror(props: UseCodeMirror) {
|
|
|
96
96
|
if (!view) {
|
|
97
97
|
const viewCurrent = new EditorView({
|
|
98
98
|
state: stateCurrent,
|
|
99
|
-
parent: container
|
|
99
|
+
parent: container,
|
|
100
100
|
root,
|
|
101
101
|
});
|
|
102
102
|
setView(viewCurrent);
|
|
@@ -105,22 +105,28 @@ export function useCodeMirror(props: UseCodeMirror) {
|
|
|
105
105
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
106
106
|
}, [container, state]);
|
|
107
107
|
|
|
108
|
-
useEffect(
|
|
109
|
-
|
|
108
|
+
useEffect(
|
|
109
|
+
() => () => {
|
|
110
110
|
if (view) {
|
|
111
111
|
view.destroy();
|
|
112
|
+
setView(undefined);
|
|
112
113
|
}
|
|
113
|
-
}
|
|
114
|
-
|
|
114
|
+
},
|
|
115
|
+
[view],
|
|
116
|
+
);
|
|
115
117
|
|
|
116
118
|
useEffect(() => {
|
|
117
|
-
if (view) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
119
|
+
if (autoFocus && view) {
|
|
120
|
+
view.focus();
|
|
121
|
+
}
|
|
122
|
+
}, [autoFocus, view]);
|
|
123
|
+
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
const currentValue = view ? view.state.doc.toString() : '';
|
|
126
|
+
if (view && value !== currentValue) {
|
|
127
|
+
view.dispatch({
|
|
128
|
+
changes: { from: 0, to: currentValue.length, insert: value || '' },
|
|
129
|
+
});
|
|
124
130
|
}
|
|
125
131
|
}, [value, view]);
|
|
126
132
|
|
|
@@ -132,23 +138,17 @@ export function useCodeMirror(props: UseCodeMirror) {
|
|
|
132
138
|
}, [
|
|
133
139
|
theme,
|
|
134
140
|
extensions,
|
|
135
|
-
placeholder,
|
|
136
141
|
height,
|
|
137
142
|
minHeight,
|
|
138
143
|
maxHeight,
|
|
139
144
|
width,
|
|
145
|
+
placeholderStr,
|
|
140
146
|
minWidth,
|
|
141
147
|
maxWidth,
|
|
142
148
|
editable,
|
|
143
|
-
|
|
144
|
-
|
|
149
|
+
defaultIndentWithTab,
|
|
150
|
+
defaultBasicSetup,
|
|
145
151
|
]);
|
|
146
152
|
|
|
147
|
-
useEffect(() => {
|
|
148
|
-
if (autoFocus && view) {
|
|
149
|
-
view.focus();
|
|
150
|
-
}
|
|
151
|
-
}, [autoFocus, view]);
|
|
152
|
-
|
|
153
153
|
return { state, setState, view, setView, container, setContainer };
|
|
154
154
|
}
|