@uiw/react-codemirror 4.3.2 → 4.4.2
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 +14 -2
- package/cjs/index.js +4 -8
- package/cjs/index.js.map +2 -3
- package/cjs/useCodeMirror.js +22 -25
- package/cjs/useCodeMirror.js.map +8 -6
- package/dist/codemirror.js +8131 -0
- package/dist/codemirror.min.js +2 -0
- package/dist/codemirror.min.js.LICENSE.txt +14 -0
- package/esm/index.js +3 -8
- package/esm/index.js.map +2 -3
- package/esm/useCodeMirror.js +28 -35
- package/esm/useCodeMirror.js.map +8 -7
- package/package.json +17 -18
- package/src/__tests__/index.test.tsx +87 -0
- package/src/index.tsx +2 -7
- package/src/useCodeMirror.ts +29 -33
- package/src/react-app-env.d.ts +0 -1
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,24 @@ 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
|
+
);
|
|
117
|
+
|
|
118
|
+
useEffect(() => autoFocus && view && (view.focus() as any), [autoFocus, view]);
|
|
115
119
|
|
|
116
120
|
useEffect(() => {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
});
|
|
123
|
-
}
|
|
121
|
+
const currentValue = view ? view.state.doc.toString() : '';
|
|
122
|
+
if (view && value !== currentValue) {
|
|
123
|
+
view.dispatch({
|
|
124
|
+
changes: { from: 0, to: currentValue.length, insert: value || '' },
|
|
125
|
+
});
|
|
124
126
|
}
|
|
125
127
|
}, [value, view]);
|
|
126
128
|
|
|
@@ -132,23 +134,17 @@ export function useCodeMirror(props: UseCodeMirror) {
|
|
|
132
134
|
}, [
|
|
133
135
|
theme,
|
|
134
136
|
extensions,
|
|
135
|
-
placeholder,
|
|
136
137
|
height,
|
|
137
138
|
minHeight,
|
|
138
139
|
maxHeight,
|
|
139
140
|
width,
|
|
141
|
+
placeholderStr,
|
|
140
142
|
minWidth,
|
|
141
143
|
maxWidth,
|
|
142
144
|
editable,
|
|
143
|
-
|
|
144
|
-
|
|
145
|
+
defaultIndentWithTab,
|
|
146
|
+
defaultBasicSetup,
|
|
145
147
|
]);
|
|
146
148
|
|
|
147
|
-
useEffect(() => {
|
|
148
|
-
if (autoFocus && view) {
|
|
149
|
-
view.focus();
|
|
150
|
-
}
|
|
151
|
-
}, [autoFocus, view]);
|
|
152
|
-
|
|
153
149
|
return { state, setState, view, setView, container, setContainer };
|
|
154
150
|
}
|
package/src/react-app-env.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
/// <reference types="react-scripts" />
|