@uiw/react-codemirror 4.23.8 → 4.23.10

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/esm/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import _extends from "@babel/runtime/helpers/extends";
2
2
  import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
3
3
  var _excluded = ["className", "value", "selection", "extensions", "onChange", "onStatistics", "onCreateEditor", "onUpdate", "autoFocus", "theme", "height", "minHeight", "maxHeight", "width", "minWidth", "maxWidth", "basicSetup", "placeholder", "indentWithTab", "editable", "readOnly", "root", "initialState"];
4
- import React, { useRef, forwardRef, useImperativeHandle } from 'react';
4
+ import React, { useRef, forwardRef, useImperativeHandle, useCallback } from 'react';
5
5
  import { useCodeMirror } from './useCodeMirror';
6
6
  import { jsx as _jsx } from "react/jsx-runtime";
7
7
  export * from '@codemirror/view';
@@ -41,9 +41,9 @@ var ReactCodeMirror = /*#__PURE__*/forwardRef((props, ref) => {
41
41
  var {
42
42
  state,
43
43
  view,
44
- container
44
+ container,
45
+ setContainer
45
46
  } = useCodeMirror({
46
- container: editor.current,
47
47
  root,
48
48
  value,
49
49
  autoFocus,
@@ -72,6 +72,10 @@ var ReactCodeMirror = /*#__PURE__*/forwardRef((props, ref) => {
72
72
  state: state,
73
73
  view: view
74
74
  }), [editor, container, state, view]);
75
+ var setEditorRef = useCallback(el => {
76
+ editor.current = el;
77
+ setContainer(el);
78
+ }, [setContainer]);
75
79
 
76
80
  // check type of value
77
81
  if (typeof value !== 'string') {
@@ -79,7 +83,7 @@ var ReactCodeMirror = /*#__PURE__*/forwardRef((props, ref) => {
79
83
  }
80
84
  var defaultClassNames = typeof theme === 'string' ? "cm-theme-" + theme : 'cm-theme';
81
85
  return /*#__PURE__*/_jsx("div", _extends({
82
- ref: editor,
86
+ ref: setEditorRef,
83
87
  className: "" + defaultClassNames + (className ? " " + className : '')
84
88
  }, other));
85
89
  });
@@ -1,4 +1,4 @@
1
- import { useEffect, useState } from 'react';
1
+ import { useEffect, useLayoutEffect, useState } from 'react';
2
2
  import { Annotation, EditorState, StateEffect } from '@codemirror/state';
3
3
  import { EditorView } from '@codemirror/view';
4
4
  import { getDefaultExtensions } from './getDefaultExtensions';
@@ -70,7 +70,7 @@ export function useCodeMirror(props) {
70
70
  getExtensions.push(EditorView.updateListener.of(onUpdate));
71
71
  }
72
72
  getExtensions = getExtensions.concat(extensions);
73
- useEffect(() => {
73
+ useLayoutEffect(() => {
74
74
  if (container && !state) {
75
75
  var config = {
76
76
  doc: value,
@@ -96,7 +96,11 @@ export function useCodeMirror(props) {
96
96
  }
97
97
  };
98
98
  }, [container, state]);
99
- useEffect(() => setContainer(props.container), [props.container]);
99
+ useEffect(() => {
100
+ if (props.container) {
101
+ setContainer(props.container);
102
+ }
103
+ }, [props.container]);
100
104
  useEffect(() => () => {
101
105
  if (view) {
102
106
  view.destroy();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uiw/react-codemirror",
3
- "version": "4.23.8",
3
+ "version": "4.23.10",
4
4
  "description": "CodeMirror component for React.",
5
5
  "homepage": "https://uiwjs.github.io/react-codemirror",
6
6
  "funding": "https://jaywcjlove.github.io/#/sponsor",
@@ -46,7 +46,7 @@
46
46
  "@codemirror/commands": "^6.1.0",
47
47
  "@codemirror/state": "^6.1.1",
48
48
  "@codemirror/theme-one-dark": "^6.0.0",
49
- "@uiw/codemirror-extensions-basic-setup": "4.23.8",
49
+ "@uiw/codemirror-extensions-basic-setup": "4.23.10",
50
50
  "codemirror": "^6.0.0"
51
51
  },
52
52
  "keywords": [
package/src/index.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useRef, forwardRef, useImperativeHandle } from 'react';
1
+ import React, { useRef, forwardRef, useImperativeHandle, useCallback } from 'react';
2
2
  import type { EditorState, EditorStateConfig, Extension, StateField } from '@codemirror/state';
3
3
  import type { EditorView, ViewUpdate } from '@codemirror/view';
4
4
  import { type BasicSetupOptions } from '@uiw/codemirror-extensions-basic-setup';
@@ -116,9 +116,8 @@ const ReactCodeMirror = forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((pr
116
116
  initialState,
117
117
  ...other
118
118
  } = props;
119
- const editor = useRef<HTMLDivElement>(null);
120
- const { state, view, container } = useCodeMirror({
121
- container: editor.current,
119
+ const editor = useRef<HTMLDivElement | null>(null);
120
+ const { state, view, container, setContainer } = useCodeMirror({
122
121
  root,
123
122
  value,
124
123
  autoFocus,
@@ -150,13 +149,23 @@ const ReactCodeMirror = forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((pr
150
149
  view,
151
150
  ]);
152
151
 
152
+ const setEditorRef = useCallback(
153
+ (el: HTMLDivElement) => {
154
+ editor.current = el;
155
+ setContainer(el);
156
+ },
157
+ [setContainer],
158
+ );
159
+
153
160
  // check type of value
154
161
  if (typeof value !== 'string') {
155
162
  throw new Error(`value must be typeof string but got ${typeof value}`);
156
163
  }
157
164
 
158
165
  const defaultClassNames = typeof theme === 'string' ? `cm-theme-${theme}` : 'cm-theme';
159
- return <div ref={editor} className={`${defaultClassNames}${className ? ` ${className}` : ''}`} {...other}></div>;
166
+ return (
167
+ <div ref={setEditorRef} className={`${defaultClassNames}${className ? ` ${className}` : ''}`} {...other}></div>
168
+ );
160
169
  });
161
170
 
162
171
  ReactCodeMirror.displayName = 'CodeMirror';
@@ -1,4 +1,4 @@
1
- import { useEffect, useState } from 'react';
1
+ import { useEffect, useLayoutEffect, useState } from 'react';
2
2
  import { Annotation, EditorState, StateEffect, type Extension } from '@codemirror/state';
3
3
  import { EditorView, type ViewUpdate } from '@codemirror/view';
4
4
  import { getDefaultExtensions } from './getDefaultExtensions';
@@ -85,7 +85,7 @@ export function useCodeMirror(props: UseCodeMirror) {
85
85
  }
86
86
  getExtensions = getExtensions.concat(extensions);
87
87
 
88
- useEffect(() => {
88
+ useLayoutEffect(() => {
89
89
  if (container && !state) {
90
90
  const config = {
91
91
  doc: value,
@@ -114,7 +114,11 @@ export function useCodeMirror(props: UseCodeMirror) {
114
114
  };
115
115
  }, [container, state]);
116
116
 
117
- useEffect(() => setContainer(props.container), [props.container]);
117
+ useEffect(() => {
118
+ if (props.container) {
119
+ setContainer(props.container);
120
+ }
121
+ }, [props.container]);
118
122
 
119
123
  useEffect(
120
124
  () => () => {