From f30adcb17fda8b87f087dbb0dfba1f8e46cdc0db Mon Sep 17 00:00:00 2001 From: Danilo Woznica Date: Mon, 19 Aug 2024 21:02:22 +0100 Subject: [PATCH] fix(code editor): remount when readOnly change (#1179) * fix(code editor): remount when readOnly change * fix: remove console --- .../CodeEditor/CodeMirror.stories.tsx | 34 ++++++++++++++++++- .../src/components/CodeEditor/CodeMirror.tsx | 32 +++++++---------- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/sandpack-react/src/components/CodeEditor/CodeMirror.stories.tsx b/sandpack-react/src/components/CodeEditor/CodeMirror.stories.tsx index 1b161be4d..8ec150474 100644 --- a/sandpack-react/src/components/CodeEditor/CodeMirror.stories.tsx +++ b/sandpack-react/src/components/CodeEditor/CodeMirror.stories.tsx @@ -8,7 +8,8 @@ import { SandpackProvider } from "../../contexts/sandpackContext"; import * as mocks from "./languages-mocks"; -import { CodeEditor } from "./index"; +import { CodeEditor, SandpackCodeEditor } from "./index"; +import { useSandpack } from "../../hooks"; const stories = storiesOf("components/CodeMirror", module); Object.entries(mocks).forEach(([languageName, mockFile]) => @@ -24,6 +25,37 @@ Object.entries(mocks).forEach(([languageName, mockFile]) => )) ); +stories.add("Ready only", () => { + return ( + <> + + + + + ); +}); + +const ReadOnlyEditor = () => { + const [isReadOnly, setIsReadOnly] = React.useState(false); + const { sandpack } = useSandpack(); + + return ( + <> + + +
{JSON.stringify(sandpack.files, null, 2)}
+ + ); +}; + stories.add("CustomLanguageShell", () => ( ( ); React.useEffect(() => { - if (!wrapper.current || !shouldInitEditor) return; + if (!wrapper.current || !shouldInitEditor || readOnly) return; const parentDiv = wrapper.current; const existingPlaceholder = parentDiv.querySelector( @@ -198,16 +198,6 @@ export const CodeMirror = React.forwardRef( doc: code, extensions: [], parent: parentDiv, - dispatch: (tr): void => { - view.update([tr]); - - if (tr.docChanged) { - const newCode = tr.newDoc.sliceString(0, tr.newDoc.length); - - setInternalCode(newCode); - onCodeUpdate?.(newCode); - } - }, }); view.contentDOM.setAttribute("data-gramm", "false"); @@ -216,12 +206,7 @@ export const CodeMirror = React.forwardRef( "aria-label", filePath ? `Code Editor for ${getFileName(filePath)}` : `Code Editor` ); - - if (readOnly) { - view.contentDOM.classList.add("cm-readonly"); - } else { - view.contentDOM.setAttribute("tabIndex", "-1"); - } + view.contentDOM.setAttribute("tabIndex", "-1"); cmView.current = view; @@ -229,10 +214,10 @@ export const CodeMirror = React.forwardRef( cmView.current?.destroy(); }; // eslint-disable-next-line react-hooks/exhaustive-deps - }, [shouldInitEditor]); + }, [shouldInitEditor, readOnly]); React.useEffect(() => { - if (cmView.current) { + if (cmView.current && !readOnly) { const customCommandsKeymap: KeyBinding[] = [ { key: "Tab", @@ -294,6 +279,15 @@ export const CodeMirror = React.forwardRef( getEditorTheme(), syntaxHighlighting(highlightTheme), + + EditorView.updateListener.of((update) => { + if (update.docChanged) { + const newCode = update.state.doc.toString(); + + setInternalCode(newCode); + onCodeUpdate?.(newCode); + } + }), ]; if (readOnly) {