Something went wrong. Try again.
A local-first note taking app
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import * as Automerge from '@automerge/automerge';
import type { FileDocument } from '../types';
/** * Splice text into an Automerge document at a given path. * Wraps Automerge.splice() for the vault document shape. * * Only works when the target field is an Automerge text object. * For plain strings, use updateTextContent instead. */export function spliceText<T extends FileDocument>( doc: Automerge.Doc<T>, path: readonly string[], index: number, deleteCount: number, insert: string,): Automerge.Doc<T> { return Automerge.change(doc, (d) => { Automerge.splice(d, path as any, index, deleteCount, insert); });}
/** * Update the `content` field of a FileDocument to match `newContent`. * * For regular strings: assigns directly (plain strings in Automerge are * replaced wholesale; this is the correct path for the PushworkFileDocument * schema which stores content as `string | Uint8Array`). * * For legacy immutable strings (RawString / ImmutableString): assigns directly. * For Automerge text objects: uses Automerge.updateText for efficient * character-level diffing. */export function updateTextContent<T extends FileDocument>( doc: Automerge.Doc<T>, newContent: string,): Automerge.Doc<T> { const current = readDocContent(doc); if (current === newContent) return doc;
return Automerge.change(doc, (d) => { const content = d.content as unknown; if (typeof content === 'string') { // Plain string: direct assignment d.content = newContent as unknown as T['content']; } else if (Automerge.isImmutableString(content)) { // ImmutableString / RawString: direct assignment d.content = newContent as unknown as T['content']; } else { // Assume it's an Automerge text object Automerge.updateText(d, ['content'] as any, newContent); } });}
/** * Read the `content` field from a FileDocument, normalizing RawString to plain string. */export function readDocContent<T extends FileDocument>(doc: Automerge.Doc<T>): string { const content = doc.content as unknown; if (typeof content === 'string') return content; if (content instanceof Uint8Array) return ''; // RawString / ImmutableString fallback if (Automerge.isImmutableString(content)) { return content.val; } return '';}