test(app): D6b link, listener cleanup and pen cases; useEmptyTap within limits (R1, D6b)
This commit is contained in:
1 parent
bbdfd27311
commit
a1ee194725
2 files changed
+144
-89
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { useCallback, useEffect, useRef } from 'react'
|
||||
import type { PointerEvent } from 'react'
|
||||
import { useEffect, useMemo, useRef } from 'react'
|
||||
import type { PointerEvent, RefObject } from 'react'
|
||||
|
||||
/** Same 4 px as the framing's drag threshold: more is a drag, not a tap. */
|
||||
const TAP_SLOP_PX = 4
|
||||
@@ -20,6 +20,9 @@ interface Press {
|
||||
last: Point
|
||||
touchPending: boolean
|
||||
swallowClick: boolean
|
||||
pointerId: number
|
||||
/** Removes the document listener of the press in flight. */
|
||||
stop: (() => void) | null
|
||||
}
|
||||
|
||||
const idle = (): Press => ({
|
||||
@@ -30,10 +33,83 @@ const idle = (): Press => ({
|
||||
last: { x: 0, y: 0 },
|
||||
touchPending: false,
|
||||
swallowClick: false,
|
||||
pointerId: -1,
|
||||
stop: null,
|
||||
})
|
||||
|
||||
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. */
|
||||
function watchOtherPointers(p: Press): () => void {
|
||||
const other = (ev: Event) => {
|
||||
if ((ev as globalThis.PointerEvent).pointerId !== p.pointerId)
|
||||
p.multi = true
|
||||
}
|
||||
document.addEventListener('pointerdown', other, true)
|
||||
return () => document.removeEventListener('pointerdown', other, true)
|
||||
}
|
||||
|
||||
function disarm(ref: Ref): void {
|
||||
ref.current.armed = false
|
||||
ref.current.stop?.()
|
||||
ref.current.stop = null
|
||||
}
|
||||
|
||||
function press(ref: Ref, e: PointerEvent): void {
|
||||
if (!e.isPrimary) {
|
||||
ref.current.multi = true
|
||||
return
|
||||
}
|
||||
disarm(ref)
|
||||
const target = e.target instanceof Element ? e.target : null
|
||||
const next: Press = {
|
||||
...idle(),
|
||||
armed: e.button === 0,
|
||||
onAnnotation: target?.closest(ANNOTATED) != null,
|
||||
last: pointOf(e),
|
||||
pointerId: e.pointerId,
|
||||
}
|
||||
ref.current = next
|
||||
if (next.armed) next.stop = watchOtherPointers(next)
|
||||
}
|
||||
|
||||
function move(ref: Ref, e: PointerEvent): void {
|
||||
const p = ref.current
|
||||
if (!e.isPrimary || !p.armed) return
|
||||
const now = pointOf(e)
|
||||
p.moved += Math.hypot(now.x - p.last.x, now.y - p.last.y)
|
||||
p.last = now
|
||||
}
|
||||
|
||||
function verdict(ref: Ref, e: PointerEvent, enabled: boolean): TapVerdict {
|
||||
const p = ref.current
|
||||
const still = p.moved <= TAP_SLOP_PX && !p.multi
|
||||
const tap = enabled && e.isPrimary && p.armed && still && !p.onAnnotation
|
||||
if (e.isPrimary) disarm(ref)
|
||||
if (!tap) return 'none'
|
||||
if (e.pointerType === 'touch') {
|
||||
p.touchPending = true
|
||||
return 'on_click'
|
||||
}
|
||||
p.swallowClick = true
|
||||
return 'now'
|
||||
}
|
||||
|
||||
function takeClick(ref: Ref, target: EventTarget | null): ClickAnswer {
|
||||
const p = ref.current
|
||||
if (p.swallowClick) {
|
||||
p.swallowClick = false
|
||||
return 'swallow'
|
||||
}
|
||||
if (!p.touchPending) return 'normal'
|
||||
p.touchPending = false
|
||||
const onMarker =
|
||||
target instanceof Element && target.closest(ANNOTATED) !== null
|
||||
return onMarker ? 'normal' : 'deselect'
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells a tap on the empty drawing from every other gesture. The decision is taken from
|
||||
* the PRESS (where it landed, whether it moved, whether a second pointer joined), never
|
||||
@@ -45,92 +121,16 @@ const pointOf = (e: PointerEvent): Point => ({ x: e.clientX, y: e.clientY })
|
||||
* Refs only: no render follows a press.
|
||||
*/
|
||||
export function useEmptyTap() {
|
||||
const press = useRef<Press>(idle())
|
||||
const pointerId = useRef<number | null>(null)
|
||||
const stopListening = useRef<(() => void) | null>(null)
|
||||
|
||||
const release = useCallback(() => {
|
||||
stopListening.current?.()
|
||||
stopListening.current = null
|
||||
}, [])
|
||||
const disarm = useCallback(() => {
|
||||
press.current.armed = false
|
||||
release()
|
||||
}, [release])
|
||||
useEffect(() => release, [release])
|
||||
|
||||
const onPointerDown = useCallback(
|
||||
(e: PointerEvent) => {
|
||||
if (!e.isPrimary) {
|
||||
press.current.multi = true
|
||||
return
|
||||
}
|
||||
release()
|
||||
pointerId.current = e.pointerId
|
||||
const target = e.target instanceof Element ? e.target : null
|
||||
press.current = {
|
||||
...idle(),
|
||||
armed: e.button === 0,
|
||||
onAnnotation: target?.closest(ANNOTATED) != null,
|
||||
last: pointOf(e),
|
||||
}
|
||||
if (!press.current.armed) return
|
||||
// A second finger can land outside the box: only the document sees it.
|
||||
const other = (ev: Event) => {
|
||||
if ((ev as globalThis.PointerEvent).pointerId !== pointerId.current) {
|
||||
press.current.multi = true
|
||||
}
|
||||
}
|
||||
document.addEventListener('pointerdown', other, true)
|
||||
stopListening.current = () =>
|
||||
document.removeEventListener('pointerdown', other, true)
|
||||
},
|
||||
[release],
|
||||
const ref = useRef<Press>(idle())
|
||||
useEffect(() => () => disarm(ref), [])
|
||||
return useMemo(
|
||||
() => ({
|
||||
onPointerDown: (e: PointerEvent) => press(ref, e),
|
||||
onPointerMove: (e: PointerEvent) => move(ref, e),
|
||||
onPointerCancel: () => disarm(ref),
|
||||
verdict: (e: PointerEvent, enabled = true) => verdict(ref, e, enabled),
|
||||
takeClick: (target: EventTarget | null) => takeClick(ref, target),
|
||||
}),
|
||||
[],
|
||||
)
|
||||
|
||||
const onPointerMove = useCallback((e: PointerEvent) => {
|
||||
const p = press.current
|
||||
if (!e.isPrimary || !p.armed) return
|
||||
const now = pointOf(e)
|
||||
p.moved += Math.hypot(now.x - p.last.x, now.y - p.last.y)
|
||||
p.last = now
|
||||
}, [])
|
||||
|
||||
const verdict = useCallback(
|
||||
(e: PointerEvent, enabled = true): TapVerdict => {
|
||||
const p = press.current
|
||||
const still = p.moved <= TAP_SLOP_PX && !p.multi
|
||||
const tap = enabled && e.isPrimary && p.armed && still && !p.onAnnotation
|
||||
if (e.isPrimary) disarm()
|
||||
if (!tap) return 'none'
|
||||
if (e.pointerType === 'touch') {
|
||||
p.touchPending = true
|
||||
return 'on_click'
|
||||
}
|
||||
p.swallowClick = true
|
||||
return 'now'
|
||||
},
|
||||
[disarm],
|
||||
)
|
||||
|
||||
const takeClick = useCallback((target: EventTarget | null): ClickAnswer => {
|
||||
const p = press.current
|
||||
if (p.swallowClick) {
|
||||
p.swallowClick = false
|
||||
return 'swallow'
|
||||
}
|
||||
if (!p.touchPending) return 'normal'
|
||||
p.touchPending = false
|
||||
const onMarker =
|
||||
target instanceof Element && target.closest(ANNOTATED) !== null
|
||||
return onMarker ? 'normal' : 'deselect'
|
||||
}, [])
|
||||
|
||||
return {
|
||||
onPointerDown,
|
||||
onPointerMove,
|
||||
onPointerCancel: disarm,
|
||||
verdict,
|
||||
takeClick,
|
||||
}
|
||||
}
|
||||
@@ -1207,6 +1207,61 @@ describe('a tap on the drawing (D6b)', () => {
|
||||
expect(announce).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('selects a link (detail trigger) through the click', async () => {
|
||||
const view = insert(drawnText(), {
|
||||
kind: 'view',
|
||||
value: {
|
||||
kind: 'detail',
|
||||
id: 'view-det',
|
||||
name: { fr: 'Tête', en: 'Head' },
|
||||
svg_asset_id: null,
|
||||
view_box: { min_x: 10, min_y: 10, width: 80, height: 80 },
|
||||
parent_view_id: 'view-main',
|
||||
},
|
||||
})
|
||||
const link = {
|
||||
id: 'link-a',
|
||||
view_id: 'view-main',
|
||||
category_id: 'cat-a',
|
||||
laterality: 'unpaired',
|
||||
geometry: { type: 'circle', cx: 90, cy: 90, r: 3 },
|
||||
review_status: 'to_confirm',
|
||||
color: null,
|
||||
kind: 'detail_trigger',
|
||||
name: null,
|
||||
opens_view_id: 'view-det',
|
||||
} as Annotation
|
||||
await mountSpy({ text: insert(view, { kind: 'annotation', value: link }) })
|
||||
tap(marker('link-a'))
|
||||
expect(screen.getByTestId('annotation-properties')).toBeInTheDocument()
|
||||
expect(announce).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('removes the document listener when the canvas unmounts mid-press', async () => {
|
||||
await selected()
|
||||
const spy = vi.spyOn(document, 'removeEventListener')
|
||||
fireEvent.pointerDown(rect(), { ...init(), clientX: 10, clientY: 10 })
|
||||
cleanup()
|
||||
expect(spy).toHaveBeenCalledWith('pointerdown', expect.any(Function), true)
|
||||
spy.mockRestore()
|
||||
})
|
||||
|
||||
it('a context menu during the press removes the listener and does not deselect', async () => {
|
||||
await selected()
|
||||
const spy = vi.spyOn(document, 'removeEventListener')
|
||||
tap(rect(), { during: () => fireEvent.contextMenu(rect()) })
|
||||
expect(spy).toHaveBeenCalledWith('pointerdown', expect.any(Function), true)
|
||||
expect(isDeselected()).toBe(false)
|
||||
spy.mockRestore()
|
||||
})
|
||||
|
||||
it('a pen tap deselects at the pointer-up like a mouse', async () => {
|
||||
await selected()
|
||||
tap(rect(), { type: 'pen' })
|
||||
expect(isDeselected()).toBe(true)
|
||||
expect(announce).toHaveBeenCalledWith(DESELECTED)
|
||||
})
|
||||
|
||||
it('keeps a pending keyboard creation with its typed name', async () => {
|
||||
await mountSpy({ text: drawnText() })
|
||||
const user = userEvent.setup()
|
||||
|
||||
Reference in new issue
Block a user