fix(app): D6b pen follows touch, stale press and hover guards (R1, D6b)

This commit is contained in:
bchanot committed 2026-11-12 13:42:01 +01:00
1 parent 132756eecd
commit 6ed90efb3a
2 files changed
+68 -10

No files matched your search

+17 -8
View File
@@ -7,7 +7,7 @@ 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. */
/** What a pointer-up decides: act now (mouse), at the click (touch, pen), 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'
@@ -41,11 +41,18 @@ const pointOf = (e: PointerEvent): Point => ({ x: e.clientX, y: e.clientY })
type Ref = RefObject<Press>
/** A second finger can land outside the box: only the document sees it. */
/**
* A second finger can land outside the box: only the document sees it. The same pointer
* pressing again means the armed press lost its release (it ended outside the box): stale.
*/
function watchOtherPointers(p: Press): () => void {
const other = (ev: Event) => {
if ((ev as globalThis.PointerEvent).pointerId !== p.pointerId)
p.multi = true
if ((ev as globalThis.PointerEvent).pointerId !== p.pointerId) p.multi = true
else {
p.armed = false
p.stop?.()
p.stop = null
}
}
document.addEventListener('pointerdown', other, true)
return () => document.removeEventListener('pointerdown', other, true)
@@ -77,7 +84,8 @@ function press(ref: Ref, e: PointerEvent): void {
function move(ref: Ref, e: PointerEvent): void {
const p = ref.current
if (!e.isPrimary || !p.armed) return
// A hover (no button, no contact) is not part of the press.
if (!e.isPrimary || !p.armed || e.buttons === 0) return
const now = pointOf(e)
p.moved += Math.hypot(now.x - p.last.x, now.y - p.last.y)
p.last = now
@@ -89,7 +97,7 @@ function verdict(ref: Ref, e: PointerEvent, enabled: boolean): TapVerdict {
const tap = enabled && e.isPrimary && p.armed && still && !p.onAnnotation
if (e.isPrimary) disarm(ref)
if (!tap) return 'none'
if (e.pointerType === 'touch') {
if (e.pointerType !== 'mouse') {
p.touchPending = true
return 'on_click'
}
@@ -116,8 +124,9 @@ function takeClick(ref: Ref, target: EventTarget | null): ClickAnswer {
* 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.
* a nearby marker; a stylus platform builds it the same way. Only the mouse acts at the
* pointer-up (a blur commit at mousedown can rebuild the drawing and drop the click);
* touch and pen act in the tap's own click.
* Refs only: no render follows a press.
*/
export function useEmptyTap() {
@@ -1112,6 +1112,7 @@ describe('a tap on the drawing (D6b)', () => {
pointerId: id,
isPrimary: true,
button: 0,
buttons: 1,
pointerType: type,
})
@@ -1255,13 +1256,59 @@ describe('a tap on the drawing (D6b)', () => {
spy.mockRestore()
})
it('a pen tap deselects at the pointer-up like a mouse', async () => {
it('the read-only twin keeps the keyboard pan and zoom', async () => {
await mountSpy({ readOnly: true })
const canvas = screen.getByTestId('catalogue-canvas')
const view = drawing(true)
fireEvent.keyDown(canvas, { key: '+' })
expect(view.style.transform).toContain('scale(1.5)')
// Handled (default prevented) by the framing's keyboard pan; the box has no size here.
expect(fireEvent.keyDown(canvas, { key: 'ArrowLeft' })).toBe(false)
})
it('a pen tap deselects in its own click, like a touch tap', async () => {
await selected()
tap(rect(), { type: 'pen' })
const hit = rect()
fireEvent.pointerDown(hit, { ...init(1, 'pen'), clientX: 10, clientY: 10 })
fireEvent.pointerUp(hit, { ...init(1, 'pen'), clientX: 10, clientY: 10 })
expect(isDeselected()).toBe(false)
fireEvent.click(hit)
expect(isDeselected()).toBe(true)
expect(announce).toHaveBeenCalledWith(DESELECTED)
})
it('a pen tap whose click lands on a marker selects that marker', async () => {
await selected({ text: insert(text(), named('anno-b', 90, 90, 3)) })
tap(rect(), { type: 'pen', upOn: marker('anno-b') })
expect(announce).not.toHaveBeenCalled()
expect(marker('anno-b')).toHaveAttribute('data-state', 'current')
expect(marker('anno-a')).not.toHaveAttribute('data-state')
})
it('a hover move after the press does not count as a drag', async () => {
await selected()
fireEvent.pointerDown(rect(), { ...init(), clientX: 10, clientY: 10 })
fireEvent.pointerMove(rect(), {
...init(),
buttons: 0,
clientX: 20,
clientY: 10,
})
fireEvent.pointerUp(rect(), { ...init(), clientX: 10, clientY: 10 })
fireEvent.click(rect())
expect(isDeselected()).toBe(true)
})
it('a new press with the same id outside the box disarms a stale press', async () => {
await selected()
fireEvent.pointerDown(rect(), { ...init(), clientX: 10, clientY: 10 })
fireEvent.pointerUp(document.body, { ...init(), clientX: 10, clientY: 10 })
fireEvent.pointerDown(document.body, { ...init(), clientX: 300, clientY: 10 })
fireEvent.pointerUp(rect(), { ...init(), clientX: 10, clientY: 10 })
fireEvent.click(rect())
expect(isDeselected()).toBe(false)
})
it('keeps a pending keyboard creation with its typed name', async () => {
await mountSpy({ text: drawnText() })
const user = userEvent.setup()
@@ -1395,6 +1442,8 @@ describe('a tap on the drawing (D6b)', () => {
tap(rect(), { type: 'touch', upOn: marker('anno-b') })
expect(announce).not.toHaveBeenCalled()
expect(isDeselected()).toBe(false)
expect(marker('anno-b')).toHaveAttribute('data-state', 'current')
expect(marker('anno-a')).not.toHaveAttribute('data-state')
})
it('a mouse tap deselects at the pointer-up and swallows the click on a marker', async () => {