forked from Easterok/telegram-onboarding-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swipe.directive.ts
109 lines (82 loc) · 2.66 KB
/
swipe.directive.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { DirectiveBinding, ObjectDirective } from 'vue';
export interface Swipe {
direction: 'top' | 'left' | 'bottom' | 'right';
events: [TouchEvent, TouchEvent];
}
const DEFAULT_THRESHOLD = 30;
const DEFAULT_TIMEOUT = 500;
type Fn = (event: TouchEvent) => void;
type Binding = {
onEvent: (element: EventTarget | null, swipe: Swipe) => void;
timeout?: number;
threshold?: number;
};
function getSwipeDirection(deltaX: number, deltaY: number): Swipe['direction'] {
if (Math.abs(deltaY) > Math.abs(deltaX)) {
return deltaY > 0 ? 'top' : 'bottom';
} else {
return deltaX > 0 ? 'left' : 'right';
}
}
const dict = new Map<HTMLElement, [Fn, Fn]>();
function beforeMount(
element: HTMLElement,
{ value }: DirectiveBinding<Binding>
) {
const events = [null, null] as [TouchEvent | null, TouchEvent | null];
const threshold =
typeof value.threshold === 'number' ? value.threshold : DEFAULT_THRESHOLD;
const timeout =
typeof value.timeout === 'number' ? value.timeout : DEFAULT_TIMEOUT;
const handle = () => {
const first = events[0];
const second = events[1];
if (first === null || second === null) {
return;
}
const isHandlable =
!!first.touches.length &&
first.touches[0].identifier === second.changedTouches[0].identifier;
if (!isHandlable) {
return;
}
const { clientX: startX, clientY: startY } = first.touches[0];
const { clientX: endX, clientY: endY } = second.changedTouches[0];
const distanceX = startX - endX;
const distanceY = startY - endY;
const duration = second.timeStamp - first.timeStamp;
const isSwipe =
(Math.abs(distanceX) > threshold || Math.abs(distanceY) > threshold) &&
duration < timeout;
if (isSwipe) {
const swipeEvent = {
direction: getSwipeDirection(distanceX, distanceY),
events: [first, second] as [TouchEvent, TouchEvent],
};
value.onEvent(element, swipeEvent);
}
};
const touchStart = (event: TouchEvent) => {
events[0] = event;
};
const touchEnd = (event: TouchEvent) => {
events[1] = event;
handle();
};
dict.set(element, [touchStart, touchEnd]);
element.addEventListener('touchstart', touchStart, { passive: true });
document.addEventListener('touchend', touchEnd, { passive: true });
}
function beforeUnmount(element: HTMLElement) {
const listeners = dict.get(element);
if (!listeners) {
return;
}
const [touchStart, touchEnd] = listeners;
element.removeEventListener('touchstart', touchStart);
document.removeEventListener('touchend', touchEnd);
}
export const SwipeDirective: ObjectDirective<HTMLElement, Binding> = {
beforeMount,
beforeUnmount,
};