Break ground on document "persistence"
This commit is contained in:
@@ -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
@@ -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