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.
This commit is contained in:
bchanot committed 2026-11-12 12:51:59 +01:00
1 parent bbb1668406
commit bbdfd27311
9 files changed
+586 -21

No files matched your search

+136
View File
@@ -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<Press>(idle())
const pointerId = useRef<number | null>(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,
}
}