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)
}