Line data Source code
1 : import type { Point2D } from '@comfyorg/shared-frontend-utils/geometry'
2 :
3 : type PointerPosition = Readonly<Point2D>
4 :
5 0 : function squaredDistance(a: PointerPosition, b: PointerPosition): number {
6 0 : const dx = a.x - b.x
7 0 : const dy = a.y - b.y
8 0 : return dx * dx + dy * dy
9 : }
10 :
11 0 : export function exceedsClickThreshold(
12 : start: PointerPosition,
13 : end: PointerPosition,
14 : threshold: number
15 : ): boolean {
16 0 : return squaredDistance(start, end) > threshold * threshold
17 : }
18 :
19 119 : export function useClickDragGuard(threshold: number = 5) {
20 119 : let start: PointerPosition | null = null
21 :
22 0 : function recordStart(e: { clientX: number; clientY: number }) {
23 0 : start = { x: e.clientX, y: e.clientY }
24 : }
25 :
26 0 : function wasDragged(e: { clientX: number; clientY: number }): boolean {
27 0 : if (!start) return false
28 0 : return exceedsClickThreshold(
29 : start,
30 : { x: e.clientX, y: e.clientY },
31 : threshold
32 : )
33 : }
34 :
35 0 : function reset() {
36 0 : start = null
37 : }
38 :
39 119 : return { recordStart, wasDragged, reset }
40 : }
|