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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100// Annotations live in one Loro doc per document (`ann:<documentId>`), keyed// by the stable client uid, which is also the row id everywhere (local doc,// DTO, REST mirror). page/type/color are separate LWW keys so a recolor on// one device merges with a move on another; the drawn payload stays one// atomic value.
import type { ResultAsync } from 'neverthrow';import type { Annotation, AnnotationType } from '../../types';import { annDocId } from '../loro';import { childMap, orCreateChildMap, type Fields } from '../containers';import { annotationsDoc, commit, DataError, nowIso, run } from '../workspace';import { changeTag } from '../changeTags';
export interface AnnotationSpec { uid: string; type: AnnotationType; data: Record<string, unknown>; color: string; page: number;}
function toAnnotation(documentId: string, uid: string, f: Fields): Annotation { const payload = (f.data as Record<string, unknown>) ?? {}; return { id: uid, document_id: documentId, page: Number(f.page ?? 1), type: (f.type as AnnotationType) ?? 'highlight', data: JSON.stringify({ ...payload, uid }), color: String(f.color ?? '#ffeb3b'), created_at: String(f.created_at ?? ''), };}
export function list(documentId: string): ResultAsync<Annotation[], DataError> { return run(async () => { const doc = await annotationsDoc(documentId); const map = doc.getMap('annotations'); const out: Annotation[] = []; for (const uid of map.keys()) { const entry = childMap(map, uid); const value = entry ? (entry.toJSON() as Fields) : (map.get(uid) as Fields | undefined); if (value && typeof value === 'object') out.push(toAnnotation(documentId, uid, value)); } return out.sort((a, b) => a.created_at.localeCompare(b.created_at)); });}
export function create( documentId: string, spec: AnnotationSpec,): ResultAsync<Annotation, DataError> { return run(async () => { const doc = await annotationsDoc(documentId); const entry = orCreateChildMap(doc.getMap('annotations'), spec.uid); const created_at = nowIso(); entry.set('page', spec.page); entry.set('type', spec.type); entry.set('color', spec.color); entry.set('data', spec.data); entry.set('created_at', created_at); await commit(doc, annDocId(documentId), changeTag.annotation.add(spec.type, spec.page)); return toAnnotation(documentId, spec.uid, { page: spec.page, type: spec.type, color: spec.color, data: spec.data, created_at, }); });}
export function update( documentId: string, uid: string, patch: { data?: Record<string, unknown>; color?: string },): ResultAsync<null, DataError> { return run(async () => { const doc = await annotationsDoc(documentId); const entry = childMap(doc.getMap('annotations'), uid); if (!entry) throw new DataError('Annotation not found', 404); if (patch.data) entry.set('data', patch.data); if (patch.color) entry.set('color', patch.color); await commit(doc, annDocId(documentId), changeTag.annotation.edit); const body: { data?: Record<string, unknown>; color?: string } = {}; if (patch.data) body.data = { ...patch.data, uid }; if (patch.color) body.color = patch.color; return null; });}
export function remove(documentId: string, uid: string): ResultAsync<null, DataError> { return run(async () => { const doc = await annotationsDoc(documentId); doc.getMap('annotations').delete(uid); await commit(doc, annDocId(documentId), changeTag.annotation.remove); return null; });}