/** * The view: what the user sees, as one pure function of the records and the * session. * * records.get() what the engine confirmed * records.view() that, with this session's pending changes on top (the * mirror replays them: `predictRecords`) * → AnnotationView, each record drawn as the change on it says * AnnotationView + session → Model * * Every read and every gesture takes this `Model`. It is memoized, and each * record keeps its identity until its own inputs change, so a hover or a * change to one record does not recompute the others. * * A lender's records (services/lending.ts) come after the annotations, so on * each page they paint above them, as form widgets do. */ import { memo, type Mirror } from '@embedpdf/core'; import { type AnnotationView, drawnAfter, drawsLive, fromDTO, type Id, type Model, type ModelAnnotation, sourceOfConfirmed, sourceOfNew, } from '@embedpdf/core-annotation'; import { annotationKey, annotationPatchBetween, type Annotation, type AnnotationPatch, } from '@embedpdf/engine-core/runtime'; import type { AnnotationLender } from '../host-contract'; import type { AnnotationContext } from '../services/context'; import type { Lending } from '../services/lending'; import type { AnnotationRecord, AnnotationRecords } from '../sync/records'; /** * This session's authority over a record (permissions.md): the same collab * mirrors the engine enforces with, asked about the record's stamped owner. * No security context (bare local engines, tests): unstamped, so allowed. */ export const authorityOf = ( ctx: Pick, dto: Annotation, ): ModelAnnotation['authority'] => { const security = ctx.doc?.security; if (!security) return undefined; const target = { ...(dto.userId != null ? { userId: dto.userId } : {}), ...(dto.groupId != null ? { groupId: dto.groupId } : {}), }; return { update: security.allowsAnnotation('update', target), delete: security.allowsAnnotation('delete', target), }; }; export function createView( ctx: Pick, records: Mirror, lending: Pick, ) { /** A confirmed record as the model has it, cached per stored version and render preference. */ const confirmed = new WeakMap(); const confirmedRecord = (stored: AnnotationRecord, vector: boolean): ModelAnnotation => { const cached = confirmed.get(stored); if (cached && cached.vector === vector) return cached.record; const projected = fromDTO(stored.dto); const record: ModelAnnotation = { ...projected, source: sourceOfConfirmed(projected.annotation, vector), pictureVersion: stored.pictureVersion, authority: authorityOf(ctx, stored.dto), }; confirmed.set(stored, { vector, record }); return record; }; /** * A record a pending change made or changed, cached per shown version, the * confirmed one it was made from and the render preference. */ const predicted = new WeakMap< AnnotationRecord, { truth: AnnotationRecord | undefined; vector: boolean; record: ModelAnnotation } >(); /** * How a record shows while a pending change is on it: * - one this session creates: drawn as a new record is, with no raster * from the engine yet; * - a confirmed one: its predicted annotation, drawn as the change says * (core `drawnAfter`, the engine's verdict on it): its raster moved, or * live, as with no change pending. Its appearance version and * authority stay the engine's. */ const predictedRecord = ( shown: AnnotationRecord, truth: AnnotationRecord | undefined, vector: boolean, ): ModelAnnotation => { const cached = predicted.get(shown); if (cached && cached.truth === truth && cached.vector === vector) return cached.record; let record: ModelAnnotation; if (!truth) { record = { ...fromDTO(shown.dto), unconfirmed: true, source: sourceOfNew(shown.dto) }; } else { const base = confirmedRecord(truth, vector); const patch = { ...annotationPatchBetween(truth.dto, shown.dto), subtype: truth.dto.subtype, } as AnnotationPatch; record = { ...base, annotation: shown.dto, ...drawnAfter(base, patch) }; } predicted.set(shown, { truth, vector, record }); return record; }; /** The lender's live drawing of a record it shows, when it draws one. */ const painterOf = ( shown: Annotation, lender: AnnotationLender, ): Pick => { const painter = lender.painter(shown); return painter ? { painter } : {}; }; /** A lent record as the model has it, cached per shown row and the confirmed row it was made from. */ const lent = new WeakMap< Annotation, { truth: Annotation | undefined; record: ModelAnnotation } >(); const lentRecord = ( shown: Annotation, truth: Annotation | undefined, lender: AnnotationLender, ): ModelAnnotation => { const cached = lent.get(shown); if (cached && cached.truth === truth) return cached.record; let record: ModelAnnotation; if (!truth) { // Only shown: one an undo brings back draws the picture it had (the // render reads keep it); a new one (a field just placed) draws live, as // the tool's ghost drew it, until the engine has its picture. record = { ...fromDTO(shown), unconfirmed: true, source: confirmedOnce.has(annotationKey(shown.ref)) ? 'baked' : 'vector', pictureVersion: lender.getPictureVersion(shown.ref), authority: lender.getAuthority(shown), ...painterOf(shown, lender), }; } else { // The lender's picture is drawn where its shape is; its painter draws it live while a // gesture or a change reshapes or restyles it, as a square is drawn. const base: ModelAnnotation = { ...fromDTO(truth), source: sourceOfConfirmed(truth, false), pictureVersion: lender.getPictureVersion(truth.ref), authority: lender.getAuthority(truth), ...painterOf(shown, lender), }; const patch = { ...annotationPatchBetween(truth, shown), subtype: truth.subtype }; record = shown === truth ? base : { ...base, annotation: shown, ...drawnAfter(base, patch as AnnotationPatch) }; // A new look the lender made itself (a setting, a value): drawn live until its picture is // there, by a record with a live drawing; one without keeps its picture. if (lender.isPending(shown.ref) && drawsLive(record)) record = { ...record, source: 'vector' }; } lent.set(shown, { truth, record }); return record; }; /** The confirmed lent rows by key, once per list; and every key ever confirmed. */ const confirmedLent = new WeakMap>(); const confirmedOnce = new Set(); const confirmedByKey = (rows: readonly Annotation[]): Map => { let byKey = confirmedLent.get(rows); if (!byKey) { byKey = new Map(rows.map((row) => [annotationKey(row.ref), row])); for (const key of byKey.keys()) confirmedOnce.add(key); confirmedLent.set(rows, byKey); } return byKey; }; const view = memo( () => [ records.get(), records.view(), ctx.state.get().vector, ctx.document(), lending.records(), ] as const, (truths, shown, vector, _document, lentRecords): AnnotationView => { const byId: Record = {}; for (const key of shown.order) { const stored = shown.byKey[key]!; const truth = truths.byKey[key]; byId[key] = stored === truth ? confirmedRecord(stored, key in vector) : predictedRecord(stored, truth, key in vector); } // The document's order, then the records this session created, then the lent ones. const order = [...shown.order]; const lender = lending.lender(); if (lentRecords && lender) { const confirmed = confirmedByKey(lentRecords.confirmed); for (const row of lentRecords.shown) { const key = annotationKey(row.ref); if (key in byId) continue; byId[key] = lentRecord(row, confirmed.get(key), lender); order.push(key); } } return { byId, order }; }, ); const model = memo( () => [ctx.state.get().session, view()] as const, (session, current): Model => ({ ...session, ...current }), ); /** Each page's records in paint order, grouped once per model. */ const recordsByPage = memo( () => [model()] as const, (whole) => { const pages = new Map(); for (const id of whole.order) { const record = whole.byId[id]!; const list = pages.get(record.annotation.page.objectNumber); if (list) list.push(record); else pages.set(record.annotation.page.objectNumber, [record]); } return pages; }, ); const pageSlices = new Map(); /** * One page's slice of the model: its records, and the selection, hover, * text editing, gesture and markup preview that concern it. It keeps its * identity while nothing on the page changed, so a page's reads recompute * only for changes on that page. */ const pageModel = (pageObjectNumber: number): Model => { const whole = model(); const cached = pageSlices.get(pageObjectNumber); if (cached?.whole === whole) return cached.model; const records = recordsByPage().get(pageObjectNumber) ?? NO_RECORDS; const onPage = (id: Id) => whole.byId[id]?.annotation.page.objectNumber === pageObjectNumber; const draft = whole.draft; const parts: PageParts = { records, selected: whole.selected.filter(onPage), hovered: whole.hovered !== null && onPage(whole.hovered) ? whole.hovered : null, editing: whole.editing !== null && onPage(whole.editing) ? whole.editing : null, draft: draft && ('page' in draft ? draft.page.objectNumber === pageObjectNumber : 'ids' in draft ? draft.ids.some(onPage) : onPage(draft.id)) ? draft : null, preview: whole.preview?.byPage[pageObjectNumber] ? whole.preview : null, settings: [whole.defaults, whole.hitMargin, whole.snap, whole.editable], }; if (cached && samePageParts(cached.parts, parts)) { pageSlices.set(pageObjectNumber, { whole, parts: cached.parts, model: cached.model }); return cached.model; } const slice: Model = { ...whole, byId: Object.fromEntries(records.map((record) => [record.id, record])), order: records.map((record) => record.id), selected: [...parts.selected], hovered: parts.hovered, editing: parts.editing, draft: parts.draft, preview: parts.preview, }; pageSlices.set(pageObjectNumber, { whole, parts, model: slice }); return slice; }; return { view, model, pageModel }; } const NO_RECORDS: readonly ModelAnnotation[] = []; /** What one page's slice of the model is made of; compared item by item. */ interface PageParts { readonly records: readonly ModelAnnotation[]; readonly selected: readonly Id[]; readonly hovered: Id | null; readonly editing: Id | null; readonly draft: Model['draft']; readonly preview: Model['preview']; readonly settings: readonly unknown[]; } const sameItems = (left: readonly unknown[], right: readonly unknown[]): boolean => left.length === right.length && left.every((item, index) => Object.is(item, right[index])); const samePageParts = (left: PageParts, right: PageParts): boolean => sameItems(left.records, right.records) && sameItems(left.selected, right.selected) && left.hovered === right.hovered && left.editing === right.editing && left.draft === right.draft && left.preview === right.preview && sameItems(left.settings, right.settings); export type View = ReturnType;