All files / core/blong-browser/src/feedback useDrag.ts

53.84% Statements 14/26
0% Branches 0/6
50% Functions 2/4
58.33% Lines 14/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57                                  18x 18x       1x 1x 1x 1x       1x 1x       1x               1x           1x 1x 1x       18x    
/**
 * useDrag.ts — pointer deltas for the mode's own boxes (`@@T-489`).
 *
 * One implementation for the callout's bevel, its corner, and the crop rectangle:
 * every one of them reports "this much since the last frame" and lets the owner
 * decide what to do with it. Deltas come from the previous pointer position
 * rather than the grab point, so a box that a clamp stopped for a frame follows
 * the pointer again as soon as it can.
 */
 
import {useCallback, useRef, type PointerEvent} from 'react';
 
export interface IDrag {
    start: (event: PointerEvent<HTMLElement>) => void;
}
 
export function useDrag(onDelta: (dx: number, dy: number) => void): IDrag {
    const lastRef = useRef<{x: number; y: number} | null>(null);
    const start = useCallback(
        (event: PointerEvent<HTMLElement>) => {
            // A drag must not select the text underneath or focus the editor the
            // box is about, and it must not reach the guard.
            event.preventDefault();
            event.stopPropagation();
            lastRef.current = {x: event.clientX, y: event.clientY};
            const node = event.currentTarget;
            // A capture is a convenience, not the mechanism: the listeners below are on
            // the node itself. A synthetic pointer (a story or a test) has no active
            // pointer id, and the call throws for it (`@@T-498`).
            try {
                node.setPointerCapture?.(event.pointerId);
            } catch {
                // No capture to take; the drag still runs on this node.
            }
            const onPointerMove = (moveEvent: globalThis.PointerEvent) => {
                const previous = lastRef.current;
                if (!previous) return;
                const dx = moveEvent.clientX - previous.x;
                const dy = moveEvent.clientY - previous.y;
                lastRef.current = {x: moveEvent.clientX, y: moveEvent.clientY};
                if (dx || dy) onDelta(dx, dy);
            };
            const end = () => {
                lastRef.current = null;
                node.removeEventListener('pointermove', onPointerMove);
                node.removeEventListener('pointerup', end);
                node.removeEventListener('pointercancel', end);
            };
            node.addEventListener('pointermove', onPointerMove);
            node.addEventListener('pointerup', end);
            node.addEventListener('pointercancel', end);
        },
        [onDelta],
    );
    return {start};
}