Skip to content

Commit

Permalink
fix(code editor): remount when readOnly change (#1179)
Browse files Browse the repository at this point in the history
* fix(code editor): remount when readOnly change

* fix: remove console
  • Loading branch information
danilowoz authored Aug 19, 2024
1 parent 0cdbf78 commit f30adcb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
34 changes: 33 additions & 1 deletion sandpack-react/src/components/CodeEditor/CodeMirror.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]) =>
Expand All @@ -24,6 +25,37 @@ Object.entries(mocks).forEach(([languageName, mockFile]) =>
))
);

stories.add("Ready only", () => {
return (
<>
<SandpackProvider
files={{
"/index.js": {
code: 'const title = "This is a simple code editor"',
},
}}
>
<ReadOnlyEditor />
</SandpackProvider>
</>
);
});

const ReadOnlyEditor = () => {
const [isReadOnly, setIsReadOnly] = React.useState(false);
const { sandpack } = useSandpack();

return (
<>
<button onClick={() => setIsReadOnly(!isReadOnly)} type="button">
Toggle read only ({isReadOnly ? "true" : "false"})
</button>
<SandpackCodeEditor readOnly={isReadOnly} />
<pre>{JSON.stringify(sandpack.files, null, 2)}</pre>
</>
);
};

stories.add("CustomLanguageShell", () => (
<SandpackProvider>
<CodeEditor
Expand Down
32 changes: 13 additions & 19 deletions sandpack-react/src/components/CodeEditor/CodeMirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export const CodeMirror = React.forwardRef<CodeMirrorRef, CodeMirrorProps>(
);

React.useEffect(() => {
if (!wrapper.current || !shouldInitEditor) return;
if (!wrapper.current || !shouldInitEditor || readOnly) return;

const parentDiv = wrapper.current;
const existingPlaceholder = parentDiv.querySelector(
Expand All @@ -198,16 +198,6 @@ export const CodeMirror = React.forwardRef<CodeMirrorRef, CodeMirrorProps>(
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");
Expand All @@ -216,23 +206,18 @@ export const CodeMirror = React.forwardRef<CodeMirrorRef, CodeMirrorProps>(
"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;

return (): void => {
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",
Expand Down Expand Up @@ -294,6 +279,15 @@ export const CodeMirror = React.forwardRef<CodeMirrorRef, CodeMirrorProps>(

getEditorTheme(),
syntaxHighlighting(highlightTheme),

EditorView.updateListener.of((update) => {
if (update.docChanged) {
const newCode = update.state.doc.toString();

setInternalCode(newCode);
onCodeUpdate?.(newCode);
}
}),
];

if (readOnly) {
Expand Down

0 comments on commit f30adcb

Please sign in to comment.