Break ground on document "persistence"

This commit is contained in:
2025-08-22 22:18:59 -07:00
parent e34898a1a6
commit f3d4cd72ac
2 changed files with 23 additions and 2 deletions
+8 -2
View File
@@ -8,17 +8,20 @@ import {
} from '@codemirror/language'; } from '@codemirror/language';
import { yCollab } from 'y-codemirror.next'; import { yCollab } from 'y-codemirror.next';
import { onCleanup, onMount } from 'solid-js'; import { onCleanup, onMount } from 'solid-js';
import { syncDoc } from '../sync';
interface EditorProps { interface EditorProps {
file: string; file: string;
} }
function Editor(props: EditorProps) { function Editor(props: EditorProps) {
let doc: Y.Doc;
let editorDiv; let editorDiv;
let editorView: EditorView; let editorView: EditorView;
onMount(() => { onMount(() => {
const doc = new Y.Doc(); doc = new Y.Doc();
syncDoc(props.file, doc);
const state = EditorState.create({ const state = EditorState.create({
doc: doc.getText().toString(), doc: doc.getText().toString(),
extensions: [ extensions: [
@@ -32,7 +35,10 @@ function Editor(props: EditorProps) {
editorView = new EditorView({ state, parent: editorDiv }); editorView = new EditorView({ state, parent: editorDiv });
}); });
onCleanup(() => editorView?.destroy()); onCleanup(() => {
editorView?.destroy();
doc?.destroy();
});
return ( return (
<div> <div>
+15
View File
@@ -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);
});
}