-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
pointer-events.ts
152 lines (137 loc) · 3.67 KB
/
pointer-events.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { addEventListener } from './listener';
const MOUSE_WAIT = 2000;
// TODO(FW-2832): types
export const createPointerEvents = (
el: Node,
pointerDown: any,
pointerMove: any,
pointerUp: any,
options: {
passive?: boolean;
capture?: boolean;
}
) => {
let rmTouchStart: (() => void) | undefined;
let rmTouchMove: (() => void) | undefined;
let rmTouchEnd: (() => void) | undefined;
let rmTouchCancel: (() => void) | undefined;
let rmMouseStart: (() => void) | undefined;
let rmMouseMove: (() => void) | undefined;
let rmMouseUp: (() => void) | undefined;
let lastTouchEvent = 0;
const handleTouchStart = (ev: any) => {
lastTouchEvent = Date.now() + MOUSE_WAIT;
if (!pointerDown(ev)) {
return;
}
if (!rmTouchMove && pointerMove) {
rmTouchMove = addEventListener(el, 'touchmove', pointerMove, options);
}
/**
* Events are dispatched on the element that is tapped and bubble up to
* the reference element in the gesture. In the event that the element this
* event was first dispatched on is removed from the DOM, the event will no
* longer bubble up to our reference element. This leaves the gesture in an
* unusable state. To account for this, the touchend and touchcancel listeners
* should be added to the event target so that they still fire even if the target
* is removed from the DOM.
*/
if (!rmTouchEnd) {
rmTouchEnd = addEventListener(ev.target, 'touchend', handleTouchEnd, options);
}
if (!rmTouchCancel) {
rmTouchCancel = addEventListener(ev.target, 'touchcancel', handleTouchEnd, options);
}
};
const handleMouseDown = (ev: any) => {
if (lastTouchEvent > Date.now()) {
return;
}
if (!pointerDown(ev)) {
return;
}
if (!rmMouseMove && pointerMove) {
rmMouseMove = addEventListener(getDocument(el), 'mousemove', pointerMove, options);
}
if (!rmMouseUp) {
rmMouseUp = addEventListener(getDocument(el), 'mouseup', handleMouseUp, options);
}
};
const handleTouchEnd = (ev: any) => {
stopTouch();
if (pointerUp) {
pointerUp(ev);
}
};
const handleMouseUp = (ev: any) => {
stopMouse();
if (pointerUp) {
pointerUp(ev);
}
};
const stopTouch = () => {
if (rmTouchMove) {
rmTouchMove();
}
if (rmTouchEnd) {
rmTouchEnd();
}
if (rmTouchCancel) {
rmTouchCancel();
}
rmTouchMove = rmTouchEnd = rmTouchCancel = undefined;
};
const stopMouse = () => {
if (rmMouseMove) {
rmMouseMove();
}
if (rmMouseUp) {
rmMouseUp();
}
rmMouseMove = rmMouseUp = undefined;
};
const stop = () => {
stopTouch();
stopMouse();
};
const enable = (isEnabled = true) => {
if (!isEnabled) {
if (rmTouchStart) {
rmTouchStart();
}
if (rmMouseStart) {
rmMouseStart();
}
rmTouchStart = rmMouseStart = undefined;
stop();
} else {
if (!rmTouchStart) {
rmTouchStart = addEventListener(el, 'touchstart', handleTouchStart, options);
}
if (!rmMouseStart) {
rmMouseStart = addEventListener(el, 'mousedown', handleMouseDown, options);
}
}
};
const destroy = () => {
enable(false);
pointerUp = pointerMove = pointerDown = undefined;
};
return {
enable,
stop,
destroy,
};
};
const getDocument = (node: Node) => {
return node instanceof Document ? node : node.ownerDocument;
};
export interface PointerEventsConfig {
element?: HTMLElement;
pointerDown: (ev: any) => boolean;
pointerMove?: (ev: any) => void;
pointerUp?: (ev: any) => void;
zone?: boolean;
capture?: boolean;
passive?: boolean;
}