diff --git a/apps/builder/app/builder/shared/code-editor-base.tsx b/apps/builder/app/builder/shared/code-editor-base.tsx index d0abfe9aaceb..7c4c3c0b8c8b 100644 --- a/apps/builder/app/builder/shared/code-editor-base.tsx +++ b/apps/builder/app/builder/shared/code-editor-base.tsx @@ -268,7 +268,6 @@ export const EditorContent = ({ }, []); // setup editor - useEffect(() => { if (editorRef.current === null) { return; @@ -277,13 +276,17 @@ export const EditorContent = ({ doc: "", parent: editorRef.current, }); - if (autoFocus) { - view.focus(); - } + viewRef.current = view; return () => { view.destroy(); }; + }, []); + + useEffect(() => { + if (autoFocus) { + viewRef.current?.focus(); + } }, [autoFocus]); // update extensions whenever variables data is changed @@ -364,6 +367,7 @@ export const EditorContent = ({ if (value === view.state.doc.toString()) { return; } + view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: value }, annotations: [ExternalChange.of(true)], @@ -376,6 +380,7 @@ export const EditorContent = ({ if (view === undefined) { return; } + view.dispatch(view.state.replaceSelection(string)); view.focus(); },