feat(app): application on the canonical sheet — report v3 store with v2 draft migration, bundled SheetProvider (SHEET_BUNDLE), annotation layer rendered from the document, zone interaction service, pickers and cards on the sheet index, PDF v3 export with v2 archive import; legacy registry, matchers, editors and assets removed

This commit is contained in:
bchanot committed 2026-10-10 11:36:14 +02:00
1 parent 0a17e390cf
commit ce2ec26112
114 files changed
+3560 -14637

No files matched your search

+113
View File
@@ -0,0 +1,113 @@
import type {
Annotation,
FindingCategory,
FindingNode,
FindingZone,
LocalizedText,
SheetView,
VertebralRegion,
} from '@faunosteo/domain'
import type { Locale } from '@/i18n/types'
import type { LoadedSheet } from '@/sheets/provider'
/**
* Read-only lookups over a loaded sheet. Everything the editor used to derive
* from SVG attributes, label matching or the species manifest comes from the
* document here, by stable id: no label drives logic.
*/
export type ObservableAnnotation = Extract<
Annotation,
{ kind: 'point' | 'zone' }
>
export interface SheetIndex {
sheet: LoadedSheet
mainView: SheetView
view(id: string): SheetView | undefined
annotation(id: string): Annotation | undefined
annotationsOfView(viewId: string): Annotation[]
findingZone(id: string): FindingZone | undefined
/** The category or node at a path, undefined when absent from the sheet. */
nodeAt(
zoneId: string,
path: readonly string[],
): FindingCategory | FindingNode | undefined
regions(): VertebralRegion[]
region(code: string): VertebralRegion | undefined
/** The annotation that opens the vertebra picker for a region on a view. */
spineAnnotation(
regionCode: string,
viewId: string,
): ObservableAnnotation | undefined
/** Whether an annotation opens the vertebra picker. */
isSpine(annotationId: string): boolean
name(annotation: Annotation): LocalizedText
}
export function pickLabel(
text: LocalizedText | null | undefined,
locale: Locale,
): string {
if (!text) return ''
return locale === 'en' ? text.en || text.fr : text.fr || text.en
}
export function isObservable(
a: Annotation | undefined,
): a is ObservableAnnotation {
return a !== undefined && a.kind !== 'detail_trigger'
}
export function buildSheetIndex(sheet: LoadedSheet): SheetIndex {
const { document } = sheet
const views = new Map(document.views.map((v) => [v.id, v]))
const annotations = new Map(document.annotations.map((a) => [a.id, a]))
const zones = new Map(document.finding_zones.map((z) => [z.id, z]))
const regions = new Map(
document.vertebral_config.regions.map((r) => [r.code, r]),
)
const mainView = document.views.find((v) => v.kind === 'main')
if (!mainView) throw new Error('sheet has no main view')
const nodeAt: SheetIndex['nodeAt'] = (zoneId, path) => {
const zone = zones.get(zoneId)
if (!zone || path.length === 0) return undefined
let node: FindingCategory | FindingNode | undefined = zone.categories.find(
(c) => c.id === path[0],
)
for (const segment of path.slice(1)) {
if (!node) return undefined
node = node.children.find((n) => n.id === segment)
}
return node
}
return {
sheet,
mainView,
view: (id) => views.get(id),
annotation: (id) => annotations.get(id),
annotationsOfView: (viewId) =>
document.annotations.filter((a) => a.view_id === viewId),
findingZone: (id) => zones.get(id),
nodeAt,
regions: () => document.vertebral_config.regions,
region: (code) => regions.get(code),
spineAnnotation: (code, viewId) =>
document.annotations.find(
(a): a is ObservableAnnotation =>
isObservable(a) &&
a.view_id === viewId &&
a.vertebral_region_code === code,
),
isSpine: (id) => {
const a = annotations.get(id)
return isObservable(a) && a.vertebral_region_code !== null
},
name: (a) =>
a.kind === 'detail_trigger'
? (a.name ?? views.get(a.opens_view_id)?.name ?? { fr: '', en: '' })
: a.name,
}
}
+213
View File
@@ -0,0 +1,213 @@
import type {
Annotation,
LateralSide,
Observation,
ReportZone,
Vertebra,
} from '@faunosteo/domain'
import { isObservable, type SheetIndex } from './sheetIndex'
/**
* Zone interaction rules of the report editor, pure: what a click on an
* annotation means for the active observation, and the observation
* reducers the route applies. The rules are the legacy ones (click chain
* of EditorRoute, BDR-019 vertebra sync), keyed by annotation ids.
*/
export interface ZoneRef {
annotationId: string
viewId: string
}
export type ZoneClickDecision =
| { kind: 'ignore' }
| { kind: 'open_view'; viewId: string }
| { kind: 'activate_fallback' }
| { kind: 'open_vertebra_picker' }
| { kind: 'open_laterality_picker' }
| { kind: 'remove_both_sides' }
| { kind: 'toggle' }
export function sameZone(zone: ReportZone, ref: ZoneRef): boolean {
return zone.annotationId === ref.annotationId && zone.viewId === ref.viewId
}
export function refOf(annotation: Annotation): ZoneRef {
return { annotationId: annotation.id, viewId: annotation.view_id }
}
export function sidesPresent(obs: Observation, ref: ZoneRef): LateralSide[] {
return obs.zones
.filter((z) => sameZone(z, ref) && z.side)
.map((z) => z.side as LateralSide)
}
/**
* Legacy click chain: detail trigger opens its view and is never added; no
* active observation → the click only activates one; spine zones always open
* the picker; bilateral zones go through the side picker, or clear both sides
* when both are set; anything else toggles.
*/
export function decideZoneClick(
annotation: Annotation | undefined,
active: Observation | undefined,
): ZoneClickDecision {
if (!annotation) return { kind: 'ignore' }
if (annotation.kind === 'detail_trigger') {
return { kind: 'open_view', viewId: annotation.opens_view_id }
}
if (!active) return { kind: 'activate_fallback' }
if (annotation.vertebral_region_code !== null) {
return { kind: 'open_vertebra_picker' }
}
if (annotation.laterality === 'bilateral') {
const sides = sidesPresent(active, refOf(annotation))
return sides.includes('left') && sides.includes('right')
? { kind: 'remove_both_sides' }
: { kind: 'open_laterality_picker' }
}
return { kind: 'toggle' }
}
function newZone(annotation: Annotation, side?: LateralSide): ReportZone {
const zone: ReportZone = refOf(annotation)
if (isObservable(annotation)) {
zone.labelFrSnapshot = annotation.name.fr
zone.labelEnSnapshot = annotation.name.en
}
if (side) zone.side = side
return zone
}
function withZones(obs: Observation, zones: ReportZone[]): Observation {
return { ...obs, zones }
}
/** Plain zone: added when absent, removed when present (no side). */
export function toggleZone(
obs: Observation,
annotation: Annotation,
): Observation {
const index = obs.zones.findIndex((z) => sameZone(z, refOf(annotation)))
if (index >= 0)
return withZones(
obs,
obs.zones.filter((_, i) => i !== index),
)
return withZones(obs, [...obs.zones, newZone(annotation)])
}
/** The zone exists after the call; returns its index. */
export function ensureZone(
obs: Observation,
annotation: Annotation,
): { observation: Observation; index: number } {
const index = obs.zones.findIndex((z) => sameZone(z, refOf(annotation)))
if (index >= 0) return { observation: obs, index }
const zones = [...obs.zones, newZone(annotation)]
return { observation: withZones(obs, zones), index: zones.length - 1 }
}
/** Appends the sides not present yet; never removes one (legacy semantics). */
export function addSides(
obs: Observation,
annotation: Annotation,
sides: readonly LateralSide[],
): Observation {
const present = sidesPresent(obs, refOf(annotation))
const added = sides
.filter((side) => !present.includes(side))
.map((side) => newZone(annotation, side))
return added.length === 0 ? obs : withZones(obs, [...obs.zones, ...added])
}
export function removeAllSides(obs: Observation, ref: ZoneRef): Observation {
return withZones(
obs,
obs.zones.filter((z) => !sameZone(z, ref)),
)
}
export function removeZoneAt(obs: Observation, index: number): Observation {
return withZones(
obs,
obs.zones.filter((_, i) => i !== index),
)
}
function isSpineZone(zone: ReportZone, index: SheetIndex): boolean {
return index.isSpine(zone.annotationId)
}
/**
* Vertebrae of every spine zone of the observation, the zone at `priority`
* first (the one the picker was opened from), without duplicates.
*/
export function collectVertebrae(
obs: Observation,
index: SheetIndex,
priority: number,
): Vertebra[] {
const ordered = [
...obs.zones.filter((_, i) => i === priority),
...obs.zones.filter((_, i) => i !== priority),
]
const seen = new Set<string>()
const out: Vertebra[] = []
for (const zone of ordered) {
if (!isSpineZone(zone, index)) continue
for (const v of zone.vertebrae ?? []) {
const key = `${v.region}${v.index}`
if (seen.has(key)) continue
seen.add(key)
out.push({ ...v })
}
}
return out
}
function regionOf(zone: ReportZone, index: SheetIndex): string | null {
const a = index.annotation(zone.annotationId)
return isObservable(a) ? a.vertebral_region_code : null
}
/**
* BDR-019: the picker edits every region at once. Per region the selection
* REPLACES the zone's vertebrae; a spine zone left without vertebrae is
* pruned; a region picked without a zone gets one on the view the picker
* was opened from (or the main view).
*/
export function syncVertebrae(
obs: Observation,
selection: readonly Vertebra[],
index: SheetIndex,
viewId: string,
): Observation {
const byRegion = new Map<string, Vertebra[]>()
for (const v of selection) {
const list = byRegion.get(v.region) ?? []
list.push({ ...v })
byRegion.set(v.region, list)
}
const kept: ReportZone[] = []
for (const zone of obs.zones) {
const region = isSpineZone(zone, index) ? regionOf(zone, index) : null
if (region === null) {
kept.push(zone)
continue
}
const picked = byRegion.get(region)
byRegion.delete(region)
if (!picked || picked.length === 0) continue
kept.push({ ...zone, vertebrae: picked })
}
for (const [region, picked] of byRegion) {
if (picked.length === 0) continue
const annotation =
index.spineAnnotation(region, viewId) ??
index.spineAnnotation(region, index.mainView.id)
if (!annotation) continue
kept.push({ ...newZone(annotation), vertebrae: picked })
}
return withZones(obs, kept)
}