fix(editor): one press is either a pan or a click

useFraming takes an optional panning gate read on each one-pointer move;
the editor passes the press machine's drag-with-pan phase, so a zoomed
pen within its 6 px slop or a mouse wiggle no longer pans and clicks.
Catalogue: no options, behaviour unchanged.

Test added: « a press is either a pan or a click, never both (R-8) »
(pen 5 px clicks without pan, pen 8 px pans without click, mouse wiggle
clicks without pan). No assertion removed.
This commit is contained in:
bchanot committed 2026-11-17 17:10:45 +01:00
1 parent 35e4a4c75a
commit b2d78f9621
4 files changed
+83 -21

No files matched your search

+20 -1
View File
@@ -25,6 +25,11 @@ type Point = { x: number; y: number }
export interface FramingOptions {
/** Asked on a one-pointer press; false leaves that drag to the caller (a drawing tool). */
canPan?: (event: PointerEvent) => boolean
/**
* Asked on each one-pointer move: when given, it alone decides whether the move pans (the
* caller tells a drag from a click), replacing the 4 px threshold and `canPan`.
*/
panning?: () => boolean
}
function boxOf(
@@ -43,6 +48,15 @@ function distance(a: Point, b: Point): number {
return Math.hypot(a.x - b.x, a.y - b.y)
}
/** One-pointer pan: the caller's gate when it gives one, else past the threshold and allowed. */
function pansNow(
gate: (() => boolean) | undefined,
dragged: RefObject<boolean>,
allowed: RefObject<boolean>,
): boolean {
return gate ? gate() : dragged.current && allowed.current
}
/**
* Zoom and pan of the canvas box: buttons, a drag with one pointer, a pinch
* with two, the wheel (pans when zoomed; with Ctrl, as a trackpad pinch
@@ -65,6 +79,8 @@ export function useFraming(
const panAllowed = useRef(true)
const canPan = useRef(options.canPan)
canPan.current = options.canPan
const panning = useRef(options.panning)
panning.current = options.panning
useEffect(() => setFraming(FIT), [resetKey])
useEffect(() => {
@@ -135,7 +151,10 @@ export function useFraming(
const factor =
distance(now, other) / Math.max(distance(before, other), 1)
zoom(factor, at)
} else if (others.length === 0 && dragged.current && panAllowed.current) {
} else if (
others.length === 0 &&
pansNow(panning.current, dragged, panAllowed)
) {
event.currentTarget.setPointerCapture?.(event.pointerId)
pan(now.x - before.x, now.y - before.y)
}
@@ -1,6 +1,5 @@
import type { CSSProperties, KeyboardEvent, PointerEvent } from 'react'
import { useFraming } from '@/catalogue/useFraming'
import { dragAction, pointerKindOf } from './press'
import type { ToolId } from './uiState'
import { usePressGesture, type PressInput } from './usePressGesture'
@@ -60,15 +59,9 @@ function touchStyle(
export function useCanvasGestures(input: GestureInput) {
const press = usePressGesture(input)
const framing = useFraming(input.viewport, input.viewId, {
// A one-pointer pan only where the press machine would pan (a mouse or pen off the
// movable selection), decided once at pointer-down.
canPan: (event) =>
input.tool === 'select' &&
dragAction(press.target(), {
kind: pointerKindOf(event.pointerType),
movable: input.target !== null,
moveMode: false,
}).type === 'pan',
// The press machine decides: the drawing pans only while the press is a pan, so one
// press is either a pan or a click, never both (R-8).
panning: () => input.tool === 'select' && press.panning(),
})
const onKeyDown = (event: KeyboardEvent) => {
if (event.target !== event.currentTarget) return
@@ -120,7 +120,7 @@ function runClick(action: ClickAction, input: PressInput): void {
interface Machine {
press: Press
/** The press's target, read by the framing's pan decision at pointer-down. */
/** The press's target, kept for the capture decision at pointer-down. */
target: PressTarget
/** The pointers pressed on the box; one pressed outside it is never counted (no release). */
pointers: Set<number>
@@ -285,7 +285,7 @@ const IDLE: Press = { phase: 'idle' }
/**
* The editor's one decider for the drawing (U2 R-8): every pointer event goes through the
* press machine, whose effects select, deselect, place with a drawing tool, or move the
* selection as a block. `target()` is the press's target, for the framing's pan decision.
* selection as a block. `panning()` gates the framing's one-pointer pan.
*/
export function usePressGesture(input: PressInput) {
const drag = useBlockDrag(input)
@@ -306,7 +306,10 @@ export function usePressGesture(input: PressInput) {
const cancel = () => feed(latest.current, { type: 'cancel' }, null)
return {
preview: drag.preview,
target: () => machine.current.target,
panning: () => {
const press = machine.current.press
return press.phase === 'drag' && press.action.type === 'pan'
},
handlers: {
onPointerDown: (event: PointerEvent) => onPointerDown(latest, event),
onPointerMove: (event: PointerEvent) =>
@@ -1092,13 +1092,11 @@ describe('« Désélectionner » (R1, A6)', () => {
}),
})
const panel = await selectFromList()
await userEvent
.setup()
.click(
within(panel).getByRole('button', {
name: 'Remplacer par un polygone',
}),
)
await userEvent.setup().click(
within(panel).getByRole('button', {
name: 'Remplacer par un polygone',
}),
)
const open = await screen.findByTestId('annotation-properties')
expect(
within(open).queryByRole('button', { name: 'Désélectionner' }),
@@ -1461,6 +1459,55 @@ describe('a tap on the drawing (D6b)', () => {
expect(marker('anno-b')).not.toHaveAttribute('data-state')
})
it('a press is either a pan or a click, never both (R-8)', async () => {
await selected()
vi.spyOn(box(), 'getBoundingClientRect').mockReturnValue({
...{ x: 0, y: 0, left: 0, top: 0, right: 400, bottom: 300 },
...{ width: 400, height: 300, toJSON: () => ({}) },
})
await userEvent
.setup()
.click(screen.getByRole('button', { name: 'Zoom avant' }))
const transform = () => drawing().parentElement!.style.transform
/** A press at (10, 10) moved through the given x offsets, released at the last. */
const press = (type: string, path: number[]) => {
fireEvent.pointerDown(rect(), {
...init(1, type),
clientX: 10,
clientY: 10,
})
for (const dx of path) {
fireEvent.pointerMove(rect(), {
...init(1, type),
clientX: 10 + dx,
clientY: 10,
})
}
const last = 10 + (path.at(-1) ?? 0)
fireEvent.pointerUp(rect(), {
...init(1, type),
clientX: last,
clientY: 10,
})
}
const zoomed = transform()
expect(zoomed).toContain('scale(1.5)')
// A pen within its 6 px slop: a click (it deselects), no pan.
press('pen', [5])
expect(transform()).toBe(zoomed)
expect(isDeselected()).toBe(true)
// A pen past its slop: a pan, no click.
await selectFromList()
press('pen', [4, 8])
expect(transform()).not.toBe(zoomed)
expect(isDeselected()).toBe(false)
// A mouse wiggling 14 px of path, never 4 px away from the press: a click, no pan.
const panned = transform()
press('mouse', [3, -3, 2])
expect(transform()).toBe(panned)
expect(isDeselected()).toBe(true)
})
it('a finger drags the selected point (its handle), never a selected zone (A13)', async () => {
const zone = zoneAnnotation({ type: 'polygon', points: triangle })
await selected({