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:
1 parent
0a17e390cf
commit
ce2ec26112
114 files changed
+3560
-14637
No files matched your search
@@ -0,0 +1,97 @@
|
||||
import {
|
||||
createLayerElement,
|
||||
renderAnnotationLayer,
|
||||
viewBoxOf,
|
||||
} from '@faunosteo/anatomy/render'
|
||||
import type { SheetView } from '@faunosteo/domain'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
|
||||
/**
|
||||
* Builds the inline SVG of one view: the sheet's drawing (sanitised asset
|
||||
* text) framed on the view's box, plus the annotation layer generated from
|
||||
* the document. The drawing is parsed once per asset and cloned per view.
|
||||
*/
|
||||
|
||||
const SVG_NS = 'http://www.w3.org/2000/svg'
|
||||
const FORBIDDEN_TAGS = ['script', 'foreignObject', 'iframe', 'object', 'embed']
|
||||
|
||||
export type ViewStates = ReadonlyMap<string, 'current'>
|
||||
|
||||
/** Drops scripts, embedded documents, event handlers and script URLs (LRN-025). */
|
||||
export function sanitizeSvg(root: Element): void {
|
||||
for (const tag of FORBIDDEN_TAGS) {
|
||||
for (const el of Array.from(root.getElementsByTagName(tag))) el.remove()
|
||||
}
|
||||
for (const el of Array.from(root.querySelectorAll('*'))) {
|
||||
for (const attr of Array.from(el.attributes)) {
|
||||
const name = attr.name.toLowerCase()
|
||||
const value = attr.value.trim().toLowerCase()
|
||||
if (name.startsWith('on') || value.startsWith('javascript:')) {
|
||||
el.removeAttribute(attr.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function parseDrawing(svgText: string): SVGSVGElement {
|
||||
const doc = new DOMParser().parseFromString(svgText, 'image/svg+xml')
|
||||
if (doc.querySelector('parsererror')) throw new Error('invalid SVG asset')
|
||||
const root = doc.documentElement
|
||||
if (root.tagName.toLowerCase() !== 'svg')
|
||||
throw new Error('asset root is not <svg>')
|
||||
sanitizeSvg(root)
|
||||
return root as unknown as SVGSVGElement
|
||||
}
|
||||
|
||||
const parsed = new Map<string, SVGSVGElement>()
|
||||
|
||||
function drawingOf(index: SheetIndex, assetId: string): SVGSVGElement {
|
||||
const key = `${index.sheet.ref.documentSha256}:${assetId}`
|
||||
let root = parsed.get(key)
|
||||
if (!root) {
|
||||
const text = index.sheet.assets[assetId]
|
||||
if (text === undefined) throw new Error(`asset ${assetId} not bundled`)
|
||||
root = parseDrawing(text)
|
||||
parsed.set(key, root)
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
/** The asset a view draws: its own, else its parent's (a crop). */
|
||||
export function assetOfView(index: SheetIndex, view: SheetView): string {
|
||||
if (view.svg_asset_id) return view.svg_asset_id
|
||||
if (view.kind === 'detail') {
|
||||
const parent = index.view(view.parent_view_id)
|
||||
if (parent) return assetOfView(index, parent)
|
||||
}
|
||||
throw new Error(`view ${view.id} has no asset`)
|
||||
}
|
||||
|
||||
export function buildViewSvg(
|
||||
index: SheetIndex,
|
||||
viewId: string,
|
||||
states: ViewStates,
|
||||
): SVGSVGElement {
|
||||
const view = index.view(viewId)
|
||||
if (!view) throw new Error(`unknown view ${viewId}`)
|
||||
const source = drawingOf(index, assetOfView(index, view))
|
||||
const svg = document.createElementNS(SVG_NS, 'svg')
|
||||
for (const attr of Array.from(source.attributes)) {
|
||||
if (attr.name === 'width' || attr.name === 'height') continue
|
||||
svg.setAttribute(attr.name, attr.value)
|
||||
}
|
||||
svg.setAttribute(
|
||||
'viewBox',
|
||||
viewBoxOf(view) ?? source.getAttribute('viewBox') ?? '0 0 100 100',
|
||||
)
|
||||
for (const child of Array.from(source.childNodes)) {
|
||||
svg.appendChild(document.importNode(child, true))
|
||||
}
|
||||
const layer = renderAnnotationLayer({
|
||||
document: index.sheet.document,
|
||||
viewId,
|
||||
states,
|
||||
})
|
||||
svg.appendChild(createLayerElement(document, layer) as unknown as Node)
|
||||
return svg
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { pickLocaleLabel } from '@/i18n/pickLocaleLabel'
|
||||
import type { Bone } from './types'
|
||||
|
||||
/**
|
||||
* Resolves the user-facing label for an anatomical zone given the active
|
||||
* locale. Falls back to the opposite language when the requested one is
|
||||
* missing (some SVG hitboxes carry only `data-anno-name-fr`), then to the
|
||||
* provided fallback (typically the raw boneId) so the UI never renders
|
||||
* an empty label.
|
||||
*/
|
||||
export function boneLabel(
|
||||
bone: Bone | undefined,
|
||||
locale: Locale,
|
||||
fallback: string,
|
||||
): string {
|
||||
if (!bone) return fallback
|
||||
return pickLocaleLabel(bone.fr, bone.en, locale, fallback)
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
import { SvgConventionError } from './types'
|
||||
import type { Bone, BoneCategory } from './types'
|
||||
|
||||
/**
|
||||
* Validates that an annotated SVG element carries the two attributes the
|
||||
* application needs to wire it up: a stable id (`data-anno-id`) and a
|
||||
* category bucket (`data-anno-category`). Labels (`data-anno-name-fr/en`)
|
||||
* and the popup trigger (`data-anno-opens-detail`) are optional and
|
||||
* checked only at use-site.
|
||||
*
|
||||
* The category value itself is opaque to the application — any non-empty
|
||||
* string is accepted. New SVGs are free to introduce new categories
|
||||
* without touching this parser.
|
||||
*/
|
||||
function validateBoneElement(el: Element): void {
|
||||
const boneId = el.getAttribute('data-anno-id') ?? ''
|
||||
if (!boneId) {
|
||||
throw new SvgConventionError(
|
||||
'Element with missing data-anno-id',
|
||||
boneId,
|
||||
'data-anno-id',
|
||||
)
|
||||
}
|
||||
const cat = el.getAttribute('data-anno-category') ?? ''
|
||||
if (!cat) {
|
||||
throw new SvgConventionError(
|
||||
`Element data-anno-id="${boneId}" missing attribute data-anno-category`,
|
||||
boneId,
|
||||
'data-anno-category',
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an SVG document and returns a map of `data-anno-id` → Bone.
|
||||
*
|
||||
* Every element carrying `data-anno-id` is collected. Multiple elements
|
||||
* may share the same id (e.g. hitbox + visible layer); they all end up
|
||||
* grouped under the same Bone with all elements in `els`.
|
||||
*
|
||||
* Throws `SvgConventionError` only on the two required attributes
|
||||
* (`data-anno-id`, `data-anno-category`). Labels are optional.
|
||||
*/
|
||||
export function parseSvgBones(svgRoot: SVGSVGElement): Map<string, Bone> {
|
||||
const map = new Map<string, Bone>()
|
||||
const all = svgRoot.querySelectorAll<Element>('[data-anno-id]')
|
||||
all.forEach((el) => {
|
||||
validateBoneElement(el)
|
||||
const id = el.getAttribute('data-anno-id') as string
|
||||
const fr = el.getAttribute('data-anno-name-fr') ?? ''
|
||||
const en = el.getAttribute('data-anno-name-en') ?? ''
|
||||
const category = el.getAttribute('data-anno-category') as BoneCategory
|
||||
const opensDetail =
|
||||
el.getAttribute('data-anno-opens-detail') ?? undefined
|
||||
const special = el.getAttribute('data-anno-special') ?? undefined
|
||||
const lateral = (el.getAttribute('data-anno-lateral') ?? undefined) as
|
||||
| 'bilateral'
|
||||
| 'unpaired'
|
||||
| undefined
|
||||
|
||||
const existing = map.get(id)
|
||||
if (existing) {
|
||||
existing.els.push(el)
|
||||
if (opensDetail && !existing.opensDetail) {
|
||||
existing.opensDetail = opensDetail
|
||||
}
|
||||
if (special && !existing.special) {
|
||||
existing.special = special
|
||||
}
|
||||
if (lateral && !existing.lateral) {
|
||||
existing.lateral = lateral
|
||||
}
|
||||
if (!existing.fr && fr) existing.fr = fr
|
||||
if (!existing.en && en) existing.en = en
|
||||
} else {
|
||||
map.set(id, {
|
||||
id,
|
||||
fr,
|
||||
en,
|
||||
category,
|
||||
opensDetail,
|
||||
special,
|
||||
lateral,
|
||||
els: [el],
|
||||
})
|
||||
}
|
||||
})
|
||||
return map
|
||||
}
|
||||
|
||||
/**
|
||||
* Walks up from `event.target` to find the nearest element carrying
|
||||
* `data-anno-id`. Returns null when the click did not land on an
|
||||
* annotated element.
|
||||
*/
|
||||
export function getBoneIdFromEvent(event: Event): string | null {
|
||||
return getAnnotatedElement(event)?.getAttribute('data-anno-id') || null
|
||||
}
|
||||
|
||||
export function getAnnotatedElement(event: Event): Element | null {
|
||||
let node = event.target as Element | null
|
||||
while (node && node.nodeType === 1) {
|
||||
if (node.hasAttribute('data-anno-id')) return node
|
||||
node = node.parentElement
|
||||
}
|
||||
return null
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
export async function loadSvg(publicPath: string): Promise<SVGSVGElement> {
|
||||
const resp = await fetch(publicPath)
|
||||
if (!resp.ok) {
|
||||
throw new Error(`Failed to load SVG ${publicPath}: HTTP ${resp.status}`)
|
||||
}
|
||||
const text = await resp.text()
|
||||
const doc = new DOMParser().parseFromString(text, 'image/svg+xml')
|
||||
const parseErr = doc.querySelector('parsererror')
|
||||
if (parseErr) {
|
||||
throw new Error(`Invalid SVG at ${publicPath}: ${parseErr.textContent}`)
|
||||
}
|
||||
const root = doc.documentElement
|
||||
if (root.tagName.toLowerCase() !== 'svg') {
|
||||
throw new Error(`Root element of ${publicPath} is not <svg>`)
|
||||
}
|
||||
return root as unknown as SVGSVGElement
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
/**
|
||||
* `data-anno-category` is a free-form string supplied by the SVG.
|
||||
*
|
||||
* The application never branches on category values — it treats every
|
||||
* annotation the same way. Categories are kept around because they're
|
||||
* useful for grouping and future filtering, but no code path here may
|
||||
* rely on a closed set.
|
||||
*/
|
||||
export type BoneCategory = string
|
||||
|
||||
/**
|
||||
* Logical annotation as parsed from an SVG. All DOM elements sharing the
|
||||
* same `data-anno-id` are grouped into one Bone. `id` is the persisted
|
||||
* identifier surfaced in the report JSON contract
|
||||
* (`ObservationZone.boneId`); keep it stable across SVG renames.
|
||||
*/
|
||||
export interface Bone {
|
||||
id: string
|
||||
/** French label from data-anno-name-fr — may be empty for opens-detail-only triggers */
|
||||
fr: string
|
||||
/** English label from data-anno-name-en — may be empty */
|
||||
en: string
|
||||
/** Raw value from data-anno-category — opaque to the application */
|
||||
category: BoneCategory
|
||||
/** Detail popup id from data-anno-opens-detail, if present */
|
||||
opensDetail?: string
|
||||
/**
|
||||
* Free-form behavior hint from `data-anno-special`.
|
||||
*
|
||||
* Known values consumed by the application:
|
||||
* - `vertebral-precision`: clicking the zone opens VertebraPicker.
|
||||
*
|
||||
* Opaque to the parser; new values are accepted without code changes.
|
||||
*/
|
||||
special?: string
|
||||
/** Laterality hint from data-anno-lateral — bilateral or unpaired */
|
||||
lateral?: 'bilateral' | 'unpaired'
|
||||
/** All DOM elements carrying this annotation (used for colorization) */
|
||||
els: Element[]
|
||||
}
|
||||
|
||||
/** Visual state applied to SVG elements (active-observation highlight). */
|
||||
export type BoneState = 'current' | 'default'
|
||||
|
||||
/** Error thrown when SVG violates the convention. */
|
||||
export class SvgConventionError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public readonly boneId: string,
|
||||
public readonly missingAttribute: string,
|
||||
) {
|
||||
super(message)
|
||||
this.name = 'SvgConventionError'
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user