Skip to content

Commit

Permalink
fix(codemirror): shift approach to remove placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
danilowoz committed Jan 20, 2022
1 parent 18db086 commit 12583ba
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions sandpack-react/src/components/CodeEditor/CodeMirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ export const CodeMirror = React.forwardRef<CodeMirrorRef, CodeMirrorProps>(
});

const parentDiv = wrapper.current;
const existingPlaceholder = parentDiv.querySelector(
".sp-pre-placeholder"
);
if (existingPlaceholder) {
parentDiv.removeChild(existingPlaceholder);
}

const view = new EditorView({
state: startState,
Expand Down Expand Up @@ -350,9 +356,7 @@ export const CodeMirror = React.forwardRef<CodeMirrorRef, CodeMirrorProps>(
if (readOnly) {
return (
<pre ref={combinedRef} className={c("cm", editorState)} translate="no">
{!shouldInitEditor && (
<code className={c("pre-placeholder")}>{code}</code>
)}
<code className={c("pre-placeholder")}>{code}</code>

{readOnly && showReadOnly && (
<span className={c("read-only")}>Read-only</span>
Expand All @@ -376,16 +380,14 @@ export const CodeMirror = React.forwardRef<CodeMirrorRef, CodeMirrorProps>(
tabIndex={0}
translate="no"
>
{!shouldInitEditor && (
<pre
className={c("pre-placeholder")}
style={{
marginLeft: showLineNumbers ? 28 : 0, // gutter line offset
}}
>
{code}
</pre>
)}
<pre
className={c("pre-placeholder")}
style={{
marginLeft: showLineNumbers ? 28 : 0, // gutter line offset
}}
>
{code}
</pre>

<>
<p
Expand Down

0 comments on commit 12583ba

Please sign in to comment.