gesturx 0.0.0 → 1.0.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/.prettierrc.json +3 -1
- package/eslint.config.js +18 -9
- package/index.html +2 -2
- package/package.json +16 -3
- package/src/core/index.ts +4 -0
- package/src/core/manager.ts +36 -35
- package/src/core/pan/PanRecognizer.ts +192 -0
- package/src/core/pinch/PinchRecognizer.ts +131 -0
- package/src/core/recognizer/Recognizer.ts +54 -0
- package/src/core/recognizer/RecognizerEvent.ts +39 -0
- package/src/core/recognizer/mapToRecognizerEvent.ts +142 -0
- package/src/core/rotate/RotateRecognizer.ts +115 -0
- package/src/core/swipe/SwipeRecognizer.test.ts +160 -0
- package/src/core/swipe/SwipeRecognizer.ts +82 -0
- package/src/core/swipe/isRecognizedAsSwipe.ts +13 -0
- package/src/core/{tapRecognizer.test.ts → tap/TapRecognizer.test.ts} +162 -137
- package/src/core/tap/TapRecognizer.ts +172 -0
- package/src/core/utils/events.ts +186 -0
- package/src/core/utils/geometry.ts +115 -0
- package/src/core/utils/utils.test.ts +26 -0
- package/src/core/utils/utils.ts +58 -0
- package/src/demo/App.tsx +171 -0
- package/src/demo/DebugBox.tsx +10 -0
- package/src/demo/useManager.ts +40 -0
- package/src/demo/useTap.tsx +87 -0
- package/src/index.css +66 -0
- package/src/main.tsx +14 -0
- package/src/theme.ts +11 -0
- package/tsconfig.json +5 -2
- package/tsconfig.node.json +11 -0
- package/vite.config.ts +34 -29
- package/vitest.config.ts +2 -2
- package/src/core/tapRecognizer.ts +0 -143
- package/src/core/types.ts +0 -3
- package/src/core/utils.ts +0 -211
- package/src/main.ts +0 -19
- package/src/style.css +0 -29
package/src/demo/App.tsx
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { useEffect, useState } from "react"
|
|
2
|
+
import { PanRecognizer } from "../core/pan/PanRecognizer"
|
|
3
|
+
import { Box, Stack, Text, useToast } from "@chakra-ui/react"
|
|
4
|
+
import { ArrowForwardIcon } from "@chakra-ui/icons"
|
|
5
|
+
import { DebugBox } from "./DebugBox"
|
|
6
|
+
import { useManager } from "./useManager"
|
|
7
|
+
import { useTap } from "./useTap"
|
|
8
|
+
|
|
9
|
+
function App() {
|
|
10
|
+
const toast = useToast()
|
|
11
|
+
const [container, setContainer] = useState<HTMLElement | undefined>()
|
|
12
|
+
const { manager } = useManager(container)
|
|
13
|
+
const { tapDebug } = useTap(manager)
|
|
14
|
+
const [boxAngle, setBoxAngle] = useState(0)
|
|
15
|
+
const [boxScale, setBoxScale] = useState(1)
|
|
16
|
+
const [numberOfFingers, setNumberOfFingers] = useState(0)
|
|
17
|
+
const [rotation, setRotation] = useState(0)
|
|
18
|
+
const [scale, setScale] = useState(1)
|
|
19
|
+
const [distance, setDistance] = useState(0)
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
const rotateSub = manager.events$.subscribe((e) => {
|
|
23
|
+
if (e.type === "rotate") {
|
|
24
|
+
setBoxAngle((state) => state + e.deltaAngle)
|
|
25
|
+
setRotation(e.angle)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (e.type === "rotateEnd") {
|
|
29
|
+
setRotation(0)
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const pinchSub = manager.events$.subscribe((e) => {
|
|
34
|
+
if (e.type === "pinchMove") {
|
|
35
|
+
setBoxScale((value) => value * e.deltaDistanceScale)
|
|
36
|
+
setScale(e.scale)
|
|
37
|
+
setDistance(e.distance)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (e.type === "pinchEnd") {
|
|
41
|
+
setScale(1)
|
|
42
|
+
setDistance(0)
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const swipeSub = manager.events$.subscribe((e) => {
|
|
47
|
+
if (e.type === "swipe") {
|
|
48
|
+
toast({
|
|
49
|
+
title: "Swipe",
|
|
50
|
+
description: (
|
|
51
|
+
<Stack>
|
|
52
|
+
<Text display="flex" gap={4}>
|
|
53
|
+
Direction:{" "}
|
|
54
|
+
<ArrowForwardIcon
|
|
55
|
+
boxSize={6}
|
|
56
|
+
transform={`rotate(${e.angle}deg)`}
|
|
57
|
+
/>{" "}
|
|
58
|
+
</Text>
|
|
59
|
+
<Text display="flex" gap={4}>
|
|
60
|
+
Velocity: X: <b>{e.velocityX.toFixed(2)}</b> / Y:{" "}
|
|
61
|
+
<b>{e.velocityY.toFixed(2)} </b>
|
|
62
|
+
</Text>
|
|
63
|
+
</Stack>
|
|
64
|
+
),
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* To track active fingers, listen for the fingers$ observable on one of
|
|
71
|
+
* your pan recognizer which is configured in a way to track all fingers.
|
|
72
|
+
*/
|
|
73
|
+
const fingerTrackingSub = manager.recognizers
|
|
74
|
+
.find(
|
|
75
|
+
(recognizer): recognizer is PanRecognizer =>
|
|
76
|
+
recognizer instanceof PanRecognizer,
|
|
77
|
+
)
|
|
78
|
+
?.fingers$.subscribe((fingers) => {
|
|
79
|
+
setNumberOfFingers(fingers)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Moving a box on x,y axis.
|
|
84
|
+
* We track the deltaX/Y values.
|
|
85
|
+
*/
|
|
86
|
+
let latestBoxPosition = { x: 0, y: 0 }
|
|
87
|
+
const sub3 = manager.events$.subscribe((event) => {
|
|
88
|
+
if (
|
|
89
|
+
event.type === "panMove" ||
|
|
90
|
+
event.type === "panEnd" ||
|
|
91
|
+
event.type === "panStart"
|
|
92
|
+
) {
|
|
93
|
+
const boxElement = document.getElementById(`boxContainer`)
|
|
94
|
+
|
|
95
|
+
if (boxElement) {
|
|
96
|
+
const domRect = boxElement.getBoundingClientRect()
|
|
97
|
+
|
|
98
|
+
if (event.type === "panStart") {
|
|
99
|
+
latestBoxPosition.x = domRect.left
|
|
100
|
+
latestBoxPosition.y = domRect.top
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
boxElement.style.left = `${latestBoxPosition.x + event.deltaX}px`
|
|
104
|
+
boxElement.style.top = `${latestBoxPosition.y + event.deltaY}px`
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* track center.
|
|
111
|
+
* We use the center value, which indicate the true center
|
|
112
|
+
* no matter how many fingers
|
|
113
|
+
*/
|
|
114
|
+
const sub = manager.events$.subscribe((event) => {
|
|
115
|
+
const boxElement = document.getElementById(`boxCenter`)
|
|
116
|
+
|
|
117
|
+
if (boxElement) {
|
|
118
|
+
boxElement.style.left = `${event.center.x - boxElement.offsetWidth / 2}px`
|
|
119
|
+
boxElement.style.top = `${event.center.y - boxElement.offsetHeight / 2}px`
|
|
120
|
+
}
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
return () => {
|
|
124
|
+
sub.unsubscribe()
|
|
125
|
+
swipeSub.unsubscribe()
|
|
126
|
+
fingerTrackingSub?.unsubscribe()
|
|
127
|
+
sub3.unsubscribe()
|
|
128
|
+
rotateSub.unsubscribe()
|
|
129
|
+
pinchSub.unsubscribe()
|
|
130
|
+
}
|
|
131
|
+
}, [manager])
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<>
|
|
135
|
+
<Stack position="absolute" right={0} top={0} pr={2} pt={2} zIndex={1}>
|
|
136
|
+
<DebugBox>fingers: {numberOfFingers}</DebugBox>
|
|
137
|
+
{tapDebug}
|
|
138
|
+
<DebugBox>
|
|
139
|
+
rotation:{" "}
|
|
140
|
+
<ArrowForwardIcon boxSize={6} transform={`rotate(${rotation}deg)`} />{" "}
|
|
141
|
+
</DebugBox>
|
|
142
|
+
<DebugBox>
|
|
143
|
+
pinch:
|
|
144
|
+
<Text>
|
|
145
|
+
{scale.toFixed(1)}% {distance.toFixed(0)}px
|
|
146
|
+
</Text>
|
|
147
|
+
</DebugBox>
|
|
148
|
+
</Stack>
|
|
149
|
+
<Box
|
|
150
|
+
id="container"
|
|
151
|
+
ref={(element) => {
|
|
152
|
+
if (element) {
|
|
153
|
+
setContainer(element)
|
|
154
|
+
}
|
|
155
|
+
}}
|
|
156
|
+
>
|
|
157
|
+
<Box id="boxContainer">
|
|
158
|
+
<div
|
|
159
|
+
id="box"
|
|
160
|
+
style={{
|
|
161
|
+
transform: `rotate(${boxAngle}deg) scale(${boxScale})`,
|
|
162
|
+
}}
|
|
163
|
+
></div>
|
|
164
|
+
</Box>
|
|
165
|
+
<div id="boxCenter"></div>
|
|
166
|
+
</Box>
|
|
167
|
+
</>
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export default App
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useEffect, useState } from "react"
|
|
2
|
+
import {
|
|
3
|
+
PanRecognizer,
|
|
4
|
+
SwipeRecognizer,
|
|
5
|
+
TapRecognizer,
|
|
6
|
+
createManager,
|
|
7
|
+
} from "../core"
|
|
8
|
+
import { RotateRecognizer } from "../core/rotate/RotateRecognizer"
|
|
9
|
+
import { PinchRecognizer } from "../core/pinch/PinchRecognizer"
|
|
10
|
+
|
|
11
|
+
export const useManager = (container?: HTMLElement) => {
|
|
12
|
+
const [manager] = useState(() => {
|
|
13
|
+
const panRecognizer = new PanRecognizer()
|
|
14
|
+
const swipeRecognizer = new SwipeRecognizer()
|
|
15
|
+
const rotateRecognizer = new RotateRecognizer()
|
|
16
|
+
const pinchRecognizer = new PinchRecognizer()
|
|
17
|
+
const tapRecognizer = new TapRecognizer({
|
|
18
|
+
maxTaps: 3,
|
|
19
|
+
failWith: [panRecognizer],
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
return createManager({
|
|
23
|
+
recognizers: [
|
|
24
|
+
tapRecognizer,
|
|
25
|
+
panRecognizer,
|
|
26
|
+
swipeRecognizer,
|
|
27
|
+
rotateRecognizer,
|
|
28
|
+
pinchRecognizer,
|
|
29
|
+
],
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
if (container) {
|
|
35
|
+
manager.initialize(container)
|
|
36
|
+
}
|
|
37
|
+
}, [container, manager])
|
|
38
|
+
|
|
39
|
+
return { manager }
|
|
40
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { useEffect, useState } from "react"
|
|
2
|
+
import { Manager } from "../core/manager"
|
|
3
|
+
import {
|
|
4
|
+
FormControl,
|
|
5
|
+
FormLabel,
|
|
6
|
+
NumberDecrementStepper,
|
|
7
|
+
NumberIncrementStepper,
|
|
8
|
+
NumberInput,
|
|
9
|
+
NumberInputField,
|
|
10
|
+
NumberInputStepper,
|
|
11
|
+
Stack,
|
|
12
|
+
Text,
|
|
13
|
+
useToast,
|
|
14
|
+
} from "@chakra-ui/react"
|
|
15
|
+
import { DebugBox } from "./DebugBox"
|
|
16
|
+
import { TapRecognizer } from "../core"
|
|
17
|
+
|
|
18
|
+
const TapDebug = ({
|
|
19
|
+
value,
|
|
20
|
+
onChange,
|
|
21
|
+
}: {
|
|
22
|
+
value: number
|
|
23
|
+
onChange: (value: number) => void
|
|
24
|
+
}) => {
|
|
25
|
+
return (
|
|
26
|
+
<DebugBox>
|
|
27
|
+
<Stack>
|
|
28
|
+
<Text>Tap:</Text>
|
|
29
|
+
<FormControl>
|
|
30
|
+
<FormLabel>max taps</FormLabel>
|
|
31
|
+
<NumberInput
|
|
32
|
+
defaultValue={value}
|
|
33
|
+
min={1}
|
|
34
|
+
max={20}
|
|
35
|
+
onChange={(valueString) => onChange(parseInt(valueString))}
|
|
36
|
+
maxWidth={100}
|
|
37
|
+
>
|
|
38
|
+
<NumberInputField />
|
|
39
|
+
<NumberInputStepper>
|
|
40
|
+
<NumberIncrementStepper />
|
|
41
|
+
<NumberDecrementStepper />
|
|
42
|
+
</NumberInputStepper>
|
|
43
|
+
</NumberInput>
|
|
44
|
+
</FormControl>
|
|
45
|
+
</Stack>
|
|
46
|
+
</DebugBox>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const useTap = (manager: Manager) => {
|
|
51
|
+
const toast = useToast()
|
|
52
|
+
const [maxTaps, setMaxTaps] = useState(3)
|
|
53
|
+
const tapRecognizer = manager.recognizers.find(
|
|
54
|
+
(recognizer) => recognizer instanceof TapRecognizer,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
const clickSub = manager.events$.subscribe((e) => {
|
|
59
|
+
if (e.type === "tap") {
|
|
60
|
+
toast({
|
|
61
|
+
title: "Click",
|
|
62
|
+
description: (
|
|
63
|
+
<Stack>
|
|
64
|
+
<Text display="flex" gap={4}>
|
|
65
|
+
Taps: <b>{e.taps}</b>
|
|
66
|
+
</Text>
|
|
67
|
+
</Stack>
|
|
68
|
+
),
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
return () => {
|
|
74
|
+
clickSub.unsubscribe()
|
|
75
|
+
}
|
|
76
|
+
}, [manager, toast])
|
|
77
|
+
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
tapRecognizer?.update({ maxTaps })
|
|
80
|
+
}, [maxTaps, tapRecognizer])
|
|
81
|
+
|
|
82
|
+
const tapDebug = (
|
|
83
|
+
<TapDebug value={maxTaps} onChange={(value) => setMaxTaps(value)} />
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
return { tapDebug }
|
|
87
|
+
}
|
package/src/index.css
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
:root {
|
|
2
|
+
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
|
|
3
|
+
line-height: 1.5;
|
|
4
|
+
font-weight: 400;
|
|
5
|
+
|
|
6
|
+
color-scheme: light dark;
|
|
7
|
+
color: rgba(255, 255, 255, 0.87);
|
|
8
|
+
|
|
9
|
+
font-synthesis: none;
|
|
10
|
+
text-rendering: optimizeLegibility;
|
|
11
|
+
-webkit-font-smoothing: antialiased;
|
|
12
|
+
-moz-osx-font-smoothing: grayscale;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
html {
|
|
16
|
+
height: 100%;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
body {
|
|
20
|
+
margin: 0;
|
|
21
|
+
display: flex;
|
|
22
|
+
min-width: 100vw;
|
|
23
|
+
min-height: 100%;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
#root {
|
|
27
|
+
flex: 1;
|
|
28
|
+
display: flex;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
#container {
|
|
32
|
+
display: flex;
|
|
33
|
+
justify-content: center;
|
|
34
|
+
align-items: center;
|
|
35
|
+
flex: 1;
|
|
36
|
+
position: relative;
|
|
37
|
+
overflow: hidden;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.container {
|
|
41
|
+
position: relative;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
#boxContainer {
|
|
45
|
+
height: 150px;
|
|
46
|
+
width: 150px;
|
|
47
|
+
position: absolute;
|
|
48
|
+
left: 0;
|
|
49
|
+
top: 0;
|
|
50
|
+
border: 1px solid white;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
#box {
|
|
54
|
+
height: 148px;
|
|
55
|
+
width: 148px;
|
|
56
|
+
background-color: red;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
#boxCenter {
|
|
60
|
+
height: 10px;
|
|
61
|
+
width: 10px;
|
|
62
|
+
background-color: white;
|
|
63
|
+
position: absolute;
|
|
64
|
+
left: 0;
|
|
65
|
+
top: 0;
|
|
66
|
+
}
|
package/src/main.tsx
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from "react"
|
|
2
|
+
import ReactDOM from "react-dom/client"
|
|
3
|
+
import App from "./demo/App.tsx"
|
|
4
|
+
import "./index.css"
|
|
5
|
+
import { ChakraProvider } from "@chakra-ui/react"
|
|
6
|
+
import { theme } from "./theme.ts"
|
|
7
|
+
|
|
8
|
+
ReactDOM.createRoot(document.getElementById("root")!).render(
|
|
9
|
+
<React.StrictMode>
|
|
10
|
+
<ChakraProvider theme={theme}>
|
|
11
|
+
<App />
|
|
12
|
+
</ChakraProvider>
|
|
13
|
+
</React.StrictMode>,
|
|
14
|
+
)
|
package/src/theme.ts
ADDED
package/tsconfig.json
CHANGED
|
@@ -2,16 +2,18 @@
|
|
|
2
2
|
"compilerOptions": {
|
|
3
3
|
"target": "ES2020",
|
|
4
4
|
"useDefineForClassFields": true,
|
|
5
|
-
"module": "ESNext",
|
|
6
5
|
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
|
6
|
+
"module": "ESNext",
|
|
7
7
|
"skipLibCheck": true,
|
|
8
8
|
|
|
9
9
|
/* Bundler mode */
|
|
10
10
|
"moduleResolution": "bundler",
|
|
11
11
|
"allowImportingTsExtensions": true,
|
|
12
|
+
"noUncheckedIndexedAccess": true,
|
|
12
13
|
"resolveJsonModule": true,
|
|
13
14
|
"isolatedModules": true,
|
|
14
15
|
"noEmit": true,
|
|
16
|
+
"jsx": "react-jsx",
|
|
15
17
|
|
|
16
18
|
/* Linting */
|
|
17
19
|
"strict": true,
|
|
@@ -19,5 +21,6 @@
|
|
|
19
21
|
"noUnusedParameters": true,
|
|
20
22
|
"noFallthroughCasesInSwitch": true
|
|
21
23
|
},
|
|
22
|
-
"include": ["src"]
|
|
24
|
+
"include": ["src"],
|
|
25
|
+
"references": [{ "path": "./tsconfig.node.json" }]
|
|
23
26
|
}
|
package/vite.config.ts
CHANGED
|
@@ -1,33 +1,38 @@
|
|
|
1
|
-
import { defineConfig } from "vite"
|
|
2
|
-
import dts from "vite-plugin-dts"
|
|
3
|
-
import { resolve } from "path"
|
|
4
|
-
import
|
|
5
|
-
import
|
|
1
|
+
import { defineConfig } from "vite"
|
|
2
|
+
import dts from "vite-plugin-dts"
|
|
3
|
+
import { resolve } from "path"
|
|
4
|
+
import externals from "rollup-plugin-node-externals"
|
|
5
|
+
import react from "@vitejs/plugin-react"
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
export default defineConfig(({ mode }) => {
|
|
8
|
+
const libMode = process.env.LIB_MODE === "true"
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
sourcemap: true,
|
|
19
|
-
},
|
|
20
|
-
plugins: [
|
|
21
|
-
{
|
|
22
|
-
enforce: `pre`,
|
|
23
|
-
...externals({
|
|
24
|
-
peerDeps: true,
|
|
25
|
-
deps: true,
|
|
26
|
-
devDeps: true,
|
|
10
|
+
return {
|
|
11
|
+
build: {
|
|
12
|
+
minify: false,
|
|
13
|
+
...(libMode && {
|
|
14
|
+
lib: {
|
|
15
|
+
entry: resolve(__dirname, `src/core/index.ts`),
|
|
16
|
+
name: "gesturx",
|
|
17
|
+
fileName: "index",
|
|
18
|
+
},
|
|
27
19
|
}),
|
|
20
|
+
emptyOutDir: mode !== `development`,
|
|
21
|
+
sourcemap: true,
|
|
28
22
|
},
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
23
|
+
plugins: [
|
|
24
|
+
react(),
|
|
25
|
+
{
|
|
26
|
+
enforce: `pre`,
|
|
27
|
+
...externals({
|
|
28
|
+
peerDeps: libMode,
|
|
29
|
+
deps: libMode,
|
|
30
|
+
devDeps: libMode,
|
|
31
|
+
}),
|
|
32
|
+
},
|
|
33
|
+
dts({
|
|
34
|
+
entryRoot: "src",
|
|
35
|
+
}),
|
|
36
|
+
],
|
|
37
|
+
}
|
|
38
|
+
})
|
package/vitest.config.ts
CHANGED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Observable,
|
|
3
|
-
buffer,
|
|
4
|
-
debounceTime,
|
|
5
|
-
exhaustMap,
|
|
6
|
-
filter,
|
|
7
|
-
first,
|
|
8
|
-
ignoreElements,
|
|
9
|
-
map,
|
|
10
|
-
merge,
|
|
11
|
-
of,
|
|
12
|
-
share,
|
|
13
|
-
shareReplay,
|
|
14
|
-
switchMap,
|
|
15
|
-
takeUntil,
|
|
16
|
-
timer,
|
|
17
|
-
withLatestFrom,
|
|
18
|
-
} from "rxjs";
|
|
19
|
-
import {
|
|
20
|
-
fromFailWith,
|
|
21
|
-
fromPointerDown,
|
|
22
|
-
getPointerEvents,
|
|
23
|
-
hasAtLeastOneItem,
|
|
24
|
-
isOUtsidePosThreshold,
|
|
25
|
-
trackActivePointers,
|
|
26
|
-
} from "./utils";
|
|
27
|
-
import { GestureEvent } from "./types";
|
|
28
|
-
|
|
29
|
-
export type TapEvent = GestureEvent & { type: "tap"; taps: number };
|
|
30
|
-
|
|
31
|
-
type Options = {
|
|
32
|
-
// Maximum time in ms between multiple taps.
|
|
33
|
-
multiTapThreshold?: number;
|
|
34
|
-
// Maximum press time in ms.
|
|
35
|
-
maximumPressTime?: number;
|
|
36
|
-
maxTaps?: number;
|
|
37
|
-
posThreshold?: number;
|
|
38
|
-
failWith?: { start$: Observable<unknown> }[];
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const mapToTapEvent = (
|
|
42
|
-
stream: Observable<[PointerEvent, ...PointerEvent[]]>,
|
|
43
|
-
): Observable<TapEvent> =>
|
|
44
|
-
stream.pipe(
|
|
45
|
-
map((events) => ({
|
|
46
|
-
type: "tap",
|
|
47
|
-
srcEvent: events[0],
|
|
48
|
-
taps: events.length,
|
|
49
|
-
})),
|
|
50
|
-
);
|
|
51
|
-
|
|
52
|
-
export const createTapRecognizer = ({
|
|
53
|
-
multiTapThreshold = 200,
|
|
54
|
-
maximumPressTime = 250,
|
|
55
|
-
maxTaps = 1,
|
|
56
|
-
posThreshold = 5,
|
|
57
|
-
failWith,
|
|
58
|
-
}: Options = {}) => {
|
|
59
|
-
return ({
|
|
60
|
-
container,
|
|
61
|
-
afterEventReceived = (event) => event,
|
|
62
|
-
}: {
|
|
63
|
-
container: HTMLElement;
|
|
64
|
-
afterEventReceived?: (event: PointerEvent) => PointerEvent;
|
|
65
|
-
}) => {
|
|
66
|
-
const { pointerUp$, pointerLeave$, pointerCancel$, pointerMove$ } =
|
|
67
|
-
getPointerEvents({
|
|
68
|
-
container,
|
|
69
|
-
afterEventReceived,
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
const pointerDown$ = fromPointerDown({ container, afterEventReceived });
|
|
73
|
-
const activePointers$ = trackActivePointers({ container }).pipe(
|
|
74
|
-
shareReplay(1),
|
|
75
|
-
);
|
|
76
|
-
const hasMoreThanOneActivePointer$ = activePointers$.pipe(
|
|
77
|
-
filter((pointers) => pointers.length > 1),
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
const onlyOnePointer = <T>(stream: Observable<T>) =>
|
|
81
|
-
stream.pipe(
|
|
82
|
-
withLatestFrom(activePointers$),
|
|
83
|
-
filter(([, pointers]) => pointers.length === 1),
|
|
84
|
-
map(([pointer]) => pointer),
|
|
85
|
-
);
|
|
86
|
-
|
|
87
|
-
const bufferPointerDowns = (stream: Observable<PointerEvent>) =>
|
|
88
|
-
stream.pipe(
|
|
89
|
-
buffer(pointerUp$.pipe(debounceTime(multiTapThreshold))),
|
|
90
|
-
first(),
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
const failWith$ = fromFailWith(failWith);
|
|
94
|
-
|
|
95
|
-
const tap$ = merge(
|
|
96
|
-
pointerDown$,
|
|
97
|
-
activePointers$.pipe(ignoreElements()),
|
|
98
|
-
).pipe(
|
|
99
|
-
onlyOnePointer,
|
|
100
|
-
exhaustMap((initialPointerEvent) => {
|
|
101
|
-
const pointerDowns$ = merge(of(initialPointerEvent), pointerDown$);
|
|
102
|
-
const subsequentPointerEvents$ = merge(
|
|
103
|
-
pointerUp$,
|
|
104
|
-
pointerLeave$,
|
|
105
|
-
pointerCancel$,
|
|
106
|
-
pointerMove$,
|
|
107
|
-
);
|
|
108
|
-
const pointerDownsBuffered$ = pointerDowns$.pipe(bufferPointerDowns);
|
|
109
|
-
const subsequentPointersOutOfPositionThreshold$ =
|
|
110
|
-
subsequentPointerEvents$.pipe(
|
|
111
|
-
filter((event) =>
|
|
112
|
-
isOUtsidePosThreshold(initialPointerEvent, event, posThreshold),
|
|
113
|
-
),
|
|
114
|
-
first(),
|
|
115
|
-
);
|
|
116
|
-
const waitedTooLong$ = activePointers$.pipe(
|
|
117
|
-
switchMap(() => timer(maximumPressTime)),
|
|
118
|
-
);
|
|
119
|
-
|
|
120
|
-
const takeUntil$ = merge(
|
|
121
|
-
failWith$,
|
|
122
|
-
pointerCancel$,
|
|
123
|
-
pointerLeave$,
|
|
124
|
-
hasMoreThanOneActivePointer$,
|
|
125
|
-
subsequentPointersOutOfPositionThreshold$,
|
|
126
|
-
waitedTooLong$,
|
|
127
|
-
);
|
|
128
|
-
|
|
129
|
-
const tap$ = pointerDownsBuffered$.pipe(
|
|
130
|
-
filter(hasAtLeastOneItem),
|
|
131
|
-
filter((events) => events.length <= maxTaps),
|
|
132
|
-
mapToTapEvent,
|
|
133
|
-
takeUntil(takeUntil$),
|
|
134
|
-
);
|
|
135
|
-
|
|
136
|
-
return tap$;
|
|
137
|
-
}),
|
|
138
|
-
share(),
|
|
139
|
-
);
|
|
140
|
-
|
|
141
|
-
return { events$: tap$ };
|
|
142
|
-
};
|
|
143
|
-
};
|
package/src/core/types.ts
DELETED