Something went wrong. Try again.
Synapse is a study workspace. Organize courses read and annotate the material, train with generated exercises and watch your progress forked from denishovart.com/synapse
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879// Loro container helpers shared by the repos (through workspace.ts) and the// bootstrap import. Entities are child LoroMaps inside per-kind registries;// ordering lives in MovableLists.
import { LoroMap, LoroMovableList } from 'loro-crdt';import type { LoroDoc } from 'loro-crdt';import { previewingNow } from './previewGuard';
export type Registry = 'courses' | 'topics' | 'documents' | 'links' | 'layout' | 'exercises' | 'lexicon';
export type Fields = Record<string, unknown>;
export function childMap(parent: LoroMap, key: string): LoroMap | null { const value = parent.get(key); return value instanceof LoroMap ? value : null;}
// Read paths call these to reach a child container that repos normally create on// demand. While previewing, the doc is checked out (detached) and setContainer// throws, so return a fresh unattached container instead: it reads as empty and// leaves the detached doc untouched.export function orCreateChildMap(parent: LoroMap, key: string): LoroMap { const existing = childMap(parent, key); if (existing) return existing; return previewingNow() ? new LoroMap() : parent.setContainer(key, new LoroMap());}
export function childList(parent: LoroMap, key: string): LoroMovableList | null { const value = parent.get(key); return value instanceof LoroMovableList ? value : null;}
export function orCreateChildList(parent: LoroMap, key: string): LoroMovableList { const existing = childList(parent, key); if (existing) return existing; return previewingNow() ? new LoroMovableList() : parent.setContainer(key, new LoroMovableList());}
export function getEntity(doc: LoroDoc, registry: Registry, id: string): Fields | null { const entry = childMap(doc.getMap(registry), id); return entry ? (entry.toJSON() as Fields) : null;}
// Create-or-patch: only the given keys are written, so concurrent edits to// other fields of the same entity merge instead of clobbering.export function setEntity(doc: LoroDoc, registry: Registry, id: string, fields: Fields): void { const entry = orCreateChildMap(doc.getMap(registry), id); for (const [key, value] of Object.entries(fields)) entry.set(key, value);}
export function deleteEntity(doc: LoroDoc, registry: Registry, id: string): void { doc.getMap(registry).delete(id);}
export function allEntities(doc: LoroDoc, registry: Registry): Map<string, Fields> { const map = doc.getMap(registry); const out = new Map<string, Fields>(); for (const key of map.keys()) { const entry = childMap(map, key); if (entry) out.set(key, entry.toJSON() as Fields); } return out;}
export function listRemove(list: LoroMovableList, value: string): void { const idx = list.toArray().indexOf(value); if (idx >= 0) list.delete(idx, 1);}
// Replace a list's content wholesale (the layout PUT semantics). Skipped when// already equal so no-op saves don't spam the history.export function listReplace(list: LoroMovableList, values: string[]): void { const current = list.toArray() as string[]; if (current.length === values.length && current.every((v, i) => v === values[i])) return; if (current.length) list.delete(0, current.length); for (const v of values) list.push(v);}