diff --git a/apps/app/src/editor/annotations/useBlockDrag.ts b/apps/app/src/editor/annotations/useBlockDrag.ts index 3f3591b9..a348f4c3 100644 --- a/apps/app/src/editor/annotations/useBlockDrag.ts +++ b/apps/app/src/editor/annotations/useBlockDrag.ts @@ -47,18 +47,18 @@ function useDropOnChange(target: MoveTarget | null, drop: () => void) { useEffect(() => dropRef.current(), [id, text]) } -/** - * The selection moved as a block (`drag_shape`, `drag_point`): the overlay previews the - * translated outline, kept local to the canvas for speed; the drop commits once. - */ -export function useBlockDrag(input: BlockDragInput) { - const drag = useRef(null) - const [preview, setPreview] = useState(null) +type SetPreview = (geometry: Geometry | null) => void + +/** Start, follow, drop and cancel of the block drag held in `drag`. */ +function dragActions( + input: BlockDragInput, + drag: RefObject, + setPreview: SetPreview, +) { const cancel = () => { drag.current = null setPreview(null) } - useDropOnChange(input.target, cancel) const start = (pressedAt: XY, client: XY) => { const origin = clientToDrawing(input.overlay.current, pressedAt) if (!input.target || !origin) return @@ -77,5 +77,17 @@ export function useBlockDrag(input: BlockDragInput) { cancel() if (gesture && dropped) input.onDrop(gesture.target.id, dropped) } - return { preview, start, follow, drop, cancel } + return { start, follow, drop, cancel } +} + +/** + * The selection moved as a block (`drag_shape`, `drag_point`): the overlay previews the + * translated outline, kept local to the canvas for speed; the drop commits once. + */ +export function useBlockDrag(input: BlockDragInput) { + const drag = useRef(null) + const [preview, setPreview] = useState(null) + const actions = dragActions(input, drag, setPreview) + useDropOnChange(input.target, actions.cancel) + return { preview, ...actions } } diff --git a/apps/app/src/editor/annotations/usePressGesture.ts b/apps/app/src/editor/annotations/usePressGesture.ts index a7a60267..0526de81 100644 --- a/apps/app/src/editor/annotations/usePressGesture.ts +++ b/apps/app/src/editor/annotations/usePressGesture.ts @@ -136,21 +136,26 @@ interface Feed { drag: ReturnType } -/** The click effect: a drawing tool places, the select tool selects or deselects. */ -function onClick(f: Feed, target: PressTarget, event: PointerEvent): void { - const { input } = f - const at = clientOf(event) - if (input.tool !== 'select') { - const drawn = clientToDrawing(input.overlay.current, at) - if (drawn) input.onPlace(drawn) - return - } +/** A click with a drawing tool places at the pointer-up, in drawing units. */ +function place(input: PressInput, event: PointerEvent): void { + const drawn = clientToDrawing(input.overlay.current, clientOf(event)) + if (drawn) input.onPlace(drawn) +} + +/** A click with the select tool: its action, or `open` as the second of a double click. */ +function selectClick(f: Feed, target: PressTarget, event: PointerEvent): void { const m = f.machine.current - const sel = selectionOf(input.selectedId) + const at = clientOf(event) + const sel = selectionOf(f.input.selectedId) const double = isDoubleClick(m.lastClick, at, event.timeStamp) m.lastClick = double ? null : { at, time: event.timeStamp } const action = clickAction(target, sel) - runClick(double ? doubled(action, target, sel) : action, input) + runClick(double ? doubled(action, target, sel) : action, f.input) +} + +function onClick(f: Feed, target: PressTarget, event: PointerEvent): void { + if (f.input.tool === 'select') selectClick(f, target, event) + else place(f.input, event) } const BLOCK_DRAGS = new Set(['drag_shape', 'drag_point']) @@ -234,23 +239,28 @@ function startPress(latest: RefObject, event: PointerEvent): void { m.stop = watchOtherPointers(latest) } +/** The press's target: the selection's handles, then the topmost annotation. */ +function targetAt(input: PressInput, at: XY): PressTarget { + const hits = hitsAt(input.viewport.current, at, pointIdOf(input.target)) + return pressTarget(hits, at, selectionOf(input.selectedId)) +} + function onPointerDown(latest: RefObject, event: PointerEvent): void { - const f = latest.current - const m = f.machine.current + const m = latest.current.machine.current const at = clientOf(event) - const hits = hitsAt(f.input.viewport.current, at, pointIdOf(f.input.target)) - m.target = pressTarget(hits, at, selectionOf(f.input.selectedId)) + m.target = targetAt(latest.current.input, at) if (event.button !== 0) return if (isFresh(m, event)) startPress(latest, event) m.pointers.add(event.pointerId) - const { pointerId } = event - const kind = pointerKindOf(event.pointerType) - const pointers = m.pointers.size - feed( - f, - { type: 'down', pointerId, kind, at, target: m.target, pointers }, - event, - ) + const down: PressEvent = { + type: 'down', + pointerId: event.pointerId, + kind: pointerKindOf(event.pointerType), + at, + target: m.target, + pointers: m.pointers.size, + } + feed(latest.current, down, event) } function onPointerMove(f: Feed, event: PointerEvent): void {