refactor(editor): one press machine for clicks, drags and taps on the drawing

press.ts (pure: slop per pointer kind, nearest handle, press target, click
and drag actions, double click, stepPress) is now the editor canvas's only
decider; useCanvasGestures no longer uses clickHandler, useEmptyTap,
swallowClick nor useMoveGesture (deleted), and the box has no onClick.
canPan = dragAction(press target).type === 'pan' at pointer-down; framing
keys only when the box itself is the target. workStore.ts holds the U2
work with stale stamps: dropStale on every documentText change, reset on
a new session (editorStore subscription). The catalogue keeps useFraming
defaults and useEmptyTap, unchanged.

Named change (A13): a finger drag on a selected zone's (or circle link's)
interior moves nothing; the selected point is its own handle and still
moves by any pointer. Second observable change: touch and pen taps are
decided at the pointer-up from the press target, no longer from the
following click (no browser touch adjustment toward a marker).

tests/setup.ts: happy-dom has no elementsFromPoint; a shim answers the
pointerdown target and its ancestors.

Rewritten tests (editor-annotations-tab.test.tsx, D6b):
- "a touch tap deselects in its own click, not at the pointer-up" ->
  "a touch tap deselects at the pointer-up" (deselected after pointerup)
- "a touch tap whose click lands on a marker (adjustment) selects that
  marker" -> "a touch tap is decided by its press: a click adjusted toward
  a marker selects nothing" (deselected, announced, marker b not current)
- "a pen tap deselects in its own click, like a touch tap" -> "a pen tap
  deselects at the pointer-up, like a mouse tap"
- "a pen tap whose click lands on a marker selects that marker" -> "a pen
  tap is decided by its press: a click landing on a marker selects
  nothing"
Added: "a finger drags the selected point (its handle), never a selected
zone (A13)".
Drive-only changes, assertions unchanged: canvas fireEvent.click(x, y)
-> pressAt (pointerdown + pointerup) in the tab suite (C26, tracing,
paths, H3, H6, H8, filter reselect), editor-keep-alive and
editor-sections; drag moves carry buttons: 1 (C34, polygon block move,
H8 lostpointercapture and read-only, editor-no-loss drag in flight),
since a move without a button is a hover.
This commit is contained in:
bchanot committed 2026-11-17 16:56:35 +01:00
1 parent 5ca2b7e3ad
commit 08d266c89f
18 files changed
+1341 -311

No files matched your search

@@ -0,0 +1,345 @@
/** U2 Task 2: the press machine, the editor's one decider for clicks, drags and taps. */
import { describe, expect, it } from 'vitest'
import {
clickAction,
dragAction,
isDoubleClick,
nearestHandle,
pressTarget,
stepPress,
type DragContext,
type Hit,
type Press,
type PressEvent,
type PressTarget,
type Selection,
} from '@/editor/annotations/press'
const NONE: Selection = { selectedId: null, chosenVertex: null }
const ZONE: Selection = { selectedId: 'zone', chosenVertex: null }
describe('nearestHandle (R-9)', () => {
it('takes the nearest of three overlapping hit circles (an 11 px segment)', () => {
const hits: Hit[] = [
{
kind: 'handle',
handle: { kind: 'vertex', index: 0 },
centre: { x: 0, y: 0 },
},
{
kind: 'handle',
handle: { kind: 'insert', after: 0 },
centre: { x: 5.5, y: 0 },
},
{
kind: 'handle',
handle: { kind: 'vertex', index: 1 },
centre: { x: 11, y: 0 },
},
]
expect(nearestHandle(hits, { x: 9, y: 0 })).toEqual({
kind: 'vertex',
index: 1,
})
expect(nearestHandle(hits, { x: 5, y: 1 })).toEqual({
kind: 'insert',
after: 0,
})
expect(nearestHandle(hits, { x: 1, y: 0 })).toEqual({
kind: 'vertex',
index: 0,
})
})
it('at a tie a vertex beats « − », which beats « + »', () => {
const at = { x: 0, y: 0 }
const remove: Hit = {
kind: 'handle',
handle: { kind: 'remove', index: 2 },
centre: { x: 3, y: 0 },
}
const insert: Hit = {
kind: 'handle',
handle: { kind: 'insert', after: 2 },
centre: { x: -3, y: 0 },
}
const vertex: Hit = {
kind: 'handle',
handle: { kind: 'vertex', index: 2 },
centre: { x: 0, y: 3 },
}
expect(nearestHandle([insert, remove, vertex], at)).toEqual(vertex.handle)
expect(nearestHandle([insert, remove], at)).toEqual(remove.handle)
expect(nearestHandle([remove, insert], at)).toEqual(remove.handle)
})
it('answers null without a handle', () => {
expect(
nearestHandle([{ kind: 'annotation', id: 'a' }], { x: 0, y: 0 }),
).toBeNull()
})
})
describe('pressTarget (board 32)', () => {
const at = { x: 0, y: 0 }
it('a selected zone hit first, a point hitbox under it: the interior', () => {
const hits: Hit[] = [
{ kind: 'annotation', id: 'zone' },
{ kind: 'annotation', id: 'point' },
]
expect(pressTarget(hits, at, ZONE)).toEqual({
kind: 'interior',
id: 'zone',
})
})
it('a point twin hit first, the selected zone under it: the other one', () => {
const hits: Hit[] = [
{ kind: 'annotation', id: 'point' },
{ kind: 'annotation', id: 'zone' },
]
expect(pressTarget(hits, at, ZONE)).toEqual({ kind: 'other', id: 'point' })
})
it('a handle wins over every annotation; nothing is empty', () => {
const hits: Hit[] = [
{ kind: 'annotation', id: 'point' },
{ kind: 'handle', handle: { kind: 'vertex', index: 1 }, centre: at },
]
expect(pressTarget(hits, at, ZONE)).toEqual({
kind: 'handle',
handle: { kind: 'vertex', index: 1 },
})
expect(pressTarget([], at, ZONE)).toEqual({ kind: 'empty' })
})
})
describe('clickAction (A1)', () => {
const handle = (h: PressTarget & { kind: 'handle' }) => h
it('selects another, deselects on empty', () => {
expect(clickAction({ kind: 'other', id: 'b' }, ZONE)).toEqual({
type: 'select',
id: 'b',
})
expect(clickAction({ kind: 'empty' }, ZONE)).toEqual({ type: 'deselect' })
})
it('the interior opens without a chosen vertex, unchooses with one', () => {
const interior: PressTarget = { kind: 'interior', id: 'zone' }
expect(clickAction(interior, ZONE)).toEqual({ type: 'open', id: 'zone' })
expect(clickAction(interior, { ...ZONE, chosenVertex: 2 })).toEqual({
type: 'unchoose',
})
})
it('maps each handle, and a point handle opens the selection', () => {
const on = (h: Parameters<typeof handle>[0]['handle']) =>
clickAction(handle({ kind: 'handle', handle: h }), ZONE)
expect(on({ kind: 'vertex', index: 3 })).toEqual({
type: 'choose_vertex',
index: 3,
})
expect(on({ kind: 'insert', after: 1 })).toEqual({
type: 'insert',
after: 1,
})
expect(on({ kind: 'remove', index: 2 })).toEqual({
type: 'remove',
index: 2,
})
expect(on({ kind: 'point' })).toEqual({ type: 'open', id: 'zone' })
expect(
clickAction({ kind: 'handle', handle: { kind: 'point' } }, NONE),
).toEqual({
type: 'deselect',
})
})
})
describe('isDoubleClick (R-8)', () => {
const last = { at: { x: 0, y: 0 }, time: 1000 }
it('holds within 400 ms and 8 px, edges included', () => {
expect(isDoubleClick(last, { x: 8, y: 0 }, 1400)).toBe(true)
expect(isDoubleClick(last, { x: 8.01, y: 0 }, 1400)).toBe(false)
expect(isDoubleClick(last, { x: 0, y: 0 }, 1400.5)).toBe(false)
expect(isDoubleClick(null, { x: 0, y: 0 }, 1000)).toBe(false)
})
})
describe('dragAction (A13)', () => {
const ctx = (over: Partial<DragContext> = {}): DragContext => ({
kind: 'mouse',
movable: true,
moveMode: false,
...over,
})
const interior: PressTarget = { kind: 'interior', id: 'zone' }
const point: PressTarget = { kind: 'handle', handle: { kind: 'point' } }
it('a move mode drags whatever the target or the pointer', () => {
for (const kind of ['mouse', 'pen', 'touch'] as const) {
for (const target of [
interior,
point,
{ kind: 'empty' } as PressTarget,
]) {
expect(dragAction(target, ctx({ kind, moveMode: true }))).toEqual({
type: 'drag_mode',
})
}
}
})
it('a finger moves only what it holds: off a handle it is futile', () => {
const touch = ctx({ kind: 'touch' })
expect(dragAction(point, touch)).toEqual({ type: 'drag_point' })
expect(
dragAction(
{ kind: 'handle', handle: { kind: 'vertex', index: 4 } },
touch,
),
).toEqual({
type: 'drag_vertex',
index: 4,
})
expect(dragAction(interior, touch)).toEqual({ type: 'none', futile: true })
expect(dragAction({ kind: 'empty' }, touch)).toEqual({
type: 'none',
futile: true,
})
})
it('a mouse or pen drags the movable interior, pans otherwise', () => {
expect(dragAction(interior, ctx())).toEqual({
type: 'drag_shape',
id: 'zone',
})
expect(dragAction(interior, ctx({ kind: 'pen' }))).toEqual({
type: 'drag_shape',
id: 'zone',
})
expect(dragAction(interior, ctx({ movable: false }))).toEqual({
type: 'pan',
})
expect(dragAction({ kind: 'other', id: 'b' }, ctx())).toEqual({
type: 'pan',
})
expect(dragAction({ kind: 'empty' }, ctx())).toEqual({ type: 'pan' })
})
it('« + » and « − » never drag; an unmovable handle moves nothing', () => {
for (const kind of ['mouse', 'touch'] as const) {
expect(
dragAction(
{ kind: 'handle', handle: { kind: 'insert', after: 0 } },
ctx({ kind }),
),
).toEqual({ type: 'none', futile: false })
expect(
dragAction(
{ kind: 'handle', handle: { kind: 'remove', index: 0 } },
ctx({ kind }),
),
).toEqual({ type: 'none', futile: false })
}
expect(dragAction(point, ctx({ movable: false }))).toEqual({
type: 'none',
futile: false,
})
})
})
describe('stepPress (R-8)', () => {
const ctx = { movable: true, moveMode: false }
const target: PressTarget = { kind: 'interior', id: 'zone' }
const down = (
pointerId: number,
pointers: number,
kind = 'mouse' as const,
): PressEvent => ({
type: 'down',
pointerId,
kind,
at: { x: 0, y: 0 },
target,
pointers,
})
const move = (pointerId: number, x: number): PressEvent => ({
type: 'move',
pointerId,
at: { x, y: 0 },
})
const up = (pointerId: number, pointers: number): PressEvent => ({
type: 'up',
pointerId,
pointers,
})
/** Runs the events from idle; answers the effects and the last press. */
function run(events: PressEvent[]) {
let press: Press = { phase: 'idle' }
const effects = events.map((event) => {
const step = stepPress(press, event, ctx)
press = step.press
return step.effect
})
return { press, effects }
}
it('a single press within the slop is a click', () => {
const { press, effects } = run([down(1, 1), move(1, 4), up(1, 0)])
expect(effects).toEqual([null, null, { type: 'click', target }])
expect(press).toEqual({ phase: 'idle' })
})
it('past the slop it starts a drag, then ends it', () => {
const action = { type: 'drag_shape', id: 'zone' }
const { press, effects } = run([
down(1, 1),
move(1, 5),
move(1, 30),
up(1, 0),
])
expect(effects).toEqual([
null,
{ type: 'drag_start', action },
null,
{ type: 'drag_end' },
])
expect(press).toEqual({ phase: 'idle' })
})
it('the slop follows the pointer kind', () => {
const finger = run([down(1, 1, 'touch' as never), move(1, 10), up(1, 0)])
expect(finger.effects[2]).toEqual({ type: 'click', target })
})
it('a second down while down is a gesture: no click on any up, idle at zero pointers', () => {
const { press, effects } = run([down(1, 1), down(2, 2), up(1, 1), up(2, 0)])
expect(effects).toEqual([null, null, null, null])
expect(press).toEqual({ phase: 'idle' })
const held = run([down(1, 1), down(2, 2), up(2, 1)])
expect(held.press).toEqual({ phase: 'multi' })
})
it('a second down while dragging cancels the drag', () => {
const { press, effects } = run([down(1, 1), move(1, 20), down(2, 2)])
expect(effects[2]).toEqual({ type: 'drag_cancel' })
expect(press).toEqual({ phase: 'multi' })
})
it('cancel while dragging cancels the drag; while down it is silent', () => {
expect(
run([down(1, 1), move(1, 20), { type: 'cancel' }]).effects[2],
).toEqual({
type: 'drag_cancel',
})
const quiet = run([down(1, 1), { type: 'cancel' }, up(1, 0)])
expect(quiet.effects).toEqual([null, null, null])
})
it('ignores a move from another pointer id', () => {
const { press, effects } = run([down(1, 1), move(2, 50), up(1, 0)])
expect(effects).toEqual([null, null, { type: 'click', target }])
expect(press).toEqual({ phase: 'idle' })
})
})