Minor 1: the loupe's copy of the drawing drops every id (the equine drawing has 142); its url(#…) and href="#…" resolve to the originals. Minor 2: useFraming's onPinch marks the two-finger flag at the pinch move itself; the passive effect that read the live press phase and the press hook's multi() are removed.
90 lines
3.0 KiB
TypeScript
90 lines
3.0 KiB
TypeScript
import type { CSSProperties, KeyboardEvent, PointerEvent } from 'react'
|
|
import { useFraming } from '@/catalogue/useFraming'
|
|
import type { ToolId } from './uiState'
|
|
import { usePressGesture, type PressInput } from './usePressGesture'
|
|
import { markTwoFingers } from './workStore'
|
|
|
|
export interface GestureInput extends PressInput {
|
|
viewId: string
|
|
/** Enter and Escape while the canvas box itself has the focus (a tracing). */
|
|
onTraceKey: (event: KeyboardEvent) => void
|
|
}
|
|
|
|
type Framing = ReturnType<typeof useFraming>
|
|
type Press = ReturnType<typeof usePressGesture>
|
|
|
|
/** The press machine first (it decides the press's target), then the framing. */
|
|
function composedPointer(framing: Framing, press: Press) {
|
|
const both =
|
|
(
|
|
name:
|
|
| 'onPointerDown'
|
|
| 'onPointerMove'
|
|
| 'onPointerUp'
|
|
| 'onPointerCancel',
|
|
) =>
|
|
(event: PointerEvent) => {
|
|
press.handlers[name](event)
|
|
framing.handlers[name](event)
|
|
}
|
|
return {
|
|
onPointerDown: both('onPointerDown'),
|
|
onPointerMove: both('onPointerMove'),
|
|
onPointerUp: both('onPointerUp'),
|
|
onPointerCancel: both('onPointerCancel'),
|
|
onLostPointerCapture: press.handlers.onLostPointerCapture,
|
|
onContextMenu: press.handlers.onContextMenu,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* `touch-action: none` while a drawing tool, a zoom or a movable selection (a drag in
|
|
* flight implies one) is active: the browser reads it at touchstart, so a touch drag of
|
|
* the selected marker would otherwise be taken as a pan and cancelled before it starts.
|
|
*/
|
|
function touchStyle(
|
|
tool: ToolId,
|
|
scale: number,
|
|
movable: boolean,
|
|
): CSSProperties {
|
|
const none = tool !== 'select' || scale !== 1 || movable
|
|
return { touchAction: none ? 'none' : 'pan-x pan-y' }
|
|
}
|
|
|
|
/**
|
|
* Everything the canvas box listens to: zoom and pan, the press machine (click, drag,
|
|
* tap), and the keys. Keys only count when the box itself is the target: Enter on
|
|
* « Annuler le tracé » (or a list row) is that button's, never a close of the tracing, and
|
|
* an arrow on an element inside the box never pans the drawing.
|
|
*/
|
|
export function useCanvasGestures(input: GestureInput) {
|
|
const press = usePressGesture(input)
|
|
const framing = useFraming(input.viewport, input.viewId, {
|
|
// 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(),
|
|
twoPointerPan: true,
|
|
// R-1: a two-finger pan or zoom that worked; the visit never shows the hint again.
|
|
onPinch: markTwoFingers,
|
|
})
|
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
if (event.target !== event.currentTarget) return
|
|
framing.handlers.onKeyDown(event)
|
|
input.onTraceKey(event)
|
|
}
|
|
return {
|
|
framing,
|
|
drag: press.preview,
|
|
finger: press.finger,
|
|
boxProps: {
|
|
...composedPointer(framing, press),
|
|
onKeyDown,
|
|
style: touchStyle(
|
|
input.tool,
|
|
framing.framing.scale,
|
|
input.target !== null,
|
|
),
|
|
},
|
|
}
|
|
}
|