@inizioevoke/astro-core 1.6.4 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inizioevoke/astro-core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"./lib/Modal": "./src/components/Modal/index.ts",
|
|
15
15
|
"./lib/PdfViewer": "./src/components/PdfViewer/index.ts",
|
|
16
16
|
"./lib/ScrollContainer": "./src/components/ScrollContainer/index.ts",
|
|
17
|
-
"./lib/
|
|
17
|
+
"./lib/gestures": "./src/lib/gestures/index.ts",
|
|
18
18
|
"./v2": "./src/components/v2.ts",
|
|
19
19
|
"./v2/lib": "./src/lib/v2.ts",
|
|
20
20
|
"./v2/lib/Modal": "./src/components/Modal/v2/index.ts"
|
|
@@ -11,11 +11,16 @@ interface Props {
|
|
|
11
11
|
zoom?: 50 | 60 | 70 | 80 | 90 | 100 | 110 | 120 | 130 | 140 | 150;
|
|
12
12
|
page?: number;
|
|
13
13
|
pages?: string;
|
|
14
|
-
bookmarks?: Record<string, number>;
|
|
15
|
-
bookmarkText?: string;
|
|
14
|
+
// bookmarks?: Record<string, number>;
|
|
15
|
+
// bookmarkText?: string;
|
|
16
16
|
toolbar?: {
|
|
17
|
+
nav?: boolean;
|
|
17
18
|
page?: boolean;
|
|
18
19
|
zoom?: boolean;
|
|
20
|
+
bookmarks?: {
|
|
21
|
+
placeholder?: string;
|
|
22
|
+
items: Record<string, number>;
|
|
23
|
+
}
|
|
19
24
|
}
|
|
20
25
|
}
|
|
21
26
|
const {
|
|
@@ -26,8 +31,6 @@ const {
|
|
|
26
31
|
pdf,
|
|
27
32
|
zoom,
|
|
28
33
|
page = 1,
|
|
29
|
-
bookmarks,
|
|
30
|
-
bookmarkText = 'Jump to',
|
|
31
34
|
toolbar,
|
|
32
35
|
...rest
|
|
33
36
|
} = Astro.props;
|
|
@@ -50,7 +53,7 @@ if (height) {
|
|
|
50
53
|
<slot name="toolbar-start" />
|
|
51
54
|
</div>
|
|
52
55
|
<div class="evo-pdf-viewer-toolbar-controls">
|
|
53
|
-
<div class="evo-pdf-viewer-toolbar-arrows">
|
|
56
|
+
<div class:list={["evo-pdf-viewer-toolbar-arrows", toolbar?.nav === false ? 'evo-hidden' : null]}>
|
|
54
57
|
<button class="evo-pdf-viewer-toolbar-prev" title="Previous">
|
|
55
58
|
<slot name="toolbar-prev">
|
|
56
59
|
<svg class="evo-pdf-viewer-toolbar-icon" width="20px" height="20px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
@@ -69,8 +72,7 @@ if (height) {
|
|
|
69
72
|
<div class:list={['evo-pdf-viewer-nav-page', toolbar?.page === false ? 'evo-hidden' : null]}>
|
|
70
73
|
<select><option value="1">1</option></select> of <span class="evo-pdf-viewer-nav-page-count">1</span>
|
|
71
74
|
</div>
|
|
72
|
-
{toolbar?.zoom
|
|
73
|
-
<div class="evo-pdf-viewer-toolbar-zoom">
|
|
75
|
+
<div class:list={['evo-pdf-viewer-toolbar-zoom', toolbar?.zoom === false ? 'evo-hidden' : null]}>
|
|
74
76
|
<button class="evo-pdf-viewer-toolbar-zoom-out" title="Zoom out">
|
|
75
77
|
<slot name="toolbar-zoom-out">
|
|
76
78
|
<svg class="evo-pdf-viewer-toolbar-icon" width="16px" height="16px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
@@ -91,12 +93,12 @@ if (height) {
|
|
|
91
93
|
</svg>
|
|
92
94
|
</slot>
|
|
93
95
|
</button>
|
|
94
|
-
</div>
|
|
95
|
-
{bookmarks && Object.keys(bookmarks).length > 0 &&
|
|
96
|
+
</div>
|
|
97
|
+
{toolbar?.bookmarks && Object.keys(toolbar.bookmarks.items).length > 0 &&
|
|
96
98
|
<div class="evo-pdf-viewer-toolbar-bookmarks">
|
|
97
99
|
<select>
|
|
98
|
-
<option value="" disabled selected hidden>{
|
|
99
|
-
{Object.entries(bookmarks).map((b) => (
|
|
100
|
+
<option value="" disabled selected hidden>{toolbar.bookmarks.placeholder ?? 'Jump To'}</option>
|
|
101
|
+
{Object.entries(toolbar.bookmarks.items).map((b) => (
|
|
100
102
|
<option value={b[1]}>{b[0]}</option>
|
|
101
103
|
))}
|
|
102
104
|
</select>
|
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
.evo-pdf-viewer-toolbar {
|
|
17
|
+
--evo-pdf-viewer-toolbar-font-color: #333333;
|
|
18
|
+
--evo-pdf-viewer-toolbar-separator-color: #cccccc;
|
|
19
|
+
--evo-pdf-viewer-toolbar-hover: #dddddd;
|
|
17
20
|
display: flex;
|
|
18
21
|
justify-content: var(--evo-pdf-viewer-toolbar-align);
|
|
19
22
|
align-items: center;
|
|
@@ -48,13 +51,18 @@
|
|
|
48
51
|
background-color: transparent;
|
|
49
52
|
/* border: 1px solid #888888;
|
|
50
53
|
border-radius: 6px; */
|
|
51
|
-
&:
|
|
52
|
-
background-color:
|
|
54
|
+
&:active {
|
|
55
|
+
background-color: var(--evo-pdf-viewer-toolbar-hover);
|
|
56
|
+
}
|
|
57
|
+
@media (hover: hover) {
|
|
58
|
+
&:hover {
|
|
59
|
+
background-color: var(--evo-pdf-viewer-toolbar-hover);
|
|
60
|
+
}
|
|
53
61
|
}
|
|
54
62
|
|
|
55
63
|
.evo-pdf-viewer-toolbar-icon {
|
|
56
64
|
path {
|
|
57
|
-
stroke:
|
|
65
|
+
stroke: var(--evo-pdf-viewer-toolbar-font-color);
|
|
58
66
|
}
|
|
59
67
|
}
|
|
60
68
|
}
|
|
@@ -64,7 +72,7 @@
|
|
|
64
72
|
height: 24px;
|
|
65
73
|
padding: 0;
|
|
66
74
|
cursor: pointer;
|
|
67
|
-
color:
|
|
75
|
+
color: var(--evo-pdf-viewer-toolbar-font-color);
|
|
68
76
|
font-family: sans-serif;
|
|
69
77
|
font-size: 16px;
|
|
70
78
|
line-height: 24px;
|
|
@@ -79,7 +87,8 @@
|
|
|
79
87
|
height: 30px;
|
|
80
88
|
margin: 0;
|
|
81
89
|
padding: 0 0 0 8px;
|
|
82
|
-
border-left: 1px solid
|
|
90
|
+
border-left: 1px solid var(--evo-pdf-viewer-toolbar-separator-color);
|
|
91
|
+
color: var(--evo-pdf-viewer-toolbar-font-color);
|
|
83
92
|
font-family: sans-serif;
|
|
84
93
|
font-size: 16px;
|
|
85
94
|
line-height: 30px;
|
|
@@ -133,6 +142,7 @@
|
|
|
133
142
|
.evo-scroll-container {
|
|
134
143
|
--evo-scroll-container-track-color: #cccccc;
|
|
135
144
|
.evo-scroll-content {
|
|
145
|
+
overflow-x: hidden;
|
|
136
146
|
.evo-pdf-viewer-page-wrapper {
|
|
137
147
|
display: block;
|
|
138
148
|
margin: 18px auto;
|
|
@@ -167,7 +177,7 @@
|
|
|
167
177
|
}
|
|
168
178
|
}
|
|
169
179
|
|
|
170
|
-
&.evo-pdf-viewer-theme-modern {
|
|
180
|
+
/* &.evo-pdf-viewer-theme-modern {
|
|
171
181
|
.evo-pdf-viewer-toolbar {
|
|
172
182
|
button {
|
|
173
183
|
background-color: transparent;
|
|
@@ -178,30 +188,17 @@
|
|
|
178
188
|
}
|
|
179
189
|
}
|
|
180
190
|
}
|
|
181
|
-
}
|
|
191
|
+
} */
|
|
182
192
|
&.evo-pdf-viewer-theme-dark {
|
|
183
193
|
background-color: #555555;
|
|
184
194
|
.evo-pdf-viewer-toolbar {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
.evo-pdf-viewer-toolbar-icon {
|
|
190
|
-
path {
|
|
191
|
-
stroke: #ffffff;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
+
--evo-pdf-viewer-toolbar-font-color: white;
|
|
196
|
+
--evo-pdf-viewer-toolbar-separator-color: #888888;
|
|
197
|
+
--evo-pdf-viewer-toolbar-hover: #333333;
|
|
195
198
|
select {
|
|
196
|
-
color: white;
|
|
197
199
|
border: 1px solid rgba(255,255,255,0.25);
|
|
198
200
|
background-color: #333333;
|
|
199
201
|
}
|
|
200
|
-
.evo-pdf-viewer-nav-page,
|
|
201
|
-
.evo-pdf-viewer-toolbar-bookmarks {
|
|
202
|
-
color: #ffffff;
|
|
203
|
-
border-color: #cccccc;
|
|
204
|
-
}
|
|
205
202
|
}
|
|
206
203
|
.evo-pdf-viewer-container {
|
|
207
204
|
.evo-scroll-container {
|
|
@@ -213,26 +210,13 @@
|
|
|
213
210
|
&.evo-pdf-viewer-theme-black {
|
|
214
211
|
background-color: #000000;
|
|
215
212
|
.evo-pdf-viewer-toolbar {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
.evo-pdf-viewer-toolbar-icon {
|
|
221
|
-
path {
|
|
222
|
-
stroke: #ffffff;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
213
|
+
--evo-pdf-viewer-toolbar-font-color: white;
|
|
214
|
+
--evo-pdf-viewer-toolbar-separator-color: #666666;
|
|
215
|
+
--evo-pdf-viewer-toolbar-hover: #666666;
|
|
226
216
|
select {
|
|
227
|
-
color: white;
|
|
228
217
|
border: 1px solid rgba(255,255,255,0.15);
|
|
229
218
|
background-color: #333333;
|
|
230
219
|
}
|
|
231
|
-
.evo-pdf-viewer-nav-page,
|
|
232
|
-
.evo-pdf-viewer-toolbar-bookmarks {
|
|
233
|
-
color: #ffffff;
|
|
234
|
-
border-color: #cccccc;
|
|
235
|
-
}
|
|
236
220
|
}
|
|
237
221
|
.evo-pdf-viewer-container {
|
|
238
222
|
.evo-scroll-container {
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export function createPinchListener() {
|
|
2
|
+
type PinchDirection = 'in' | 'out';
|
|
3
|
+
|
|
4
|
+
let initialDist: number = 0;
|
|
5
|
+
let lastDistance: number = 0;
|
|
6
|
+
let currentScale: number = 1;
|
|
7
|
+
let activeScale: number = 1;
|
|
8
|
+
|
|
9
|
+
function getTouchDistance(touches: TouchList) {
|
|
10
|
+
return Math.hypot(touches[0].clientX - touches[1].clientX, touches[0].clientY - touches[1].clientY);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function reset() {
|
|
14
|
+
window.removeEventListener('touchmove', touchmove);
|
|
15
|
+
window.removeEventListener('touchend', touchend);
|
|
16
|
+
currentScale = activeScale;
|
|
17
|
+
initialDist = 0;
|
|
18
|
+
lastDistance = 0;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const touchstart = (e: TouchEvent) => {
|
|
22
|
+
if (e.touches.length === 2) {
|
|
23
|
+
initialDist = getTouchDistance(e.touches);
|
|
24
|
+
lastDistance = initialDist;
|
|
25
|
+
window.addEventListener('touchmove', touchmove, { passive: true });
|
|
26
|
+
window.addEventListener('touchend', touchend, { passive: true });
|
|
27
|
+
} else {
|
|
28
|
+
reset();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const touchmove = (e: TouchEvent) => {
|
|
33
|
+
if (e.touches.length === 2) {
|
|
34
|
+
const currentDistance = getTouchDistance(e.touches);
|
|
35
|
+
const ratio = currentDistance / initialDist;
|
|
36
|
+
|
|
37
|
+
const rawScale = currentScale * ratio;
|
|
38
|
+
|
|
39
|
+
// Detect if we're crossing 1 from either direction
|
|
40
|
+
const crossingOne =
|
|
41
|
+
(currentScale > 1 && rawScale < 1) ||
|
|
42
|
+
(currentScale < 1 && rawScale > 1);
|
|
43
|
+
|
|
44
|
+
activeScale = crossingOne ? 1 : rawScale; // snap to 1
|
|
45
|
+
|
|
46
|
+
let direction: PinchDirection | undefined = undefined;
|
|
47
|
+
if (currentDistance > lastDistance) {
|
|
48
|
+
direction = 'in';
|
|
49
|
+
} else if (currentDistance < lastDistance) {
|
|
50
|
+
direction = 'out';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (direction !== undefined) {
|
|
54
|
+
console.log(activeScale, direction);
|
|
55
|
+
lastDistance = currentDistance;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const touchend = (e: TouchEvent) => {
|
|
61
|
+
if (e.touches.length < 2) {
|
|
62
|
+
reset();
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
window.addEventListener('touchstart', touchstart, { passive: true });
|
|
66
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
export type SwipeDirection = 'left' | 'right' | 'left-right' | 'up' | 'down' | 'up-down';
|
|
2
|
+
export type SwipeThreshold = '1/4' | '1/3' | '1/2' | number;
|
|
3
|
+
|
|
4
|
+
export interface OnSwipeHandlerArgs {
|
|
5
|
+
direction: SwipeDirection;
|
|
6
|
+
threshold: number;
|
|
7
|
+
distance: {
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
},
|
|
11
|
+
duration: number;
|
|
12
|
+
}
|
|
13
|
+
export type OnSwipeHandler = (args: OnSwipeHandlerArgs) => void;
|
|
14
|
+
|
|
15
|
+
export interface SwipeController {
|
|
16
|
+
enable: () => void;
|
|
17
|
+
disable: () => void;
|
|
18
|
+
dispose: () => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function createSwipeListener(direction: SwipeDirection, threshold: SwipeThreshold, handler: OnSwipeHandler): SwipeController {
|
|
22
|
+
const swipeThreshold: number = typeof threshold == 'number' ? threshold : (() => {
|
|
23
|
+
const ratios: Record<string, number> = { '1/4': 0.25, '1/3': 0.333, '1/2': 0.5 };
|
|
24
|
+
switch (direction) {
|
|
25
|
+
case 'up':
|
|
26
|
+
case 'down':
|
|
27
|
+
case 'up-down':
|
|
28
|
+
return Math.round(window.innerHeight * (ratios[threshold] ?? ratios['1/3'])); // screen.height
|
|
29
|
+
default:
|
|
30
|
+
return Math.round(window.innerWidth * (ratios[threshold] ?? ratios['1/3'])); // screen.width
|
|
31
|
+
}
|
|
32
|
+
})();
|
|
33
|
+
|
|
34
|
+
let listening = false;
|
|
35
|
+
let x: number = NaN;
|
|
36
|
+
let y: number = NaN;
|
|
37
|
+
let start: number = NaN;
|
|
38
|
+
|
|
39
|
+
const isUp = (deltaX: number, deltaY: number) => {
|
|
40
|
+
return Math.abs(deltaY) >= swipeThreshold && Math.abs(deltaX) < swipeThreshold && deltaY > 0;
|
|
41
|
+
}
|
|
42
|
+
const isDown = (deltaX: number, deltaY: number) => {
|
|
43
|
+
return Math.abs(deltaY) >= swipeThreshold && Math.abs(deltaX) < swipeThreshold && deltaY < 0;
|
|
44
|
+
}
|
|
45
|
+
const isLeft = (deltaX: number, deltaY: number) => {
|
|
46
|
+
return Math.abs(deltaX) >= swipeThreshold && Math.abs(deltaY) < swipeThreshold && deltaX > 0;
|
|
47
|
+
}
|
|
48
|
+
const isRight = (deltaX: number, deltaY: number) => {
|
|
49
|
+
return Math.abs(deltaX) >= swipeThreshold && Math.abs(deltaY) < swipeThreshold && deltaX < 0;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const handleSwipe = (deltaX: number, deltaY: number, duration: number) => {
|
|
53
|
+
let swipeDirection: SwipeDirection | undefined = undefined;
|
|
54
|
+
switch (direction) {
|
|
55
|
+
case 'up':
|
|
56
|
+
if (isUp(deltaX, deltaY)) {
|
|
57
|
+
swipeDirection = 'up';
|
|
58
|
+
}
|
|
59
|
+
break;
|
|
60
|
+
case 'down':
|
|
61
|
+
if (isDown(deltaX, deltaY)) {
|
|
62
|
+
swipeDirection = 'down';
|
|
63
|
+
}
|
|
64
|
+
break;
|
|
65
|
+
case 'up-down':
|
|
66
|
+
if (isUp(deltaX, deltaY)) {
|
|
67
|
+
swipeDirection = 'up';
|
|
68
|
+
} else if (isDown(deltaX, deltaY)) {
|
|
69
|
+
swipeDirection = 'down';
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
case 'left':
|
|
73
|
+
if (isLeft(deltaX, deltaY)) {
|
|
74
|
+
swipeDirection = 'left';
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
77
|
+
case 'right':
|
|
78
|
+
if (isRight(deltaX, deltaY)) {
|
|
79
|
+
swipeDirection = 'right';
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
case 'left-right':
|
|
83
|
+
if (isLeft(deltaX, deltaY)) {
|
|
84
|
+
swipeDirection = 'left';
|
|
85
|
+
} else if (isRight(deltaX, deltaY)) {
|
|
86
|
+
swipeDirection = 'right';
|
|
87
|
+
}
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
if (swipeDirection) {
|
|
91
|
+
try {
|
|
92
|
+
handler({ direction: swipeDirection, threshold: swipeThreshold, distance: { x: deltaX, y: deltaY }, duration});
|
|
93
|
+
} catch (err) {
|
|
94
|
+
console.error(err);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const touchstart = (e: TouchEvent) => {
|
|
100
|
+
if (!listening) return;
|
|
101
|
+
if (e.touches.length === 1 && isNaN(x)) {
|
|
102
|
+
start = performance.now();
|
|
103
|
+
x = e.touches[0].clientX;
|
|
104
|
+
y = e.touches[0].clientY;
|
|
105
|
+
} else {
|
|
106
|
+
start = NaN;
|
|
107
|
+
x = NaN;
|
|
108
|
+
y = NaN;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const touchcancel = () => {
|
|
113
|
+
x = NaN;
|
|
114
|
+
y = NaN;
|
|
115
|
+
start = NaN;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const touchend = (e: TouchEvent) => {
|
|
119
|
+
if (!listening) return;
|
|
120
|
+
if (e.changedTouches.length === 1 && !isNaN(x) && !isNaN(y)) {
|
|
121
|
+
const deltaX = x - e.changedTouches[0].clientX;
|
|
122
|
+
const deltaY = y - e.changedTouches[0].clientY;
|
|
123
|
+
handleSwipe(deltaX, deltaY, performance.now() - start);
|
|
124
|
+
x = NaN;
|
|
125
|
+
y = NaN;
|
|
126
|
+
start = NaN;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const enable = () => {
|
|
131
|
+
if (!listening) {
|
|
132
|
+
listening = true;
|
|
133
|
+
window.addEventListener('touchstart', touchstart, { passive: true });
|
|
134
|
+
window.addEventListener('touchend', touchend, { passive: true });
|
|
135
|
+
window.addEventListener('touchcancel', touchcancel, { passive: true });
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const disable = () => {
|
|
140
|
+
listening = false;
|
|
141
|
+
window.removeEventListener('touchstart', touchstart);
|
|
142
|
+
window.removeEventListener('touchend', touchend);
|
|
143
|
+
window.removeEventListener('touchcancel', touchcancel);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
enable();
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
enable,
|
|
150
|
+
disable,
|
|
151
|
+
dispose: disable
|
|
152
|
+
};
|
|
153
|
+
}
|
package/src/lib/swipe.ts
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
export type SwipeDirection = 'left' | 'right' | 'up' | 'down';
|
|
2
|
-
export type SwipeThreshold = '1/4' | '1/3' | '1/2' | number;
|
|
3
|
-
|
|
4
|
-
export interface OnSwipeHandlerArgs {
|
|
5
|
-
direction: SwipeDirection;
|
|
6
|
-
threshold: number;
|
|
7
|
-
distance: {
|
|
8
|
-
x: number;
|
|
9
|
-
y: number;
|
|
10
|
-
},
|
|
11
|
-
duration: number;
|
|
12
|
-
}
|
|
13
|
-
export type OnSwipeHandler = (args: OnSwipeHandlerArgs) => void;
|
|
14
|
-
|
|
15
|
-
export interface SwipeController {
|
|
16
|
-
enable: () => void;
|
|
17
|
-
disable: () => void;
|
|
18
|
-
dispose: () => void;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export default function onSwipe(direction: SwipeDirection, swipeThreshold: SwipeThreshold, handler: OnSwipeHandler): SwipeController {
|
|
22
|
-
const threshold: number = typeof swipeThreshold == 'number' ? swipeThreshold : (() => {
|
|
23
|
-
const ratios: Record<string, number> = { '1/4': 0.25, '1/3': 0.333, '1/2': 0.5 };
|
|
24
|
-
switch (direction) {
|
|
25
|
-
case 'up':
|
|
26
|
-
case 'down':
|
|
27
|
-
return Math.round(screen.height * (ratios[swipeThreshold] ?? ratios['1/3']));
|
|
28
|
-
default:
|
|
29
|
-
return Math.round(screen.width * (ratios[swipeThreshold] ?? ratios['1/3']));
|
|
30
|
-
}
|
|
31
|
-
})();
|
|
32
|
-
|
|
33
|
-
let listening = false;
|
|
34
|
-
let x: number = NaN;
|
|
35
|
-
let y: number = NaN;
|
|
36
|
-
let start: number = NaN;
|
|
37
|
-
|
|
38
|
-
const handleSwipe = (deltaX: number, deltaY: number, duration: number) => {
|
|
39
|
-
let flag = false;
|
|
40
|
-
switch (direction) {
|
|
41
|
-
case 'up':
|
|
42
|
-
flag = Math.abs(deltaY) >= threshold && Math.abs(deltaX) < threshold && deltaY > 0;
|
|
43
|
-
break;
|
|
44
|
-
case 'down':
|
|
45
|
-
flag = Math.abs(deltaY) >= threshold && Math.abs(deltaX) < threshold && deltaY < 0;
|
|
46
|
-
break;
|
|
47
|
-
case 'left':
|
|
48
|
-
flag = Math.abs(deltaX) >= threshold && Math.abs(deltaY) < threshold && deltaX > 0;
|
|
49
|
-
break;
|
|
50
|
-
case 'right':
|
|
51
|
-
flag = Math.abs(deltaX) >= threshold && Math.abs(deltaY) < threshold && deltaX < 0;
|
|
52
|
-
break;
|
|
53
|
-
}
|
|
54
|
-
if (flag) {
|
|
55
|
-
try {
|
|
56
|
-
handler({ direction, threshold, distance: { x: deltaX, y: deltaY }, duration});
|
|
57
|
-
} catch (err) {
|
|
58
|
-
console.error(err);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const touchstart = (e: TouchEvent) => {
|
|
64
|
-
if (!listening) return;
|
|
65
|
-
if (e.touches.length === 1 && isNaN(x)) {
|
|
66
|
-
start = performance.now();
|
|
67
|
-
x = e.touches[0].clientX;
|
|
68
|
-
y = e.touches[0].clientY;
|
|
69
|
-
} else {
|
|
70
|
-
start = NaN;
|
|
71
|
-
x = NaN;
|
|
72
|
-
y = NaN;
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
const touchend = (e: TouchEvent) => {
|
|
77
|
-
if (!listening) return;
|
|
78
|
-
if (e.changedTouches.length === 1 && !isNaN(x) && !isNaN(y)) {
|
|
79
|
-
const deltaX = x - e.changedTouches[0].clientX;
|
|
80
|
-
const deltaY = y - e.changedTouches[0].clientY;
|
|
81
|
-
handleSwipe(deltaX, deltaY, performance.now() - start);
|
|
82
|
-
x = NaN;
|
|
83
|
-
y = NaN;
|
|
84
|
-
start = NaN;
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
const enable = () => {
|
|
89
|
-
if (!listening) {
|
|
90
|
-
listening = true;
|
|
91
|
-
window.addEventListener('touchstart', touchstart, { passive: true });
|
|
92
|
-
window.addEventListener('touchend', touchend, { passive: true });
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const disable = () => {
|
|
97
|
-
listening = false;
|
|
98
|
-
window.removeEventListener('touchstart', touchstart);
|
|
99
|
-
window.removeEventListener('touchend', touchend);
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
enable();
|
|
103
|
-
|
|
104
|
-
return {
|
|
105
|
-
enable,
|
|
106
|
-
disable,
|
|
107
|
-
dispose: disable
|
|
108
|
-
};
|
|
109
|
-
}
|