refactor(anatomy): category becomes free-form string, no enum / no auto-trigger
User asked that the app interact with anything carrying data-anno-* attributes without hardcoding any specific category, id, or name. Future SVGs may introduce new categories, and the existing SVGs may be updated. Changes: - `BoneCategory` is now `type BoneCategory = string`. The closed union is gone; the parser accepts any non-empty value. New categories don't require code changes. - `VALID_CATEGORIES` and `isValidCategory()` removed from svgConvention.ts. `validateBoneElement()` now only enforces presence of `data-anno-id` and `data-anno-category`. - EditorRoute no longer auto-triggers the vertebra picker when the clicked annotation is `zone-colonne`. Any zone is added the same way to the active observation regardless of category. - ObservationCard shows a "Vertèbres…" link on every zone badge (was conditional on `category === 'zone-colonne'`). The praticien picks vertebrae only when relevant — the UI no longer knows or cares which zones are spinal. - Tests rewritten: removed the `isValidCategory` enum assertions and the "rejects unknown category" branch. New test demonstrates that parseSvgBones accepts a completely novel category string. 90/90 tests pass, lint clean, build clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
cad94a49db
commit
21b678c3d5
5 files changed
+41
-76
No files matched your search
@@ -1,18 +1,17 @@
|
|||||||
import { SvgConventionError } from './types'
|
import { SvgConventionError } from './types'
|
||||||
import type { Bone, BoneCategory } from './types'
|
import type { Bone, BoneCategory } from './types'
|
||||||
|
|
||||||
const VALID_CATEGORIES: readonly BoneCategory[] = [
|
/**
|
||||||
'articulations',
|
* Validates that an annotated SVG element carries the two attributes the
|
||||||
'charni-res-vert-brales',
|
* application needs to wire it up: a stable id (`data-anno-id`) and a
|
||||||
'zone-colonne',
|
* category bucket (`data-anno-category`). Labels (`data-anno-name-fr/en`)
|
||||||
'cadrans-du-cr-ne',
|
* and the popup trigger (`data-anno-opens-detail`) are optional and
|
||||||
'os',
|
* checked only at use-site.
|
||||||
]
|
*
|
||||||
|
* The category value itself is opaque to the application — any non-empty
|
||||||
export function isValidCategory(value: string): value is BoneCategory {
|
* string is accepted. New SVGs are free to introduce new categories
|
||||||
return (VALID_CATEGORIES as readonly string[]).includes(value)
|
* without touching this parser.
|
||||||
}
|
*/
|
||||||
|
|
||||||
export function validateBoneElement(el: Element): void {
|
export function validateBoneElement(el: Element): void {
|
||||||
const boneId = el.getAttribute('data-anno-id') ?? ''
|
const boneId = el.getAttribute('data-anno-id') ?? ''
|
||||||
if (!boneId) {
|
if (!boneId) {
|
||||||
@@ -30,26 +29,17 @@ export function validateBoneElement(el: Element): void {
|
|||||||
'data-anno-category',
|
'data-anno-category',
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if (!isValidCategory(cat)) {
|
|
||||||
throw new SvgConventionError(
|
|
||||||
`Element data-anno-id="${boneId}" has unknown data-anno-category="${cat}"`,
|
|
||||||
boneId,
|
|
||||||
'data-anno-category',
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses an SVG document and returns a map of `data-anno-id` → Bone.
|
* Parses an SVG document and returns a map of `data-anno-id` → Bone.
|
||||||
*
|
*
|
||||||
* Every element carrying `data-anno-id` is collected. Multiple elements
|
* Every element carrying `data-anno-id` is collected. Multiple elements
|
||||||
* may share the same id (hitbox + visible pattern, or layered shapes);
|
* may share the same id (e.g. hitbox + visible layer); they all end up
|
||||||
* they end up grouped under the same Bone with all elements in `els`.
|
* grouped under the same Bone with all elements in `els`.
|
||||||
*
|
*
|
||||||
* Throws `SvgConventionError` if any required attribute is missing or
|
* Throws `SvgConventionError` only on the two required attributes
|
||||||
* `data-anno-category` is unknown. `data-anno-name-fr/en` are optional —
|
* (`data-anno-id`, `data-anno-category`). Labels are optional.
|
||||||
* trigger-only zones (those carrying `data-anno-opens-detail`) typically
|
|
||||||
* skip them.
|
|
||||||
*/
|
*/
|
||||||
export function parseSvgBones(svgRoot: SVGSVGElement): Map<string, Bone> {
|
export function parseSvgBones(svgRoot: SVGSVGElement): Map<string, Bone> {
|
||||||
const map = new Map<string, Bone>()
|
const map = new Map<string, Bone>()
|
||||||
@@ -69,7 +59,6 @@ export function parseSvgBones(svgRoot: SVGSVGElement): Map<string, Bone> {
|
|||||||
if (opensDetail && !existing.opensDetail) {
|
if (opensDetail && !existing.opensDetail) {
|
||||||
existing.opensDetail = opensDetail
|
existing.opensDetail = opensDetail
|
||||||
}
|
}
|
||||||
// Prefer the first non-empty label we see.
|
|
||||||
if (!existing.fr && fr) existing.fr = fr
|
if (!existing.fr && fr) existing.fr = fr
|
||||||
if (!existing.en && en) existing.en = en
|
if (!existing.en && en) existing.en = en
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+7
-10
@@ -1,16 +1,12 @@
|
|||||||
/**
|
/**
|
||||||
* Valid data-anno-category values per the SVG convention.
|
* `data-anno-category` is a free-form string supplied by the SVG.
|
||||||
*
|
*
|
||||||
* Categories are scoped to the kind of anatomical structure each
|
* The application never branches on category values — it treats every
|
||||||
* annotation represents. Adding a new value here is the only place to
|
* annotation the same way. Categories are kept around because they're
|
||||||
* touch when the source SVGs introduce one.
|
* useful for grouping and future filtering, but no code path here may
|
||||||
|
* rely on a closed set.
|
||||||
*/
|
*/
|
||||||
export type BoneCategory =
|
export type BoneCategory = string
|
||||||
| 'articulations'
|
|
||||||
| 'charni-res-vert-brales'
|
|
||||||
| 'zone-colonne'
|
|
||||||
| 'cadrans-du-cr-ne'
|
|
||||||
| 'os'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logical annotation as parsed from an SVG. All DOM elements sharing the
|
* Logical annotation as parsed from an SVG. All DOM elements sharing the
|
||||||
@@ -24,6 +20,7 @@ export interface Bone {
|
|||||||
fr: string
|
fr: string
|
||||||
/** English label from data-anno-name-en — may be empty */
|
/** English label from data-anno-name-en — may be empty */
|
||||||
en: string
|
en: string
|
||||||
|
/** Raw value from data-anno-category — opaque to the application */
|
||||||
category: BoneCategory
|
category: BoneCategory
|
||||||
/** Detail popup id from data-anno-opens-detail, if present */
|
/** Detail popup id from data-anno-opens-detail, if present */
|
||||||
opensDetail?: string
|
opensDetail?: string
|
||||||
|
|||||||
@@ -118,10 +118,6 @@ export default function EditorRoute() {
|
|||||||
)
|
)
|
||||||
: [...activeObs.zones, newZone]
|
: [...activeObs.zones, newZone]
|
||||||
updateObservation(activeIndex, { ...activeObs, zones: nextZones })
|
updateObservation(activeIndex, { ...activeObs, zones: nextZones })
|
||||||
|
|
||||||
if (!exists && bone.category === 'zone-colonne') {
|
|
||||||
setVertebraTarget(nextZones.length - 1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onExport() {
|
async function onExport() {
|
||||||
|
|||||||
@@ -187,20 +187,18 @@ export default function ObservationCard({
|
|||||||
<ul className="flex flex-wrap gap-2">
|
<ul className="flex flex-wrap gap-2">
|
||||||
{observation.zones.map((z, i) => {
|
{observation.zones.map((z, i) => {
|
||||||
const b = bones.get(z.boneId)
|
const b = bones.get(z.boneId)
|
||||||
const isSpine = b?.category === 'zone-colonne'
|
|
||||||
return (
|
return (
|
||||||
<li key={`${z.boneId}-${i}`}>
|
<li key={`${z.boneId}-${i}`}>
|
||||||
<Badge tone="accent" className="gap-2">
|
<Badge tone="accent" className="gap-2">
|
||||||
{describeZone(z, bones)}
|
{describeZone(z, bones)}
|
||||||
{isSpine && (
|
<button
|
||||||
<button
|
type="button"
|
||||||
type="button"
|
onClick={() => onOpenVertebraPicker(i)}
|
||||||
onClick={() => onOpenVertebraPicker(i)}
|
className="underline text-xs text-[var(--color-accent-strong)]"
|
||||||
className="underline text-xs text-[var(--color-accent-strong)]"
|
title="Préciser les vertèbres pour cette zone"
|
||||||
>
|
>
|
||||||
Préciser…
|
Vertèbres…
|
||||||
</button>
|
</button>
|
||||||
)}
|
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
aria-label={`Retirer ${b?.fr ?? z.boneId}`}
|
aria-label={`Retirer ${b?.fr ?? z.boneId}`}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import { describe, it, expect } from 'vitest'
|
|||||||
import {
|
import {
|
||||||
parseSvgBones,
|
parseSvgBones,
|
||||||
getBoneIdFromEvent,
|
getBoneIdFromEvent,
|
||||||
isValidCategory,
|
|
||||||
} from '@/anatomy/svgConvention'
|
} from '@/anatomy/svgConvention'
|
||||||
import { SvgConventionError } from '@/anatomy/types'
|
import { SvgConventionError } from '@/anatomy/types'
|
||||||
|
|
||||||
@@ -20,6 +19,8 @@ const FIXTURE = `
|
|||||||
<polygon data-anno-id="zone-cervical" data-anno-name-fr="Vertèbres cervicales"
|
<polygon data-anno-id="zone-cervical" data-anno-name-fr="Vertèbres cervicales"
|
||||||
data-anno-name-en="Cervical Vertebrae" data-anno-category="zone-colonne" />
|
data-anno-name-en="Cervical Vertebrae" data-anno-category="zone-colonne" />
|
||||||
<polygon data-anno-id="zone-head" data-anno-category="os" data-anno-opens-detail="head" />
|
<polygon data-anno-id="zone-head" data-anno-category="os" data-anno-opens-detail="head" />
|
||||||
|
<polygon data-anno-id="zone-future" data-anno-name-fr="Catégorie inédite"
|
||||||
|
data-anno-category="something-new-the-parser-has-never-seen" />
|
||||||
</svg>`
|
</svg>`
|
||||||
|
|
||||||
describe('parseSvgBones', () => {
|
describe('parseSvgBones', () => {
|
||||||
@@ -38,19 +39,19 @@ describe('parseSvgBones', () => {
|
|||||||
expect(map.get('zone-head')?.opensDetail).toBe('head')
|
expect(map.get('zone-head')?.opensDetail).toBe('head')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('records zone-colonne single-element annotations', () => {
|
|
||||||
const map = parseSvgBones(svgFromString(FIXTURE))
|
|
||||||
const bone = map.get('zone-cervical')
|
|
||||||
expect(bone?.category).toBe('zone-colonne')
|
|
||||||
expect(bone?.els.length).toBe(1)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('allows annotations without data-anno-name-fr/en (trigger-only zones)', () => {
|
it('allows annotations without data-anno-name-fr/en (trigger-only zones)', () => {
|
||||||
const map = parseSvgBones(svgFromString(FIXTURE))
|
const map = parseSvgBones(svgFromString(FIXTURE))
|
||||||
const trigger = map.get('zone-head')
|
const trigger = map.get('zone-head')
|
||||||
expect(trigger?.fr).toBe('')
|
expect(trigger?.fr).toBe('')
|
||||||
expect(trigger?.en).toBe('')
|
expect(trigger?.en).toBe('')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('accepts any string as data-anno-category (no hardcoded enum)', () => {
|
||||||
|
const map = parseSvgBones(svgFromString(FIXTURE))
|
||||||
|
expect(map.get('zone-future')?.category).toBe(
|
||||||
|
'something-new-the-parser-has-never-seen',
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('parseSvgBones validation', () => {
|
describe('parseSvgBones validation', () => {
|
||||||
@@ -61,30 +62,14 @@ describe('parseSvgBones validation', () => {
|
|||||||
expect(() => parseSvgBones(svgFromString(bad))).toThrow(SvgConventionError)
|
expect(() => parseSvgBones(svgFromString(bad))).toThrow(SvgConventionError)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws on unknown category', () => {
|
it('throws on empty data-anno-id', () => {
|
||||||
const bad = `<svg xmlns="http://www.w3.org/2000/svg">
|
const bad = `<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
<circle data-anno-id="x" data-anno-category="bogus" />
|
<circle data-anno-id="" data-anno-category="anything" />
|
||||||
</svg>`
|
</svg>`
|
||||||
expect(() => parseSvgBones(svgFromString(bad))).toThrow(SvgConventionError)
|
expect(() => parseSvgBones(svgFromString(bad))).toThrow(SvgConventionError)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('isValidCategory', () => {
|
|
||||||
it('accepts documented categories', () => {
|
|
||||||
expect(isValidCategory('articulations')).toBe(true)
|
|
||||||
expect(isValidCategory('zone-colonne')).toBe(true)
|
|
||||||
expect(isValidCategory('cadrans-du-cr-ne')).toBe(true)
|
|
||||||
expect(isValidCategory('charni-res-vert-brales')).toBe(true)
|
|
||||||
expect(isValidCategory('os')).toBe(true)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('rejects unknown values', () => {
|
|
||||||
expect(isValidCategory('nope')).toBe(false)
|
|
||||||
expect(isValidCategory('articulation')).toBe(false) // old singular form
|
|
||||||
expect(isValidCategory('spine-zone')).toBe(false) // old name
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('getBoneIdFromEvent', () => {
|
describe('getBoneIdFromEvent', () => {
|
||||||
it('returns null when no anno in ancestry', () => {
|
it('returns null when no anno in ancestry', () => {
|
||||||
const div = document.createElement('div')
|
const div = document.createElement('div')
|
||||||
|
|||||||
Reference in new issue
Block a user