(
el: GlobalEventHandlers | null,
data: T,
options: ITouchEventOptions,
handle1PointDrag?: (ev: TouchEvent, start: TouchEventStart1PointDrag<T>, end: boolean) => void,
handle2PointDrag?: (ev: TouchEvent, start: TouchEventStart<T>) => void,
handle1PointClick?: (ev: TouchEvent, start: TouchEventStart<T>) => void,
)
| 147 | } |
| 148 | |
| 149 | export function useTouchEvents<T>( |
| 150 | el: GlobalEventHandlers | null, |
| 151 | data: T, |
| 152 | options: ITouchEventOptions, |
| 153 | handle1PointDrag?: (ev: TouchEvent, start: TouchEventStart1PointDrag<T>, end: boolean) => void, |
| 154 | handle2PointDrag?: (ev: TouchEvent, start: TouchEventStart<T>) => void, |
| 155 | handle1PointClick?: (ev: TouchEvent, start: TouchEventStart<T>) => void, |
| 156 | ) { |
| 157 | let alwaysSendDragEvent = options.alwaysSendDragEvent ?? false; |
| 158 | let sendDragEnd = options.sendDragEnd ?? false; |
| 159 | let initialData = useRef<T>(data); |
| 160 | let initialTouches = useRef<TouchSimple[]>(); |
| 161 | let lastTouch = useRef<{ time: number, velocity: number, touch: TouchSimple } | null>(null); |
| 162 | let isDrag = useRef<boolean>(false); |
| 163 | let latestData = useRef<T>(data); |
| 164 | let lastPressTime = useRef<number>(0); |
| 165 | latestData.current = data; |
| 166 | let handle1PointDragRef = useFunctionRef(handle1PointDrag); |
| 167 | let handle2PointDragRef = useFunctionRef(handle2PointDrag); |
| 168 | let handle1PointClickRef = useFunctionRef(handle1PointClick); |
| 169 | |
| 170 | useEffect(() => { |
| 171 | if (!el) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | function sendEvent(ev: TouchEvent) { |
| 176 | let initial = {data: initialData.current, touches: initialTouches.current!}; |
| 177 | if (!ev.touches || !initial.touches || ev.touches.length !== initial.touches.length) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | if (!isDrag.current) { |
| 182 | if (ev.touches.length > 1 || (ev.touches.length === 1 && touchPixelDist(ev.touches[0], initial.touches[0]) >= 10)) { |
| 183 | isDrag.current = true; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (ev.touches.length === 1 && handle1PointDragRef.current && (alwaysSendDragEvent || isDrag.current)) { |
| 188 | handle1PointDragRef.current(ev, { ...initial, isDragging: isDrag.current }, false); |
| 189 | } |
| 190 | if (ev.touches.length === 2 && handle2PointDragRef.current) { |
| 191 | handle2PointDragRef.current(ev, initial); |
| 192 | } |
| 193 | |
| 194 | if (ev.touches.length === 1) { |
| 195 | lastTouch.current = { |
| 196 | time: 0, |
| 197 | velocity: 0, |
| 198 | touch: copyTouchList(ev.touches)[0], |
| 199 | }; |
| 200 | |
| 201 | } else { |
| 202 | lastTouch.current = null; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | function captureInitialAndSend(ev: TouchEvent) { |
no test coverage detected