diff --git a/src/anatomy/svgConvention.ts b/src/anatomy/svgConvention.ts index f753d813..6b1e5798 100644 --- a/src/anatomy/svgConvention.ts +++ b/src/anatomy/svgConvention.ts @@ -40,8 +40,26 @@ export function validateBoneElement(el: Element): void { } } +/** + * A bone is interactive (clickable) if any of these is true: + * - at least one element carries the `hitbox` class (hitbox+visible pattern); + * - its category is `spine-zone` (single overlay polygon, pointer-events:all); + * - its category is `head` (single entry-point polygon for a detail SVG). + * + * Decorative elements (e.g. individual vertebrae paths, teeth, malar bone) + * carry `data-bone-id` so their colorization can be controlled by JS, but + * must NOT intercept clicks. We mark them with `pointer-events: none` and + * exclude them from the returned Bone map so the click handler ignores + * them naturally. + */ +function isInteractiveBone(bone: Bone): boolean { + if (bone.hitboxEls.length > 0) return true + if (bone.category === 'spine-zone' || bone.category === 'head') return true + return false +} + export function parseSvgBones(svgRoot: SVGSVGElement): Map { - const map = new Map() + const raw = new Map() const all = svgRoot.querySelectorAll('[data-bone-id]') all.forEach((el) => { validateBoneElement(el) @@ -53,13 +71,13 @@ export function parseSvgBones(svgRoot: SVGSVGElement): Map { const classes = classList(el) const isHitbox = classes.includes('hitbox') - const existing = map.get(id) + const existing = raw.get(id) if (existing) { if (isHitbox) existing.hitboxEls.push(el) else existing.visibleEls.push(el) if (opensDetail && !existing.opensDetail) existing.opensDetail = opensDetail } else { - map.set(id, { + raw.set(id, { id, fr, en, @@ -70,7 +88,23 @@ export function parseSvgBones(svgRoot: SVGSVGElement): Map { }) } }) - return map + + const interactive = new Map() + raw.forEach((bone, id) => { + if (isInteractiveBone(bone)) { + interactive.set(id, bone) + } else { + // Decorative — make sure clicks fall through to whatever is underneath. + bone.visibleEls.forEach((el) => { + if (el instanceof SVGElement || el instanceof HTMLElement) { + el.style.pointerEvents = 'none' + } else { + el.setAttribute('pointer-events', 'none') + } + }) + } + }) + return interactive } export function getBoneIdFromEvent(event: Event): string | null { diff --git a/src/components/observation/ObservationCard.tsx b/src/components/observation/ObservationCard.tsx index 84970c73..b024a4c0 100644 --- a/src/components/observation/ObservationCard.tsx +++ b/src/components/observation/ObservationCard.tsx @@ -30,39 +30,95 @@ export default function ObservationCard({ const patCount = observation.pathologyIds.length + observation.freeTextPathologies.length + function onCardKey(event: React.KeyboardEvent) { + if (event.key === 'Enter' || event.key === ' ') { + event.preventDefault() + onSelect() + } + } + + function stop(fn: () => void) { + return (event: React.MouseEvent) => { + event.stopPropagation() + fn() + } + } + return ( -
- -
- - - +
+
event.stopPropagation()} + > + + +
diff --git a/src/components/report/NextAppointmentPicker.tsx b/src/components/report/NextAppointmentPicker.tsx index fae53de2..1c5f024d 100644 --- a/src/components/report/NextAppointmentPicker.tsx +++ b/src/components/report/NextAppointmentPicker.tsx @@ -7,30 +7,53 @@ export interface NextAppointmentPickerProps { onChange: (next: Report['nextAppointment']) => void } -export default function NextAppointmentPicker({ value, onChange }: NextAppointmentPickerProps) { - const kind = value?.kind ?? 'date' +export default function NextAppointmentPicker({ + value, + onChange, +}: NextAppointmentPickerProps) { + const kind = value?.kind return (
{value?.kind === 'date' && ( - onChange({ kind: 'date', date: e.target.value })} /> + onChange({ kind: 'date', date: e.target.value })} + /> )} {value?.kind === 'period' && ( - onChange({ kind: 'period', text: e.target.value })} /> + onChange({ kind: 'period', text: e.target.value })} + /> )}
) diff --git a/src/pdf/render/svgToPng.ts b/src/pdf/render/svgToPng.ts index 9d1f9a71..bc7d1347 100644 --- a/src/pdf/render/svgToPng.ts +++ b/src/pdf/render/svgToPng.ts @@ -2,12 +2,21 @@ import { Canvg } from 'canvg' const TARGET_WIDTH = 1200 -export async function rasterizeSvg(svgString: string, scale: number): Promise { +export async function rasterizeSvg( + svgString: string, + scale: number, +): Promise { const tmp = new DOMParser().parseFromString(svgString, 'image/svg+xml') const root = tmp.documentElement - const vb = (root.getAttribute('viewBox') ?? '0 0 1000 600').split(/\s+/).map(Number) - const widthPx = Math.round(TARGET_WIDTH * Math.max(1, scale) * 0.5) - const heightPx = Math.round((vb[3] / vb[2]) * widthPx) + const vb = (root.getAttribute('viewBox') ?? '0 0 1000 600') + .split(/\s+/) + .map(Number) + const widthPx = Math.max( + 1, + Math.round(TARGET_WIDTH * Math.max(1, scale) * 0.5), + ) + const ratio = vb[3] > 0 && vb[2] > 0 ? vb[3] / vb[2] : 0.6 + const heightPx = Math.max(1, Math.round(ratio * widthPx)) const canvas = document.createElement('canvas') canvas.width = widthPx @@ -15,11 +24,38 @@ export async function rasterizeSvg(svgString: string, scale: number): Promise { - canvas.toBlob((b) => (b ? resolve(b) : reject(new Error('toBlob returned null'))), 'image/png') + try { + const canv = await Canvg.from(ctx, svgString, { + anonymousCrossOrigin: true, + }) + await canv.render() + } catch (err) { + console.error('canvg render failed; falling back to blank canvas', err) + } + + const blob = await new Promise((resolve) => { + canvas.toBlob((b) => resolve(b), 'image/png') }) - return new Uint8Array(await blob.arrayBuffer()) + if (blob) return new Uint8Array(await blob.arrayBuffer()) + + // toBlob can return null in some browsers when the canvas state is + // unusual; fall back to toDataURL which is synchronous and broadly + // available. + const dataUrl = canvas.toDataURL('image/png') + const comma = dataUrl.indexOf(',') + if (comma < 0) { + throw new Error('canvas.toDataURL returned an unexpected payload') + } + const base64 = dataUrl.slice(comma + 1) + const binary = atob(base64) + const bytes = new Uint8Array(binary.length) + for (let i = 0; i < binary.length; i += 1) { + bytes[i] = binary.charCodeAt(i) + } + return bytes } diff --git a/tests/unit/pdf.svgToPng.test.ts b/tests/unit/pdf.svgToPng.test.ts index a800dde1..4e865dd5 100644 --- a/tests/unit/pdf.svgToPng.test.ts +++ b/tests/unit/pdf.svgToPng.test.ts @@ -8,7 +8,10 @@ vi.mock('canvg', () => ({ describe('rasterizeSvg', () => { it('returns a Uint8Array', async () => { const canvas = document.createElement('canvas') - canvas.getContext = (() => ({})) as unknown as HTMLCanvasElement['getContext'] + canvas.getContext = (() => ({ + fillStyle: '', + fillRect: () => {}, + })) as unknown as HTMLCanvasElement['getContext'] canvas.toBlob = ((cb: BlobCallback) => { cb(new Blob([new Uint8Array([1, 2, 3])], { type: 'image/png' })) }) as HTMLCanvasElement['toBlob']