feat(editor): unified spine picker — show all regions, click opens own tab
The vertebra picker now edits the whole spine at once instead of a single zone. Seed it with the union of vertebrae across every spine zone of the observation (collectSpineVertebrae) so switching tabs reveals selections made on other zones. Clicking a zone always opens its own region tab (initialRegion now takes priority over the first existing selection). On confirm, syncSpineZones reconciles authoritatively per region: replace each region's vertebrae (deselection sticks), prune a spine zone whose region drops to zero, spawn a zone for a newly picked region. Non-spine zones and their order are preserved untouched. Supersedes the additive distribute model. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
fc7069c2cb
commit
38888cb3d1
5 files changed
+137
-96
No files matched your search
@@ -19,7 +19,7 @@ import { writeCachedPractitioner } from '@/state/practitionerCache'
|
||||
import { getSpecies } from '@/domain/species/registry'
|
||||
import { loadSvg } from '@/anatomy/svgLoader'
|
||||
import { parseSvgBones } from '@/anatomy/svgConvention'
|
||||
import { distributeVertebraeToZones } from '@/lib/spineZones'
|
||||
import { collectSpineVertebrae, syncSpineZones } from '@/lib/spineZones'
|
||||
import type { Bone, BoneState } from '@/anatomy/types'
|
||||
import type { Observation, ObservationZone } from '@/domain/schema/observation'
|
||||
import type { SpeciesFindings } from '@/domain/schema/findings'
|
||||
@@ -439,18 +439,17 @@ export default function EditorRoute() {
|
||||
setVertebraTarget(null)
|
||||
return
|
||||
}
|
||||
// Vertebrae picked in another region tab spawn/merge the matching
|
||||
// spine zone, so the SVG highlights it and its findings picker shows.
|
||||
const next = distributeVertebraeToZones(
|
||||
fresh.zones,
|
||||
vertebraTarget,
|
||||
vertebrae,
|
||||
bones,
|
||||
)
|
||||
// Picker edits every region at once: reconcile all spine zones —
|
||||
// spawn newly picked regions, prune emptied ones, keep the rest.
|
||||
const next = syncSpineZones(fresh.zones, vertebrae, bones)
|
||||
updateObservation(activeIndex, { ...fresh, zones: next })
|
||||
setVertebraTarget(null)
|
||||
}}
|
||||
initial={activeObs.zones[vertebraTarget].vertebrae ?? []}
|
||||
initial={collectSpineVertebrae(
|
||||
activeObs.zones,
|
||||
bones,
|
||||
activeObs.zones[vertebraTarget].boneId,
|
||||
)}
|
||||
formula={species.vertebralFormula}
|
||||
title={`${t('vertebra.title_prefix')}${boneLabel(
|
||||
bones.get(activeObs.zones[vertebraTarget].boneId),
|
||||
|
||||
@@ -24,7 +24,7 @@ export interface VertebraPickerProps {
|
||||
initial: Vertebra[]
|
||||
formula: VertebralFormula
|
||||
title: string
|
||||
/** Preselected tab. Overridden by the region of the first existing selection. */
|
||||
/** Tab to open on. Takes priority so a click always lands on its own zone. */
|
||||
initialRegion?: VertebralRegion
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ export default function VertebraPicker({
|
||||
const t = useT()
|
||||
const [selected, setSelected] = useState<Vertebra[]>(initial)
|
||||
const [activeRegion, setActiveRegion] = useState<VertebralRegion>(
|
||||
() => initial[0]?.region ?? initialRegion ?? 'C',
|
||||
() => initialRegion ?? initial[0]?.region ?? 'C',
|
||||
)
|
||||
|
||||
const has = (r: VertebralRegion, i: number) =>
|
||||
|
||||
+62
-53
@@ -42,81 +42,90 @@ export function findSpineZoneId(
|
||||
|
||||
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)
|
||||
const isSpineZone = (z: ObservationZone, bones: Map<string, Bone>) =>
|
||||
!z.detailSourceId && bones.get(z.boneId)?.special === 'vertebral-precision'
|
||||
|
||||
/**
|
||||
* Gathers the union of vertebrae across every spine zone of an observation so
|
||||
* the picker can show selections from all regions at once. `priorityBoneId`'s
|
||||
* vertebrae are listed first so the picker opens on the clicked zone's tab
|
||||
* (the picker keys its default tab off the first vertebra's region).
|
||||
*
|
||||
* Deduplicated by region+index; order otherwise follows zone order.
|
||||
*/
|
||||
export function collectSpineVertebrae(
|
||||
zones: ObservationZone[],
|
||||
bones: Map<string, Bone>,
|
||||
priorityBoneId?: string,
|
||||
): Vertebra[] {
|
||||
const spine = zones.filter((z) => isSpineZone(z, bones))
|
||||
const ordered = priorityBoneId
|
||||
? [
|
||||
...spine.filter((z) => z.boneId === priorityBoneId),
|
||||
...spine.filter((z) => z.boneId !== priorityBoneId),
|
||||
]
|
||||
: spine
|
||||
|
||||
const out: Vertebra[] = []
|
||||
const seen = new Set<string>()
|
||||
for (const z of ordered) {
|
||||
for (const v of z.vertebrae ?? []) {
|
||||
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.
|
||||
* Reconciles the observation's spine zones with the full set of vertebrae the
|
||||
* picker confirmed (the picker shows and edits every region at once).
|
||||
*
|
||||
* 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.
|
||||
* Authoritative per region: each region's zone gets exactly the vertebrae
|
||||
* picked for it (replace, so unchecking sticks); a region with no vertebrae
|
||||
* left has its spine zone pruned; a region picked for the first time spawns its
|
||||
* zone so the SVG highlights it and its findings picker appears. Non-spine
|
||||
* zones (and their order) are preserved untouched.
|
||||
*
|
||||
* Returns a new zones array; the original is not mutated.
|
||||
*/
|
||||
export function distributeVertebraeToZones(
|
||||
export function syncSpineZones(
|
||||
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[] = []
|
||||
const result: ObservationZone[] = []
|
||||
const handled = new Set<VertebralRegion>()
|
||||
|
||||
for (const [region, regionVerts] of byRegion) {
|
||||
if (region === targetRegion) continue
|
||||
const zoneId = findSpineZoneId(region, bones)
|
||||
if (!zoneId) {
|
||||
unmapped.push(...regionVerts)
|
||||
for (const z of zones) {
|
||||
const bone = bones.get(z.boneId)
|
||||
const region =
|
||||
!z.detailSourceId && bone?.special === 'vertebral-precision'
|
||||
? (regionFromLabel(bone.fr) ?? regionFromLabel(bone.en))
|
||||
: undefined
|
||||
// Non-spine, detail-sourced, or unrecognized-label zones pass through.
|
||||
if (!region) {
|
||||
result.push(z)
|
||||
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 })
|
||||
}
|
||||
const verts = byRegion.get(region)
|
||||
if (!verts) continue // region deselected to zero → prune the zone
|
||||
result.push({ ...z, vertebrae: verts })
|
||||
handled.add(region)
|
||||
}
|
||||
|
||||
next[targetIndex] = {
|
||||
...target,
|
||||
vertebrae: [...(byRegion.get(targetRegion) ?? []), ...unmapped],
|
||||
// Regions picked that had no existing zone yet → spawn one.
|
||||
for (const [region, verts] of byRegion) {
|
||||
if (handled.has(region)) continue
|
||||
const zoneId = findSpineZoneId(region, bones)
|
||||
if (zoneId) result.push({ boneId: zoneId, vertebrae: verts })
|
||||
}
|
||||
return next
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in new issue
Block a user