From 706c056646ca40f9210c1798b3bee5f1c2c6b70c Mon Sep 17 00:00:00 2001 From: bchanot Date: Mon, 23 Nov 2026 10:29:18 +0100 Subject: [PATCH] feat(anatomy): annotations win over accesses under the pointer on every surface --- apps/app/src/anatomy/annotationPick.ts | 36 +++++++++ apps/app/src/catalogue/useEmptyTap.ts | 14 ++-- .../src/components/canvas/AnatomyCanvas.tsx | 10 +-- .../components/catalogue/CatalogueCanvas.tsx | 6 +- apps/app/src/editor/annotations/press.ts | 11 +-- .../src/editor/annotations/usePressGesture.ts | 4 +- apps/app/tests/setup.ts | 1 + .../tests/unit/anatomy.annotationPick.test.ts | 75 +++++++++++++++++++ apps/app/tests/unit/annotations.press.test.ts | 25 +++++-- .../unit/components.AnatomyCanvas.test.tsx | 19 +++++ apps/app/tests/unit/routes.catalogue.test.tsx | 13 ++++ docs/ANNOTATION-RENDERING.md | 10 ++- 12 files changed, 193 insertions(+), 31 deletions(-) create mode 100644 apps/app/src/anatomy/annotationPick.ts create mode 100644 apps/app/tests/unit/anatomy.annotationPick.test.ts diff --git a/apps/app/src/anatomy/annotationPick.ts b/apps/app/src/anatomy/annotationPick.ts new file mode 100644 index 00000000..9b7673c1 --- /dev/null +++ b/apps/app/src/anatomy/annotationPick.ts @@ -0,0 +1,36 @@ +const ANNOTATED = '[data-anno-id]' + +/** + * D11 (owner 2026-11-22): among the elements under the pointer, top first, the first + * annotation that is not an access wins; an access answers only when no other annotation is + * there. `within` bounds the search to one canvas (the bilan shows two: the page and the + * detail window). + */ +export function pickAnnotation( + elements: readonly Element[], + within: Element, +): Element | null { + let access: Element | null = null + for (const element of elements) { + const annotated = element.closest(ANNOTATED) + if (annotated === null || !within.contains(annotated)) continue + if (annotated.getAttribute('data-anno-kind') !== 'detail_trigger') { + return annotated + } + access ??= annotated + } + return access +} + +/** + * The same pick for a pointer event: its target first (the top element; a synthetic click + * with no coordinates still resolves), then every element under its point. + */ +export function annotationAt( + event: { target: EventTarget | null; clientX: number; clientY: number }, + within: Element, +): Element | null { + const top = event.target instanceof Element ? [event.target] : [] + const under = document.elementsFromPoint(event.clientX, event.clientY) + return pickAnnotation([...top, ...under], within) +} diff --git a/apps/app/src/catalogue/useEmptyTap.ts b/apps/app/src/catalogue/useEmptyTap.ts index e850e857..7e70f69d 100644 --- a/apps/app/src/catalogue/useEmptyTap.ts +++ b/apps/app/src/catalogue/useEmptyTap.ts @@ -1,9 +1,9 @@ import { useEffect, useMemo, useRef } from 'react' import type { PointerEvent, RefObject } from 'react' +import { annotationAt } from '@/anatomy/annotationPick' /** Same 4 px as the framing's drag threshold: more is a drag, not a tap. */ const TAP_SLOP_PX = 4 -const ANNOTATED = '[data-anno-id]' type Point = { x: number; y: number } @@ -70,11 +70,11 @@ function press(ref: Ref, e: PointerEvent): void { return } disarm(ref) - const target = e.target instanceof Element ? e.target : null + const box = e.currentTarget instanceof Element ? e.currentTarget : null const next: Press = { ...idle(), armed: e.button === 0, - onAnnotation: target?.closest(ANNOTATED) != null, + onAnnotation: box !== null && annotationAt(e, box) !== null, last: pointOf(e), pointerId: e.pointerId, } @@ -105,7 +105,7 @@ function verdict(ref: Ref, e: PointerEvent, enabled: boolean): TapVerdict { return 'now' } -function takeClick(ref: Ref, target: EventTarget | null): ClickAnswer { +function takeClick(ref: Ref, e: MouseEvent, within: Element): ClickAnswer { const p = ref.current if (p.swallowClick) { p.swallowClick = false @@ -113,9 +113,7 @@ function takeClick(ref: Ref, target: EventTarget | null): ClickAnswer { } if (!p.touchPending) return 'normal' p.touchPending = false - const onMarker = - target instanceof Element && target.closest(ANNOTATED) !== null - return onMarker ? 'normal' : 'deselect' + return annotationAt(e, within) !== null ? 'normal' : 'deselect' } /** @@ -137,7 +135,7 @@ export function useEmptyTap() { onPointerMove: (e: PointerEvent) => move(ref, e), onPointerCancel: () => disarm(ref), verdict: (e: PointerEvent, enabled = true) => verdict(ref, e, enabled), - takeClick: (target: EventTarget | null) => takeClick(ref, target), + takeClick: (e: MouseEvent, within: Element) => takeClick(ref, e, within), }), [], ) diff --git a/apps/app/src/components/canvas/AnatomyCanvas.tsx b/apps/app/src/components/canvas/AnatomyCanvas.tsx index abef59d5..8abb4c3b 100644 --- a/apps/app/src/components/canvas/AnatomyCanvas.tsx +++ b/apps/app/src/components/canvas/AnatomyCanvas.tsx @@ -1,4 +1,5 @@ import { useCallback, useEffect, useRef, useState } from 'react' +import { annotationAt } from '@/anatomy/annotationPick' import { buildViewSvg, type ViewFailure, type ViewStates } from '@/anatomy/drawing' import type { SheetIndex } from '@/services/sheetIndex' import { coreRuntime, useCoreStopped } from '@/core/runtime' @@ -27,11 +28,6 @@ const FAILURE_MESSAGE: Record = { view: 'canvas.view_unavailable', } -function annotatedTarget(event: Event): Element | null { - const target = event.target - return target instanceof Element ? target.closest(ANNOTATED) : null -} - /** * One view of the sheet: the drawing plus the annotation layer, composed by * the core (R3), rebuilt when the view changes and restyled in place when the @@ -90,7 +86,7 @@ export default function AnatomyCanvas({ const host = svgRef.current if (!host) return const onClick = (e: Event) => { - const id = annotatedTarget(e)?.getAttribute('data-anno-id') + const id = annotationAt(e as MouseEvent, host)?.getAttribute('data-anno-id') if (id) onZoneClick(id, viewId) } host.addEventListener('click', onClick) @@ -125,7 +121,7 @@ export default function AnatomyCanvas({ if (tooltipRef.current) tooltipRef.current.style.opacity = '0' } const onMove = (e: MouseEvent) => { - const el = annotatedTarget(e) + const el = annotationAt(e, host) const wrapper = wrapperRef.current if (!el || !wrapper || el.hasAttribute('data-anno-opens-view')) return hide() diff --git a/apps/app/src/components/catalogue/CatalogueCanvas.tsx b/apps/app/src/components/catalogue/CatalogueCanvas.tsx index e4cc57fe..23c2bc6a 100644 --- a/apps/app/src/components/catalogue/CatalogueCanvas.tsx +++ b/apps/app/src/components/catalogue/CatalogueCanvas.tsx @@ -1,3 +1,4 @@ +import { annotationAt } from '@/anatomy/annotationPick' import { useCallback, useEffect, useRef, useState, type PointerEvent } from 'react' import { usePlaceVisible } from '@/lib/placeVisibility' import Button from '@/components/ui/Button' @@ -128,11 +129,10 @@ export default function CatalogueCanvas({ if (!element) return const onClick = (event: Event) => { if (swallowClick()) return - const answer = onEmptyRef.current ? takeClick(event.target) : 'normal' + const answer = onEmptyRef.current ? takeClick(event as MouseEvent, element) : 'normal' if (answer === 'swallow') return if (answer === 'deselect') return onEmptyRef.current?.() - const target = event.target instanceof Element ? event.target.closest(ANNOTATED) : null - const id = target?.getAttribute('data-anno-id') + const id = annotationAt(event as MouseEvent, element)?.getAttribute('data-anno-id') if (id) onAnnotation(id) } element.addEventListener('click', onClick) diff --git a/apps/app/src/editor/annotations/press.ts b/apps/app/src/editor/annotations/press.ts index 41eea31d..25a8c930 100644 --- a/apps/app/src/editor/annotations/press.ts +++ b/apps/app/src/editor/annotations/press.ts @@ -37,7 +37,7 @@ export type HandleRef = /** One element under the press, top first (`elementsFromPoint`); handle centres in CSS px. */ export type Hit = | { kind: 'handle'; handle: HandleRef; centre: XY } - | { kind: 'annotation'; id: string } + | { kind: 'annotation'; id: string; access: boolean } const RANK: Record = { point: 0, @@ -73,8 +73,8 @@ export type PressTarget = | { kind: 'empty' } /** - * Board 32: the selection's handles, then the TOPMOST annotation (the visible wins), else - * nothing. + * Board 32: the selection's handles, then the topmost annotation that is not an access (D11: an + * access answers only where nothing else lies), else nothing. */ export function pressTarget( hits: readonly Hit[], @@ -83,8 +83,9 @@ export function pressTarget( ): PressTarget { const handle = nearestHandle(hits, at) if (handle) return { kind: 'handle', handle } - const top = hits.find((hit) => hit.kind === 'annotation') - if (top?.kind !== 'annotation') return { kind: 'empty' } + const annotations = hits.filter((hit) => hit.kind === 'annotation') + const top = annotations.find((hit) => !hit.access) ?? annotations[0] + if (!top) return { kind: 'empty' } return top.id === sel.selectedId ? { kind: 'interior', id: top.id } : { kind: 'other', id: top.id } diff --git a/apps/app/src/editor/annotations/usePressGesture.ts b/apps/app/src/editor/annotations/usePressGesture.ts index cf6af98d..f5bff3da 100644 --- a/apps/app/src/editor/annotations/usePressGesture.ts +++ b/apps/app/src/editor/annotations/usePressGesture.ts @@ -63,7 +63,9 @@ function hitOf(element: Element): Hit | null { return { kind: 'handle', handle: ref, centre: centreOf(element) } } const id = annotationIdOf(element) - return id === null ? null : { kind: 'annotation', id } + if (id === null) return null + const kind = element.closest('[data-anno-id]')?.getAttribute('data-anno-kind') + return { kind: 'annotation', id, access: kind === 'detail_trigger' } } /** Everything under the client point inside the canvas box, top first. */ diff --git a/apps/app/tests/setup.ts b/apps/app/tests/setup.ts index 7f6d47ce..21b8377b 100644 --- a/apps/app/tests/setup.ts +++ b/apps/app/tests/setup.ts @@ -19,6 +19,7 @@ function stubHitTesting(doc: Document): void { ) Object.defineProperty(doc, 'elementsFromPoint', { configurable: true, + writable: true, value: () => { const chain: Element[] = [] for (let el = pressed; el; el = el.parentElement) chain.push(el) diff --git a/apps/app/tests/unit/anatomy.annotationPick.test.ts b/apps/app/tests/unit/anatomy.annotationPick.test.ts new file mode 100644 index 00000000..9bae0722 --- /dev/null +++ b/apps/app/tests/unit/anatomy.annotationPick.test.ts @@ -0,0 +1,75 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' +import { annotationAt, pickAnnotation } from '@/anatomy/annotationPick' + +function canvas(html: string): HTMLElement { + const root = document.createElement('div') + root.innerHTML = html + document.body.append(root) + return root +} + +const find = (root: Element, id: string) => + root.querySelector(`[data-anno-id="${id}"]`)! + +const FRAGMENT = ` + + + + ` + +afterEach(() => { + document.body.replaceChildren() + vi.restoreAllMocks() +}) + +describe('pickAnnotation (D11)', () => { + it('a point under a trigger wins, whatever the stacking order', () => { + const root = canvas(FRAGMENT) + const trigger = root.querySelector('#t')! + const ring = root.querySelector('#ring')! + expect(pickAnnotation([trigger, ring], root)).toBe(find(root, 'point')) + expect(pickAnnotation([ring, trigger], root)).toBe(find(root, 'point')) + }) + + it('a trigger alone answers', () => { + const root = canvas(FRAGMENT) + const trigger = root.querySelector('#t')! + expect(pickAnnotation([trigger], root)).toBe(find(root, 'trigger')) + }) + + it('nothing annotated gives null', () => { + const root = canvas('') + expect(pickAnnotation([root.querySelector('#p')!], root)).toBeNull() + }) + + it('skips an annotation outside the canvas it is bounded to', () => { + const root = canvas(`
${FRAGMENT}
${FRAGMENT}
`) + const detail = root.querySelector('#detail')! + const outside = root.querySelector('#page #ring')! + const inside = detail.querySelector('#t')! + expect(pickAnnotation([outside, inside], detail)).toBe(find(detail, 'trigger')) + }) + + it('a twin without an id of its own resolves to its annotation', () => { + const root = canvas(``) + expect(pickAnnotation([root.querySelector('#twin')!], root)).toBe(find(root, 'zone')) + }) +}) + +describe('annotationAt', () => { + it('resolves through the target when nothing is under the point', () => { + const root = canvas(FRAGMENT) + vi.spyOn(document, 'elementsFromPoint').mockReturnValue([]) + const target = root.querySelector('#ring')! + expect(annotationAt({ target, clientX: 0, clientY: 0 }, root)).toBe(find(root, 'point')) + }) + + it('reads every element under the point, past a trigger target', () => { + const root = canvas(FRAGMENT) + const trigger = root.querySelector('#t')! + vi.spyOn(document, 'elementsFromPoint').mockReturnValue([trigger, root.querySelector('#ring')!]) + const picked = annotationAt({ target: trigger, clientX: 5, clientY: 6 }, root) + expect(picked).toBe(find(root, 'point')) + expect(document.elementsFromPoint).toHaveBeenCalledWith(5, 6) + }) +}) diff --git a/apps/app/tests/unit/annotations.press.test.ts b/apps/app/tests/unit/annotations.press.test.ts index a5291dc8..fb4ee821 100644 --- a/apps/app/tests/unit/annotations.press.test.ts +++ b/apps/app/tests/unit/annotations.press.test.ts @@ -75,7 +75,7 @@ describe('nearestHandle (R-9)', () => { it('answers null without a handle', () => { expect( - nearestHandle([{ kind: 'annotation', id: 'a' }], { x: 0, y: 0 }), + nearestHandle([{ kind: 'annotation', id: 'a', access: false }], { x: 0, y: 0 }), ).toBeNull() }) }) @@ -84,8 +84,8 @@ describe('pressTarget (board 32)', () => { const at = { x: 0, y: 0 } it('a selected zone hit first, a point hitbox under it: the interior', () => { const hits: Hit[] = [ - { kind: 'annotation', id: 'zone' }, - { kind: 'annotation', id: 'point' }, + { kind: 'annotation', id: 'zone', access: false }, + { kind: 'annotation', id: 'point', access: false }, ] expect(pressTarget(hits, at, ZONE)).toEqual({ kind: 'interior', @@ -95,15 +95,28 @@ describe('pressTarget (board 32)', () => { it('a point twin hit first, the selected zone under it: the other one', () => { const hits: Hit[] = [ - { kind: 'annotation', id: 'point' }, - { kind: 'annotation', id: 'zone' }, + { kind: 'annotation', id: 'point', access: false }, + { kind: 'annotation', id: 'zone', access: false }, ] expect(pressTarget(hits, at, ZONE)).toEqual({ kind: 'other', id: 'point' }) }) + it('an access hit first, an annotation under it: the annotation (D11)', () => { + const hits: Hit[] = [ + { kind: 'annotation', id: 'access', access: true }, + { kind: 'annotation', id: 'point', access: false }, + ] + expect(pressTarget(hits, at, ZONE)).toEqual({ kind: 'other', id: 'point' }) + }) + + it('an access alone is still the target', () => { + const hits: Hit[] = [{ kind: 'annotation', id: 'access', access: true }] + expect(pressTarget(hits, at, ZONE)).toEqual({ kind: 'other', id: 'access' }) + }) + it('a handle wins over every annotation; nothing is empty', () => { const hits: Hit[] = [ - { kind: 'annotation', id: 'point' }, + { kind: 'annotation', id: 'point', access: false }, { kind: 'handle', handle: { kind: 'vertex', index: 1 }, centre: at }, ] expect(pressTarget(hits, at, ZONE)).toEqual({ diff --git a/apps/app/tests/unit/components.AnatomyCanvas.test.tsx b/apps/app/tests/unit/components.AnatomyCanvas.test.tsx index cd1b5db3..5eceb481 100644 --- a/apps/app/tests/unit/components.AnatomyCanvas.test.tsx +++ b/apps/app/tests/unit/components.AnatomyCanvas.test.tsx @@ -94,6 +94,25 @@ describe('AnatomyCanvas', () => { expect(onZoneClick).toHaveBeenCalledTimes(1) }) + it('an annotation under an access wins the click and the tooltip (D11)', async () => { + useLocaleStore.setState({ locale: 'fr' }) + const onZoneClick = vi.fn() + const { container } = render( + , + ) + await waitFor(() => expect(container.querySelector('[data-anno-id="head-trigger"]')).not.toBeNull()) + const trigger = container.querySelector('[data-anno-id="head-trigger"]')! + const hip = container.querySelector('circle[data-anno-id="hip"]')! + vi.spyOn(document, 'elementsFromPoint').mockReturnValue([trigger, hip]) + trigger.dispatchEvent(new Event('click', { bubbles: true })) + expect(onZoneClick).toHaveBeenCalledWith('hip', 'main') + trigger.dispatchEvent(new MouseEvent('mousemove', { bubbles: true })) + const name = hip.getAttribute('data-anno-name-fr') + expect(name).toBeTruthy() + expect(container.textContent).toContain(name) + vi.restoreAllMocks() + }) + it('applies the states right after the build, on both elements of a point', async () => { const { container } = render( { expect(canvas.querySelector('[data-anno-id="thoracic"]')).toHaveAttribute('data-state', 'current') }) + it('selects the point under an access when both lie at the click (D11)', async () => { + mount(`/app/anatomy/${SHEET}`) + const canvas = await screen.findByTestId('catalogue-canvas-drawing') + await waitFor(() => expect(canvas.querySelector('[data-anno-id="head-trigger"]')).not.toBeNull()) + const trigger = canvas.querySelector('[data-anno-id="head-trigger"]')! + const thoracic = canvas.querySelector('[data-anno-id="thoracic"]')! + const stub = vi.spyOn(globalThis.document, 'elementsFromPoint').mockReturnValue([trigger, thoracic]) + fireEvent.click(trigger) + stub.mockRestore() + expect(screen.getByRole('heading', { name: 'Zone : Vertèbres thoraciques' })).toBeInTheDocument() + expect(screen.getByRole('tab', { name: 'Vue latérale gauche' })).toHaveAttribute('aria-selected', 'true') + }) + it('opens the detail view from its link on the drawing', async () => { mount(`/app/anatomy/${SHEET}`) const canvas = await screen.findByTestId('catalogue-canvas-drawing') diff --git a/docs/ANNOTATION-RENDERING.md b/docs/ANNOTATION-RENDERING.md index 908f9598..7400d138 100644 --- a/docs/ANNOTATION-RENDERING.md +++ b/docs/ANNOTATION-RENDERING.md @@ -65,7 +65,15 @@ annotation id to the core's click rule (`zone.click`, R2). In the editor the press machine (`press.ts`, U2) lists the elements under the pointer (`elementsFromPoint`), reads the nearest `[data-anno-id]` of each and decides one action for the whole press. Behaviour never depends on a label, an element -id or a class. The canvas sets and removes +id or a class. + +An access (kind `detail_trigger`) never hides another annotation (D11). On the +editor, the catalogue and the bilan, the page reads every element under the +pointer (`elementsFromPoint`, bounded to the canvas, `anatomy/annotationPick.ts`) +and takes the first annotation that is not an access; an access answers only +where no other annotation lies. The same pick drives the click and the tooltip. +The paint order of the core is unchanged. The access labels (Part 6) are page +buttons over the drawing, outside the layer. The canvas sets and removes `data-state` in place when the active observation changes (no rebuild), for the annotation ids of every zone of that observation, whatever their view; the custom tooltip reads the two name attributes (none on a trigger).