-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
179 lines (162 loc) · 4.04 KB
/
index.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
import { getProperty } from '@joyfulljs/vendor-property';
import XTouch from '@joyfulljs/xtouch';
/**
* `transform` property name with browser vendor prefix if needed.
*/
export const transformProperty = getProperty('transform');
/**
* make a element draggable
* @param el target html element
* @param options options
*/
export default function Draggable(el: HTMLElement, options: IOptions) {
let { onMoving, onStart, onEnd, maxX, maxY, minX, minY, useCapture } = options || {};
let startX: number = 0, startY: number = 0;
let beginX: number = 0, beginY: number = 0;
let isTouchDown: boolean = false;
const unbind = XTouch(el, { onStart: handleDown, onMove: handleMove, onEnd: handleUp, capture: useCapture });
const oldParts = getTransform(el);
function handleDown(e: TouchEvent) {
isTouchDown = true;
beginX = startX = e.touches[0].pageX;
beginY = startY = e.touches[0].pageY;
onStart && onStart(e);
}
function handleMove(e: TouchEvent) {
if (isTouchDown) {
const touch = e.touches[0];
let parts: Array<string | number> = getTransform(el);
let deltX = touch.pageX - startX;
let deltY = touch.pageY - startY;
let x = deltX + +parts[4];
let y = deltY + +parts[5];
startX = touch.pageX;
startY = touch.pageY;
// take transform: scale into consideration
// TODO: does transformOrigin affect the result?
// if (parts[0] != 1) {
// maxX *= +parts[0];
// minX *= +parts[0];
// }
// if (parts[3] != 1) {
// maxY *= +parts[3];
// minY *= +parts[3];
// }
if (x > maxX) {
x = maxX
} else if (x < minX) {
x = minX
}
if (y > maxY) {
y = maxY
} else if (y < minY) {
y = minY
}
if (onMoving && onMoving({
totalDeltX: touch.pageX - beginX,
totalDeltY: touch.pageY - beginY,
deltX,
deltY,
originalEvent: e
}) === false) {
return
}
parts[4] = x;
parts[5] = y;
// @ts-ignore ts handle string index incorrectly, so ignore it.
el.style[transformProperty] = `matrix(${parts.join(',')})`;
}
}
function handleUp(e: TouchEvent) {
isTouchDown = false;
onEnd && onEnd(e)
}
function reset() {
const parts = getTransform(el);
parts[4] = oldParts[4];
parts[5] = oldParts[5];
// @ts-ignore
el.style[transformProperty] = `matrix(${parts.join(',')})`;
}
return {
reset,
destroy: unbind
}
}
/**
* get computed style of transform
* @param el target html element
*/
export function getTransform(el: HTMLElement): string[] {
// @ts-ignore
let transform = window.getComputedStyle(el)[transformProperty]
if (!transform || transform === 'none') {
transform = 'matrix(1, 0, 0, 1, 0, 0)'
}
return transform.replace(/\(|\)|matrix|\s+/g, '').split(',');
}
/**
* instance configraton options
*/
export interface IOptions {
/**
* triggered when touchmove/mousemove
* return false to cancel this movement.
* @param e e
*/
onMoving(e: IMoveEvent): boolean;
/**
* triggered when touchstart/mousedown
* @param e e
*/
onStart(e: TouchEvent): boolean;
/**
* triggered when touchend/mouseup
* @param e e
*/
onEnd(e: TouchEvent): void;
/**
* x轴正向最大拖动
*/
maxX?: number;
/**
* y轴正向最大拖动
*/
maxY?: number;
/**
* x轴负向最大拖动
*/
minX?: number;
/**
* y轴负向最大拖动
*/
minY?: number;
/**
* use capture phase for the underneth event binding.
*/
useCapture?: boolean
}
export interface IMoveEvent {
/**
* total move distance for x direction since start
*/
totalDeltX: number;
/**
* total move distance for Y direction since start
*/
totalDeltY: number;
/**
* move distance for x direction
*/
deltX: number;
/**
* move distance for y direction
*/
deltY: number;
/**
* the original event argument
* TouchEvent for touch device
* MouseEvent for none-touch device
*/
originalEvent: TouchEvent;
}