// The note document: a string in an Automerge document, edited by splicing. // // **This is the part other clients should copy.** An Automerge document holds one // field, `text`, and every edit — a keystroke, a whole file rewritten in another // editor — becomes a `splice` against whatever the document holds at that moment. // Assigning `doc.text = next` would be simpler and would throw away the merge: // two clients that both assign end up with whichever arrived last, which is the // problem the CRDT exists to solve. // // The rule, in one line: **never replace the string; hand the new value to // `Automerge.updateText` and let it compute the minimal edit.** The library diffs // against what the document holds now, which is why the editor has to keep its // own view in step — see `applyRemote` in `main.ts`. import * as Automerge from "@automerge/automerge"; import type { DocHandle } from "@automerge/automerge-repo"; /** The document is one string. An index signature keeps Automerge's generics happy. */ export interface NoteDoc { text: string; [key: string]: unknown; } export type Handle = DocHandle; export function read(handle: Handle): string { return handle.doc()?.text ?? ""; } export function heads(handle: Handle): string[] { return Automerge.getHeads(handle.doc() as Automerge.Doc) as unknown as string[]; } /** Create a document with `seed` as its text. */ export function seed(handle: Handle, text: string): void { handle.change((doc) => { doc.text = text; }); } /** * Replace the document's text with `next`, as a splice of the difference. * * The diff is taken against what the document holds *now*, not against any * remembered copy, so it composes with whatever arrived from a peer a moment * ago: applying a keystroke onto a document someone else just edited merges * instead of clobbering. */ export function write(handle: Handle, next: string): void { if (read(handle) === next) return; // `updateText` is Automerge's own: it diffs the document's current string // against `next` and applies a minimal edit script. That is the same job our // hand-rolled fast-diff splice did, with the index arithmetic inside the // library instead of here — and it is what Ink & Switch's Patchwork uses for // exactly this. handle.change((doc) => { Automerge.updateText(doc as unknown as Automerge.Doc, ["text"], next); }); } /** * Apply a file that was edited outside this client. * * Only when the document still holds exactly what the file held when we last * wrote it (`base`). If the document has moved on, the two edits are concurrent * and this function refuses rather than guessing — the caller shows a conflict. * That refusal is the whole point: it is the difference between merging and * overwriting someone else's work. */ export function applyExternal(handle: Handle, base: string, next: string): boolean { if (read(handle) !== base) return false; write(handle, next); return true; } /** A plain document value, for callers that need one without a handle. */ export function plain(text: string): Automerge.Doc { return Automerge.from({ text }); }