@inizioevoke/astro-core 1.6.5 → 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 +2 -2
- package/src/lib/gestures/index.ts +2 -0
- package/src/lib/gestures/pinch.ts +66 -0
- package/src/lib/gestures/swipe.ts +153 -0
- package/src/lib/swipe.ts +0 -109
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"
|
|
@@ -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
|
-
}
|