|
|
@@ -2,6 +2,11 @@ import { useEffect } from 'react';
|
|
|
|
|
|
import type { Extension } from '@codemirror/state';
|
|
|
import { keymap, scrollPastEnd } from '@codemirror/view';
|
|
|
+// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
+// @ts-ignore
|
|
|
+import { yCollab } from 'y-codemirror.next';
|
|
|
+import { SocketIOProvider } from 'y-socket.io';
|
|
|
+import * as Y from 'yjs';
|
|
|
|
|
|
import { GlobalCodeMirrorEditorKey } from '../consts';
|
|
|
import { useCodeMirrorEditorIsolated } from '../stores';
|
|
|
@@ -18,11 +23,13 @@ type Props = {
|
|
|
onChange?: (value: string) => void,
|
|
|
onSave?: () => void,
|
|
|
indentSize?: number,
|
|
|
+ ydoc?: Y.Doc,
|
|
|
+ provider?: SocketIOProvider,
|
|
|
}
|
|
|
|
|
|
export const CodeMirrorEditorMain = (props: Props): JSX.Element => {
|
|
|
const {
|
|
|
- onSave, onChange, indentSize,
|
|
|
+ onSave, onChange, indentSize, ydoc, provider,
|
|
|
} = props;
|
|
|
|
|
|
const { data: codeMirrorEditor } = useCodeMirrorEditorIsolated(GlobalCodeMirrorEditorKey.MAIN);
|
|
|
@@ -57,6 +64,22 @@ export const CodeMirrorEditorMain = (props: Props): JSX.Element => {
|
|
|
return cleanupFunction;
|
|
|
}, [codeMirrorEditor, onSave]);
|
|
|
|
|
|
+ // setup yCollab
|
|
|
+ useEffect(() => {
|
|
|
+ if (ydoc == null || provider == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const ytext = ydoc.getText('codemirror');
|
|
|
+ const undoManager = new Y.UndoManager(ytext);
|
|
|
+ codeMirrorEditor?.initDoc(ytext.toString());
|
|
|
+ const cleanup = codeMirrorEditor?.appendExtensions?.([
|
|
|
+ yCollab(ytext, provider.awareness, { undoManager }),
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return cleanup;
|
|
|
+ }, [codeMirrorEditor, provider, ydoc]);
|
|
|
+
|
|
|
return (
|
|
|
<CodeMirrorEditor
|
|
|
editorKey={GlobalCodeMirrorEditorKey.MAIN}
|