feat(editor): cross-region vertebra picks spawn their spine zone

Picking vertebrae in a region tab other than the clicked zone now creates
(or additively merges) the matching spine zone, so the SVG highlights it,
it appears in the observation's selected zones, and its findings picker shows.

Extract distributeVertebraeToZones + findSpineZoneId + regionFromLabel into
src/lib/spineZones.ts (regionFromLabel re-exported from VertebraPicker for
back-compat). Target region replaces (deselection sticks); other regions union
(side-tab picks never wipe a pre-existing zone). Unmapped regions fall back to
the target zone.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Bastien ChanotandClaude Opus 4.7 committed 2026-05-27 18:14:01 +02:00
1 parent 8e94196e5a
commit 9544924165
4 files changed
+220 -20

No files matched your search

+122
View File
@@ -0,0 +1,122 @@
import type {
ObservationZone,
Vertebra,
VertebralRegion,
} from '@/domain/schema/observation'
import type { Bone } from '@/anatomy/types'
/**
* Infers the matching vertebral region from a free-form anatomical label
* (zone name in FR or EN). Matches on substring roots, case-insensitive,
* accent-insensitive. Returns undefined when no root matches.
*/
export function regionFromLabel(label: string): VertebralRegion | undefined {
const norm = label
.normalize('NFD')
.replace(/\p{Diacritic}/gu, '')
.toLowerCase()
if (norm.includes('cervic')) return 'C'
if (norm.includes('thorac') || norm.includes('dors')) return 'T'
if (norm.includes('lombair') || norm.includes('lumbar')) return 'L'
if (norm.includes('sacral') || norm.includes('sacre')) return 'S'
if (norm.includes('caudal') || norm.includes('coccyg')) return 'Cd'
return undefined
}
/**
* Finds the spine-zone boneId whose label maps to the given vertebral region.
* Spine zones are the SVG elements flagged data-anno-special="vertebral-precision".
* Returns undefined when the SVG exposes no zone for that region.
*/
export function findSpineZoneId(
region: VertebralRegion,
bones: Map<string, Bone>,
): string | undefined {
for (const [id, bone] of bones) {
if (bone.special !== 'vertebral-precision') continue
const r = regionFromLabel(bone.fr) ?? regionFromLabel(bone.en)
if (r === region) return id
}
return undefined
}
const key = (v: Vertebra) => `${v.region}${v.index}`
/** Set union of two vertebra lists, keyed by region+index, preserving order. */
function unionVertebrae(base: Vertebra[], extra: Vertebra[]): Vertebra[] {
const seen = new Set(base.map(key))
const out = [...base]
for (const v of extra) {
if (seen.has(key(v))) continue
seen.add(key(v))
out.push(v)
}
return out
}
/**
* Spreads vertebrae confirmed in the picker across one spine zone per region.
*
* The picker is anchored on the clicked zone (targetIndex) but lets the user
* switch region tabs. Vertebrae picked in another region must behave as if the
* matching spine zone was also selected: it gets created if missing so the SVG
* highlights it and its findings picker appears.
*
* The target region is replaced (the picker was seeded with its full set, so
* unchecking deselects). Other regions are merged additively so a side-tab pick
* never wipes a pre-existing zone the picker did not display.
*
* Returns a new zones array; the original is not mutated.
*/
export function distributeVertebraeToZones(
zones: ObservationZone[],
targetIndex: number,
vertebrae: Vertebra[],
bones: Map<string, Bone>,
): ObservationZone[] {
const target = zones[targetIndex]
if (!target) return zones
const targetBone = bones.get(target.boneId)
const targetRegion =
regionFromLabel(targetBone?.fr ?? '') ?? regionFromLabel(targetBone?.en ?? '')
// Unknown target region: cannot split safely — keep all vertebrae on target.
if (!targetRegion) {
const next = [...zones]
next[targetIndex] = { ...target, vertebrae }
return next
}
const byRegion = new Map<VertebralRegion, Vertebra[]>()
for (const v of vertebrae) {
byRegion.set(v.region, [...(byRegion.get(v.region) ?? []), v])
}
const next = [...zones]
const unmapped: Vertebra[] = []
for (const [region, regionVerts] of byRegion) {
if (region === targetRegion) continue
const zoneId = findSpineZoneId(region, bones)
if (!zoneId) {
unmapped.push(...regionVerts)
continue
}
const idx = next.findIndex((z) => z.boneId === zoneId && !z.detailSourceId)
if (idx >= 0) {
next[idx] = {
...next[idx],
vertebrae: unionVertebrae(next[idx].vertebrae ?? [], regionVerts),
}
} else {
next.push({ boneId: zoneId, vertebrae: regionVerts })
}
}
next[targetIndex] = {
...target,
vertebrae: [...(byRegion.get(targetRegion) ?? []), ...unmapped],
}
return next
}