-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDragScroll.ts
296 lines (266 loc) · 8.55 KB
/
DragScroll.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import EventEmitter from "@scena/event-emitter";
import { isFunction, isString, now } from "@daybrush/utils";
import { CheckScrollOptions, DragScrollEvents, DragScrollOptions, Rect } from "./types";
function getDefaultScrollPosition(e: { container: HTMLElement, direction: number[] }) {
let container = e.container;
if (container === document.body) {
return [
container.scrollLeft || document.documentElement.scrollLeft,
container.scrollTop || document.documentElement.scrollTop,
];
}
return [
container.scrollLeft,
container.scrollTop,
];
}
function checkDefaultScrollEvent(container: HTMLElement | Window, callback: () => void) {
container.addEventListener("scroll", callback);
return () => {
container.removeEventListener("scroll", callback);
}
}
function getContainerElement(container: DragScrollOptions["container"]): HTMLElement {
if (!container) {
return null;
} else if (isString(container)) {
return document.querySelector<HTMLElement>(container);
} if (isFunction(container)) {
return container();
} else if (container instanceof Element) {
return container;
} else if ("current" in container) {
return container.current;
} else if ("value" in container) {
return container.value;
}
}
/**
* @sort 1
*/
class DragScroll extends EventEmitter<DragScrollEvents> {
private _startRect: Rect | null = null;
private _startPos: number[] = [];
private _prevTime: number = 0;
private _timer: number = 0;
private _prevScrollPos: number[] = [0, 0];
private _isWait = false;
private _flag = false;
private _currentOptions: DragScrollOptions | null = null;
private _lock = false;
private _unregister: (() => void) | null = null;
/**
*/
public dragStart(e: any, options: DragScrollOptions) {
const container = getContainerElement(options.container);
if (!container) {
this._flag = false;
return;
}
let top = 0;
let left = 0;
let width = 0;
let height = 0;
if (container === document.body) {
width = window.innerWidth;
height = window.innerHeight;
} else {
const rect = container.getBoundingClientRect();
top = rect.top;
left = rect.left;
width = rect.width;
height = rect.height;
}
this._flag = true;
this._startPos = [e.clientX, e.clientY];
this._startRect = { top, left, width, height };
this._prevScrollPos = this._getScrollPosition([0, 0], options);
this._currentOptions = options;
this._registerScrollEvent(options);
}
public drag(e: any, options: DragScrollOptions) {
clearTimeout(this._timer);
if (!this._flag) {
return;
}
const {
clientX,
clientY,
} = e;
const {
threshold = 0,
} = options;
const {
_startRect,
_startPos,
} = this;
this._currentOptions = options;
const direction = [0, 0];
if (_startRect.top > clientY - threshold) {
if (_startPos[1] > _startRect.top || clientY < _startPos[1]) {
direction[1] = -1;
}
} else if (_startRect.top + _startRect.height < clientY + threshold) {
if (_startPos[1] < _startRect.top + _startRect.height || clientY > _startPos[1]) {
direction[1] = 1;
}
}
if (_startRect.left > clientX - threshold) {
if (_startPos[0] > _startRect.left || clientX < _startPos[0]) {
direction[0] = -1;
}
} else if (_startRect.left + _startRect.width < clientX + threshold) {
if (_startPos[0] < _startRect.left + _startRect.width || clientX > _startPos[0]) {
direction[0] = 1;
}
}
if (!direction[0] && !direction[1]) {
return false;
}
return this._continueDrag({
...options,
direction,
inputEvent: e,
isDrag: true,
});
}
/**
*/
public checkScroll(options: CheckScrollOptions) {
if (this._isWait) {
return false;
}
const {
prevScrollPos = this._prevScrollPos,
direction,
throttleTime = 0,
inputEvent,
isDrag,
} = options;
const nextScrollPos = this._getScrollPosition(direction || [0, 0], options);
const offsetX = nextScrollPos[0] - prevScrollPos[0];
const offsetY = nextScrollPos[1] - prevScrollPos[1];
const nextDirection = direction || [
offsetX ? Math.abs(offsetX) / offsetX : 0,
offsetY ? Math.abs(offsetY) / offsetY : 0,
];
this._prevScrollPos = nextScrollPos;
this._lock = false;
if (!offsetX && !offsetY) {
return false;
}
/**
* @event DragScroll#move
*/
this.emit("move", {
offsetX: nextDirection[0] ? offsetX : 0,
offsetY: nextDirection[1] ? offsetY : 0,
inputEvent,
});
if (throttleTime && isDrag) {
clearTimeout(this._timer);
this._timer = window.setTimeout(() => {
this._continueDrag(options);
}, throttleTime);
}
return true;
}
/**
*
*/
public dragEnd() {
this._flag = false;
this._lock = false;
clearTimeout(this._timer);
this._unregisterScrollEvent();
}
private _getScrollPosition(direction: number[], options: DragScrollOptions) {
const {
container,
getScrollPosition = getDefaultScrollPosition,
} = options;
return getScrollPosition({ container: getContainerElement(container), direction });
}
private _continueDrag(options: CheckScrollOptions) {
const {
container,
direction,
throttleTime,
useScroll,
isDrag,
inputEvent,
} = options;
if (!this._flag || (isDrag && this._isWait)) {
return;
}
const nowTime = now();
const distTime = Math.max(throttleTime + this._prevTime - nowTime, 0);
if (distTime > 0) {
clearTimeout(this._timer);
this._timer = window.setTimeout(() => {
this._continueDrag(options);
}, distTime);
return false;
}
this._prevTime = nowTime;
const prevScrollPos = this._getScrollPosition(direction, options);
this._prevScrollPos = prevScrollPos;
if (isDrag) {
this._isWait = true;
}
// unregister native scroll event
if (!useScroll) {
this._lock = true;
}
const param = {
container: getContainerElement(container),
direction,
inputEvent,
};
options.requestScroll?.(param);
/**
* @event DragScroll#scroll
*/
this.emit("scroll", param);
this._isWait = false;
return useScroll || this.checkScroll({
...options,
prevScrollPos,
direction,
inputEvent,
});
}
private _registerScrollEvent(options: DragScrollOptions) {
this._unregisterScrollEvent();
const checkScrollEvent = options.checkScrollEvent;
if (!checkScrollEvent) {
return;
}
const callback = checkScrollEvent === true ? checkDefaultScrollEvent : checkScrollEvent;
const container = getContainerElement(options.container);
if (checkScrollEvent === true && (container === document.body || container === document.documentElement)) {
this._unregister = checkDefaultScrollEvent(window, this._onScroll);
} else {
this._unregister = callback(container, this._onScroll);
}
}
private _unregisterScrollEvent() {
this._unregister?.();
this._unregister = null;
}
private _onScroll = () => {
const options = this._currentOptions;
if (this._lock || !options) {
return;
}
this.emit("scrollDrag", {
next: (inputEvent: any) => {
this.checkScroll({
container: options.container,
inputEvent,
});
},
});
};
}
export default DragScroll;