From f3d4cd72ac864866be5637e9ba47f3d723547de2 Mon Sep 17 00:00:00 2001 From: Chris Mounce Date: Fri, 22 Aug 2025 22:18:59 -0700 Subject: [PATCH] Break ground on document "persistence" --- src/components/Editor.tsx | 10 ++++++++-- src/sync.ts | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/sync.ts diff --git a/src/components/Editor.tsx b/src/components/Editor.tsx index 66ea248..44e1f7f 100644 --- a/src/components/Editor.tsx +++ b/src/components/Editor.tsx @@ -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 (
diff --git a/src/sync.ts b/src/sync.ts new file mode 100644 index 0000000..0e2ac23 --- /dev/null +++ b/src/sync.ts @@ -0,0 +1,15 @@ +import * as Y from 'yjs'; + +const fakeStore = new Map(); + +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); + }); +}