Skip to content

Commit

Permalink
[#53250] Assert collaboration is ready before mounting CodeMirror
Browse files Browse the repository at this point in the history
If collaboration is enabled we need to wait until WebSocket connection is made. Only then we can initialize the CodeMirror with the collaborative state. Mounting CodeMirror earlier results in an invalid state (empty editor and rendered document)
  • Loading branch information
MaciejWas committed Apr 18, 2024
1 parent eb9e3e3 commit 0b7d35e
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/components/CodeMirror.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useRef, useState } from 'preact/hooks';
import { html } from "htm/preact";
import { EditorView } from "codemirror";
import { EditorState } from "@codemirror/state";
import { EditorState, StateEffect } from "@codemirror/state";
import styled from 'styled-components';
import useCollaboration from '../hooks/useCollaboration';
import { adjustToMode } from './Preview';
Expand Down Expand Up @@ -130,6 +130,9 @@ const CodeMirror = ({ text, id, name, className, mode, collaboration, spellcheck
const ycomments = useComments(ydoc, provider, getAvatar);

useEffect(() => {
if (collaboration.enabled && !ready) return;
if (editorRef.current) return;

const startState = EditorState.create({
doc: collaboration.enabled ? ytext.toString() : text.get(),
extensions: ExtensionBuilder.basicSetup()
Expand Down Expand Up @@ -158,7 +161,7 @@ const CodeMirror = ({ text, id, name, className, mode, collaboration, spellcheck
}
view.destroy();
};
}, []);
}, [ready]);

useEffect(() => {
const mystEditorCount = document.querySelectorAll("#myst-css-namespace").length;
Expand Down

0 comments on commit 0b7d35e

Please sign in to comment.