@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.
@@ -1,8 +1,8 @@
1
1
  import { useEffect, useState } from 'react';
2
- import { basicSetup as defaultBasicSetup } from '@codemirror/basic-setup';
2
+ import { basicSetup } from '@codemirror/basic-setup';
3
3
  import { EditorState, StateEffect } from '@codemirror/state';
4
- import { indentWithTab as defaultIndentWithTab } from '@codemirror/commands';
5
- import { EditorView, keymap, ViewUpdate, placeholder as extendPlaceholder } from '@codemirror/view';
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 (indentWithTab) {
57
- getExtensions.unshift(keymap.of([defaultIndentWithTab]));
56
+ if (defaultIndentWithTab) {
57
+ getExtensions.unshift(keymap.of([indentWithTab]));
58
58
  }
59
- if (basicSetup) {
60
- getExtensions.unshift(defaultBasicSetup);
59
+ if (defaultBasicSetup) {
60
+ getExtensions.unshift(basicSetup);
61
61
  }
62
62
 
63
- if (placeholder) {
64
- getExtensions.unshift(extendPlaceholder(placeholder));
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 as any,
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
- return () => {
108
+ useEffect(
109
+ () => () => {
110
110
  if (view) {
111
111
  view.destroy();
112
+ setView(undefined);
112
113
  }
113
- };
114
- }, [view]);
114
+ },
115
+ [view],
116
+ );
115
117
 
116
118
  useEffect(() => {
117
- if (view) {
118
- const currentValue = view.state.doc.toString();
119
- if (value !== currentValue) {
120
- view.dispatch({
121
- changes: { from: 0, to: currentValue.length, insert: value || '' },
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
- indentWithTab,
144
- basicSetup,
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
  }