Break ground on document "persistence"
This commit is contained in:
@@ -8,17 +8,20 @@ import {
|
||||
} from '@codemirror/language';
|
||||
import { yCollab } from 'y-codemirror.next';
|
||||
import { onCleanup, onMount } from 'solid-js';
|
||||
import { syncDoc } from '../sync';
|
||||
|
||||
interface EditorProps {
|
||||
file: string;
|
||||
}
|
||||
|
||||
function Editor(props: EditorProps) {
|
||||
let doc: Y.Doc;
|
||||
let editorDiv;
|
||||
let editorView: EditorView;
|
||||
|
||||
onMount(() => {
|
||||
const doc = new Y.Doc();
|
||||
doc = new Y.Doc();
|
||||
syncDoc(props.file, doc);
|
||||
const state = EditorState.create({
|
||||
doc: doc.getText().toString(),
|
||||
extensions: [
|
||||
@@ -32,7 +35,10 @@ function Editor(props: EditorProps) {
|
||||
editorView = new EditorView({ state, parent: editorDiv });
|
||||
});
|
||||
|
||||
onCleanup(() => editorView?.destroy());
|
||||
onCleanup(() => {
|
||||
editorView?.destroy();
|
||||
doc?.destroy();
|
||||
});
|
||||
|
||||
return (
|
||||
<div>
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import * as Y from 'yjs';
|
||||
|
||||
const fakeStore = new Map<string, Uint8Array[]>();
|
||||
|
||||
export function syncDoc(id: string, doc: Y.Doc) {
|
||||
const updates = fakeStore.get(id) ?? [];
|
||||
fakeStore.set(id, updates);
|
||||
for (const update of updates) {
|
||||
Y.applyUpdate(doc, update);
|
||||
}
|
||||
|
||||
doc.on('update', (update: Uint8Array) => {
|
||||
updates.push(update);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user