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

+13 -6
View File
@@ -22,6 +22,11 @@ export function focusRowOrDrawing(id: string): boolean {
return focusRow({ kind: 'annotation', id }) || focusDrawing()
}
/** After a tap on the drawing: the box takes the focus, unless it already holds it. */
function drawingFocus(): void {
if (visibleElement(CANVAS_BOX) !== document.activeElement) focusDrawing()
}
/**
* Deselecting: `id` is the RESOLVED selection (never the raw id, which can name a removed
* annotation). The button always moves the focus to the row; Escape does only when it was
@@ -33,17 +38,18 @@ export function useDeselect(id: string | null, clear: () => void) {
const tRef = useRef(t)
tRef.current = t
const announce = useContext(AnnounceContext)
const run = (focus: boolean) => {
const run = (focus: ((id: string) => unknown) | null) => {
if (id === null) return false
commitFocusedField()
flushSync(clear)
if (focus) focusRowOrDrawing(id)
focus?.(id)
announce(tRef.current('editor.annotations.deselected'))
return true
}
return {
byButton: () => run(true),
byKey: (fromPanel: boolean) => run(fromPanel),
byButton: () => run(focusRowOrDrawing),
byKey: (fromPanel: boolean) => run(fromPanel ? focusRowOrDrawing : null),
byDrawing: () => run(drawingFocus),
}
}
@@ -82,7 +88,8 @@ export function useEscapeDeselect(
/**
* A body's Escape: deselects `id` while `active`. Returns the ref of the panel column, which
* tells a key pressed inside the panel (focus follows the row) from one pressed elsewhere.
* tells a key pressed inside the panel (focus follows the row) from one pressed elsewhere,
* and the tap-on-the-drawing deselect.
*/
export function useSelectionEscape(
id: string | null,
@@ -94,5 +101,5 @@ export function useSelectionEscape(
useEscapeDeselect(active, (event) =>
deselect.byKey(asideRef.current?.contains(event.target as Node) ?? false),
)
return asideRef
return { asideRef, byDrawing: deselect.byDrawing }
}
@@ -6,6 +6,7 @@ import type {
RefObject,
} from 'react'
import type { Geometry } from '@faunosteo/core-wasm/types'
import { useEmptyTap } from '@/catalogue/useEmptyTap'
import { useFraming } from '@/catalogue/useFraming'
import type { XY } from './frame'
import { clientToDrawing } from './gesture'
@@ -23,15 +24,23 @@ export interface GestureInput {
/** A click with a drawing tool, in drawing units. */
onPlace: (at: XY) => void
onDrop: (id: string, geometry: Geometry) => void
/** A tap on the empty drawing with the select tool. */
onDeselect: () => void
/** Enter and Escape while the canvas box itself has the focus (a tracing). */
onTraceKey: (event: KeyboardEvent) => void
}
type Framing = ReturnType<typeof useFraming>
type Move = ReturnType<typeof useMoveGesture>
type Tap = ReturnType<typeof useEmptyTap>
/** Each pointer handler of the framing and of the move gesture, one after the other. */
function composedPointer(framing: Framing, move: Move) {
function composedPointer(
framing: Framing,
move: Move,
tap: Tap,
input: GestureInput,
) {
const both =
(
name:
@@ -45,18 +54,38 @@ function composedPointer(framing: Framing, move: Move) {
move.handlers[name](event)
}
return {
onPointerDown: both('onPointerDown'),
onPointerMove: both('onPointerMove'),
onPointerUp: both('onPointerUp'),
onPointerCancel: both('onPointerCancel'),
onPointerDown: (event: PointerEvent) => {
both('onPointerDown')(event)
tap.onPointerDown(event)
},
onPointerMove: (event: PointerEvent) => {
both('onPointerMove')(event)
tap.onPointerMove(event)
},
onPointerUp: (event: PointerEvent) => {
both('onPointerUp')(event)
// Every press ends in a verdict, whatever the tool: it disarms the tracker.
if (tap.verdict(event, input.tool === 'select') === 'now')
input.onDeselect()
},
onPointerCancel: (event: PointerEvent) => {
both('onPointerCancel')(event)
tap.onPointerCancel()
},
onLostPointerCapture: move.handlers.onLostPointerCapture,
}
}
/** A click selects with the select tool, places with a drawing tool; a drag swallows it. */
function clickHandler(input: GestureInput, framing: Framing) {
function clickHandler(input: GestureInput, framing: Framing, tap: Tap) {
return (event: MouseEvent) => {
if (framing.swallowClick()) return
const answer = tap.takeClick(event.target)
if (answer === 'swallow') return
if (answer === 'deselect') {
input.onDeselect()
return
}
if (input.tool === 'select') {
const id = annotationIdOf(event.target)
if (id) input.onSelect(id)
@@ -97,6 +126,7 @@ export function useCanvasGestures(input: GestureInput) {
const framing = useFraming(input.viewport, input.viewId, {
canPan: (event) => input.tool === 'select' && !move.claims(event),
})
const tap = useEmptyTap()
const onKeyDown = (event: KeyboardEvent) => {
framing.handlers.onKeyDown(event)
if (event.target === event.currentTarget) input.onTraceKey(event)
@@ -105,9 +135,10 @@ export function useCanvasGestures(input: GestureInput) {
framing,
drag: move.preview,
boxProps: {
...composedPointer(framing, move),
...composedPointer(framing, move, tap, input),
onContextMenu: tap.onPointerCancel,
onKeyDown,
onClick: clickHandler(input, framing),
onClick: clickHandler(input, framing, tap),
style: touchStyle(
input.tool,
move.armed,