refactor(web): extract timeline touch helpers

This commit is contained in:
2026-04-01 09:48:04 +02:00
parent 167eec31de
commit 3abb3bc865
4 changed files with 151 additions and 30 deletions
+50
View File
@@ -0,0 +1,50 @@
export type TouchPoint = {
clientX: number;
clientY: number;
};
export type TouchDecisionState = {
x: number;
y: number;
decided: boolean;
};
type TouchLike = {
clientX: number;
clientY: number;
};
type TouchEventLike = {
touches: ArrayLike<TouchLike>;
changedTouches: ArrayLike<TouchLike>;
};
export function getTouchPoint(event: TouchEventLike): TouchPoint {
const touch = event.touches[0] ?? event.changedTouches[0];
return {
clientX: touch?.clientX ?? 0,
clientY: touch?.clientY ?? 0,
};
}
export function resolveTouchDragDecision(
state: TouchDecisionState,
point: TouchPoint,
thresholdPx = 8,
): { nextState: TouchDecisionState; shouldHandleDrag: boolean } {
if (state.decided) {
return { nextState: state, shouldHandleDrag: true };
}
const dx = Math.abs(point.clientX - state.x);
const dy = Math.abs(point.clientY - state.y);
if (dx <= thresholdPx && dy <= thresholdPx) {
return { nextState: state, shouldHandleDrag: false };
}
const nextState = { ...state, decided: true };
return {
nextState,
shouldHandleDrag: dx >= dy,
};
}