Files
faunosteo/apps/app/src/services/sheetIndex.ts
T

119 lines
3.6 KiB
TypeScript

import type {
Annotation,
AnatomicalSheetDocument,
FindingZone,
LocalizedText,
ObservableAnnotation,
SheetIndexData,
SheetRef,
SheetStatus,
SheetView,
VertebralRegion,
} from '@faunosteo/core-wasm/types'
import type { CoreOutcome } from '@faunosteo/core-wasm'
import type { SheetOps } from '@/core/sheetOps'
/**
* The loaded sheet as React reads it (R2). Every lookup table is computed once by the
* core (`SheetIndexData`): positions by id with the TypeScript `Map` semantics (last
* duplicate wins), names, spine ids and the finding paths that exist. This class only reads
* them, so no render calls the core; the sheet-bound rules go through `ops`.
*/
export type { ObservableAnnotation }
/** What the screens read about the loaded sheet. */
export interface LoadedSheetView {
status: SheetStatus
ref: SheetRef
speciesId: string
schemaVersion: number
/** The document as the core parsed it (defaults filled in). */
document: AnatomicalSheetDocument
}
function at<T>(
list: readonly T[],
positions: Readonly<Record<string, number>>,
id: string,
): T | undefined {
return Object.hasOwn(positions, id) ? list[positions[id]!] : undefined
}
export function isObservable(
a: Annotation | undefined,
): a is ObservableAnnotation {
return a !== undefined && a.kind !== 'detail_trigger'
}
export class SheetIndex {
readonly sheet: LoadedSheetView
/** The first `main` view of the document. */
readonly mainViewId: string
private readonly spine: ReadonlySet<string>
private readonly nodes: ReadonlySet<string>
/** The composed canvas of each view: constant for a loaded sheet, composed once (R3). */
private readonly canvases = new Map<string, string>()
constructor(
private readonly data: SheetIndexData,
readonly ops: SheetOps,
) {
const { document } = data
this.sheet = {
status: data.status,
ref: data.ref,
speciesId: data.speciesId,
schemaVersion: data.schemaVersion,
document,
}
this.mainViewId = data.mainViewId
this.spine = new Set(data.spineIds)
this.nodes = new Set(data.nodeKeys)
}
/** The SVG text of a view's canvas (sanitised drawing + annotation layer), from the core. */
canvasSvg(viewId: string): CoreOutcome<string> {
const known = this.canvases.get(viewId)
if (known !== undefined) return { ok: true, value: known }
const outcome = this.ops.canvasView(viewId)
if (!outcome.ok) return outcome
this.canvases.set(viewId, outcome.value.svg)
return { ok: true, value: outcome.value.svg }
}
view(id: string): SheetView | undefined {
return at(this.data.document.views, this.data.viewById, id)
}
annotation(id: string): Annotation | undefined {
return at(this.data.document.annotations, this.data.annotationById, id)
}
findingZone(id: string): FindingZone | undefined {
return at(this.data.document.finding_zones, this.data.findingZoneById, id)
}
regions(): VertebralRegion[] {
return this.data.document.vertebral_config.regions
}
/** Whether an annotation opens the vertebra picker. */
isSpine(annotationId: string): boolean {
return this.spine.has(annotationId)
}
/** The name the sheet gives an annotation (a trigger: its own or its view's). */
name(annotation: Annotation): LocalizedText {
const names = this.data.names
return Object.hasOwn(names, annotation.id)
? names[annotation.id]!
: { fr: '', en: '' }
}
/** Whether the finding zone holds this category or node path. */
hasNode(zoneId: string, path: readonly string[]): boolean {
return this.nodes.has(JSON.stringify([zoneId, ...path]))
}
}