From bbdfd27311b0c6d1a8066cf27576c4d96209299d Mon Sep 17 00:00:00 2001 From: bchanot Date: Thu, 12 Nov 2026 12:51:59 +0100 Subject: [PATCH] feat(app): a click on the drawing deselects (R1, D6b) The decision comes from the press (landed off a marker, moved <= 4 px, one pointer), not the click target: mouse and pen act at pointerup, touch in the tap's own click. The reader passes no onEmpty and is unchanged. No existing assertion rewritten. --- apps/app/src/catalogue/useEmptyTap.ts | 136 +++++++ .../components/catalogue/CatalogueCanvas.tsx | 36 +- .../editor/annotations/AnnotationCanvas.tsx | 1 + .../annotations/EditableAnnotations.tsx | 19 +- .../components/editor/tabs/AnnotationsTab.tsx | 3 +- apps/app/src/editor/annotations/deselect.ts | 19 +- .../editor/annotations/useCanvasGestures.ts | 47 ++- .../unit/editor-annotations-tab.test.tsx | 332 ++++++++++++++++++ apps/app/tests/unit/routes.catalogue.test.tsx | 14 + 9 files changed, 586 insertions(+), 21 deletions(-) create mode 100644 apps/app/src/catalogue/useEmptyTap.ts diff --git a/apps/app/src/catalogue/useEmptyTap.ts b/apps/app/src/catalogue/useEmptyTap.ts new file mode 100644 index 00000000..95031234 --- /dev/null +++ b/apps/app/src/catalogue/useEmptyTap.ts @@ -0,0 +1,136 @@ +import { useCallback, useEffect, useRef } from 'react' +import type { PointerEvent } from 'react' + +/** 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 } + +/** What a pointer-up decides: act now (mouse, pen), at the click (touch), or not at all. */ +export type TapVerdict = 'none' | 'now' | 'on_click' +/** What the click that follows must do: nothing, deselect, or its usual work. */ +export type ClickAnswer = 'swallow' | 'deselect' | 'normal' + +interface Press { + armed: boolean + onAnnotation: boolean + moved: number + multi: boolean + last: Point + touchPending: boolean + swallowClick: boolean +} + +const idle = (): Press => ({ + armed: false, + onAnnotation: false, + moved: 0, + multi: false, + last: { x: 0, y: 0 }, + touchPending: false, + swallowClick: false, +}) + +const pointOf = (e: PointerEvent): Point => ({ x: e.clientX, y: e.clientY }) + +/** + * Tells a tap on the empty drawing from every other gesture. The decision is taken from + * the PRESS (where it landed, whether it moved, whether a second pointer joined), never + * from the click's target: the move gesture captures the pointer on the box, which + * retargets the click of a press on the selected marker; and on touch the browser builds + * the click after the lift, on the layout without the selection, and may adjust it toward + * a nearby marker. Mouse and pen act at the pointer-up (a blur commit at mousedown can + * rebuild the drawing and drop the click); touch acts in the tap's own click. + * Refs only: no render follows a press. + */ +export function useEmptyTap() { + const press = useRef(idle()) + const pointerId = useRef(null) + const stopListening = useRef<(() => void) | null>(null) + + const release = useCallback(() => { + stopListening.current?.() + stopListening.current = null + }, []) + const disarm = useCallback(() => { + press.current.armed = false + release() + }, [release]) + useEffect(() => release, [release]) + + const onPointerDown = useCallback( + (e: PointerEvent) => { + if (!e.isPrimary) { + press.current.multi = true + return + } + release() + pointerId.current = e.pointerId + const target = e.target instanceof Element ? e.target : null + press.current = { + ...idle(), + armed: e.button === 0, + onAnnotation: target?.closest(ANNOTATED) != null, + last: pointOf(e), + } + if (!press.current.armed) return + // A second finger can land outside the box: only the document sees it. + const other = (ev: Event) => { + if ((ev as globalThis.PointerEvent).pointerId !== pointerId.current) { + press.current.multi = true + } + } + document.addEventListener('pointerdown', other, true) + stopListening.current = () => + document.removeEventListener('pointerdown', other, true) + }, + [release], + ) + + const onPointerMove = useCallback((e: PointerEvent) => { + const p = press.current + if (!e.isPrimary || !p.armed) return + const now = pointOf(e) + p.moved += Math.hypot(now.x - p.last.x, now.y - p.last.y) + p.last = now + }, []) + + const verdict = useCallback( + (e: PointerEvent, enabled = true): TapVerdict => { + const p = press.current + const still = p.moved <= TAP_SLOP_PX && !p.multi + const tap = enabled && e.isPrimary && p.armed && still && !p.onAnnotation + if (e.isPrimary) disarm() + if (!tap) return 'none' + if (e.pointerType === 'touch') { + p.touchPending = true + return 'on_click' + } + p.swallowClick = true + return 'now' + }, + [disarm], + ) + + const takeClick = useCallback((target: EventTarget | null): ClickAnswer => { + const p = press.current + if (p.swallowClick) { + p.swallowClick = false + return 'swallow' + } + if (!p.touchPending) return 'normal' + p.touchPending = false + const onMarker = + target instanceof Element && target.closest(ANNOTATED) !== null + return onMarker ? 'normal' : 'deselect' + }, []) + + return { + onPointerDown, + onPointerMove, + onPointerCancel: disarm, + verdict, + takeClick, + } +} diff --git a/apps/app/src/components/catalogue/CatalogueCanvas.tsx b/apps/app/src/components/catalogue/CatalogueCanvas.tsx index 86559b78..994fb177 100644 --- a/apps/app/src/components/catalogue/CatalogueCanvas.tsx +++ b/apps/app/src/components/catalogue/CatalogueCanvas.tsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useRef, useState } from 'react' +import { useCallback, useEffect, useRef, useState, type PointerEvent } from 'react' import { usePlaceVisible } from '@/lib/placeVisibility' import Button from '@/components/ui/Button' import Notice from '@/components/ui/Notice' @@ -6,6 +6,7 @@ import { coreRuntime, useCoreStopped } from '@/core/runtime' import { coreFailureMessage } from '@/core/failure' import type { CataloguePreview } from '@/catalogue/preview' import { transformOf } from '@/catalogue/framing' +import { useEmptyTap } from '@/catalogue/useEmptyTap' import { useFraming } from '@/catalogue/useFraming' import { useT } from '@/i18n/useT' @@ -15,6 +16,8 @@ export interface CatalogueCanvasProps { viewName: string selectedId: string | null onAnnotation: (annotationId: string) => void + /** A tap on the empty drawing; without it the canvas behaves as the reader always did. */ + onEmpty?: () => void } type CanvasFailure = 'engine' | 'view' @@ -54,6 +57,7 @@ export default function CatalogueCanvas({ viewName, selectedId, onAnnotation, + onEmpty, }: CatalogueCanvasProps) { const t = useT() const viewport = useRef(null) @@ -66,6 +70,29 @@ export default function CatalogueCanvas({ viewport, viewId, ) + const tap = useEmptyTap() + const { takeClick } = tap + const emptyHandlers = onEmpty + ? { + onPointerDown: (e: PointerEvent) => { + handlers.onPointerDown(e) + tap.onPointerDown(e) + }, + onPointerMove: (e: PointerEvent) => { + handlers.onPointerMove(e) + tap.onPointerMove(e) + }, + onPointerUp: (e: PointerEvent) => { + handlers.onPointerUp(e) + if (tap.verdict(e) === 'now') onEmpty() + }, + onPointerCancel: (e: PointerEvent) => { + handlers.onPointerCancel(e) + tap.onPointerCancel() + }, + onContextMenu: tap.onPointerCancel, + } + : handlers const retryEngine = useCallback(() => { void coreRuntime() @@ -98,13 +125,16 @@ export default function CatalogueCanvas({ if (!element) return const onClick = (event: Event) => { if (swallowClick()) return + const answer = onEmpty ? takeClick(event.target) : 'normal' + if (answer === 'swallow') return + if (answer === 'deselect') return onEmpty?.() const target = event.target instanceof Element ? event.target.closest(ANNOTATED) : null const id = target?.getAttribute('data-anno-id') if (id) onAnnotation(id) } element.addEventListener('click', onClick) return () => element.removeEventListener('click', onClick) - }, [onAnnotation, swallowClick]) + }, [onAnnotation, onEmpty, swallowClick, takeClick]) return (
@@ -118,7 +148,7 @@ export default function CatalogueCanvas({ data-testid="catalogue-canvas" className="svg-canvas aspect-[4/3] w-full cursor-grab select-none focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-[var(--color-accent)] sm:aspect-[3/2]" style={{ touchAction: framing.scale === 1 ? 'pan-x pan-y' : 'none' }} - {...handlers} + {...emptyHandlers} >
void onDrop: (id: string, geometry: Geometry) => void + onDeselect: () => void onTraceKey: (event: KeyboardEvent) => void } diff --git a/apps/app/src/components/editor/annotations/EditableAnnotations.tsx b/apps/app/src/components/editor/annotations/EditableAnnotations.tsx index 1a99be47..3fe4f6bf 100644 --- a/apps/app/src/components/editor/annotations/EditableAnnotations.tsx +++ b/apps/app/src/components/editor/annotations/EditableAnnotations.tsx @@ -133,14 +133,18 @@ export default function EditableAnnotations(props: Props) { const placement = usePlacement({ ...scene, context }) const { ui, selected } = scene const visible = usePlaceVisible() - const asideRef = useSelectionEscape( + const { asideRef, byDrawing } = useSelectionEscape( selected?.id ?? null, () => scene.dispatch({ type: 'select', id: null }), visible && selected !== undefined && ui.tracing === null && !ui.pending, ) return (
- + @@ -153,9 +157,11 @@ type Placement = ReturnType function CanvasColumn({ scene, placement, + onDeselect, }: { scene: Scene placement: Placement + onDeselect: () => void }) { const { doc, ui, dispatch, model, selected } = scene const noCategory = doc.categories.length === 0 @@ -175,7 +181,11 @@ function CanvasColumn({ navigate={scene.navigate} /> {selected && } - + {ui.tracing && ( void }) { const locale = useLocaleStore((s) => s.locale) // Hidden (the editor is read-only): no drag in flight survives, none is committed. @@ -240,6 +252,7 @@ function CanvasStage({ onSelect={(id) => dispatch({ type: 'select', id })} onPlace={placement.onPlace} onDrop={placement.onDrop} + onDeselect={onDeselect} onTraceKey={placement.onTraceKey} /> ) diff --git a/apps/app/src/components/editor/tabs/AnnotationsTab.tsx b/apps/app/src/components/editor/tabs/AnnotationsTab.tsx index 41dd56d7..74ada879 100644 --- a/apps/app/src/components/editor/tabs/AnnotationsTab.tsx +++ b/apps/app/src/components/editor/tabs/AnnotationsTab.tsx @@ -166,7 +166,7 @@ function ReadOnlyAnnotations({ const view = doc.views.find((v) => v.id === viewId) const selected = model.annotations.find((a) => a.id === selectedId) const visible = usePlaceVisible() - const asideRef = useSelectionEscape( + const { asideRef, byDrawing } = useSelectionEscape( selected?.id ?? null, () => setSelectedId(null), visible && selected !== undefined, @@ -195,6 +195,7 @@ function ReadOnlyAnnotations({ viewName={view.name[locale]} selectedId={selectedId} onAnnotation={setSelectedId} + onEmpty={byDrawing} />