forked from software-mansion/react-native-gesture-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createHandler.web.js
205 lines (169 loc) · 4.77 KB
/
createHandler.web.js
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
import React from 'react';
import { TouchableWithoutFeedback } from 'react-native';
import State from './State';
function handleHandlerStateChange(props, event, oldState, state) {
const { enabled, onHandlerStateChange } = props;
if (enabled !== false && onHandlerStateChange) {
const {
nativeEvent: {
locationX: x,
locationY: y,
pageX: absoluteX,
pageY: absoluteY,
},
} = event;
onHandlerStateChange({
nativeEvent: {
oldState,
state,
x,
y,
absoluteX,
absoluteY,
pointerInside: true,
},
});
}
}
function handleFailed(props, event) {
handleHandlerStateChange(props, event, State.ACTIVE, State.FAILED);
}
function handleEnd(props, event) {
handleHandlerStateChange(props, event, State.ACTIVE, State.END);
}
function handleActivate(props, event) {
handleHandlerStateChange(props, event, State.BEGAN, State.ACTIVE);
}
function handleBegan(props, event) {
handleHandlerStateChange(props, event, State.UNDETERMINED, State.BEGAN);
}
class UnimplementedGestureHandler extends React.Component {
setNativeProps = () => {
// Do nothing
};
render() {
return this.props.children;
}
}
const handlers = {
NativeViewGestureHandler: class NativeViewGestureHandler extends React.Component {
render() {
const { children } = this.props;
return children;
}
},
TapGestureHandler: class TapGestureHandler extends React.Component {
static defaultProps = {
numberOfTaps: 1,
maxDurationMs: 500,
maxDelayMs: 500,
minPointers: 1,
maxDeltaX: Number.MAX_SAFE_INTEGER,
maxDeltaY: Number.MAX_SAFE_INTEGER,
maxDist: Number.MAX_SAFE_INTEGER,
};
hasBegun = false;
touchBank = [];
timeout = null;
setNativeProps() {}
clearState = () => {
this.hasBegun = false;
this.touchBank = [];
window.clearTimeout(this.timeout);
};
handlePressIn = event => {
const { maxDelayMs } = this.props;
if (!this.hasBegun) {
event.persist();
this.hasBegun = true;
handleBegan(this.props, event);
// Cancel if not finished in time
this.timeout = window.setTimeout(() => {
if (this.hasBegun) {
this.clearState();
handleFailed(this.props, event);
}
}, maxDelayMs);
}
};
handlePressOut = event => {
const {
touchHistory: { touchBank = [] },
} = event;
const { maxDeltaX, maxDeltaY, maxDurationMs, numberOfTaps } = this.props;
this.touchBank = this.touchBank.concat(touchBank);
// Check if all touches are valid
const areTouchesValid = this.touchBank.every(touch => {
const {
currentPageX,
currentPageY,
currentTimeStamp,
startPageX,
startPageY,
startTimeStamp,
} = touch;
// Check if touch took longer than it may
if (startTimeStamp + maxDurationMs < currentTimeStamp) {
return false;
}
// Check if touch moved too far away
if (
startPageX + maxDeltaX < currentPageX ||
startPageX - maxDeltaX > currentPageX
) {
return false;
}
// Check if touch moved too far away
if (
startPageY + maxDeltaY < currentPageY ||
startPageY - maxDeltaY > currentPageY
) {
return false;
}
return true;
});
// Check if all touches were valid and the necessary number of touches was achieved
if (!areTouchesValid) {
this.clearState();
handleFailed(this.props, event);
} else if (this.touchBank.length >= numberOfTaps) {
handleActivate(this.props, event);
this.clearState();
handleEnd(this.props, event);
}
};
render() {
const { children, style } = this.props;
return (
<TouchableWithoutFeedback
style={style}
onPressIn={this.handlePressIn}
onPressOut={this.handlePressOut}>
{children}
</TouchableWithoutFeedback>
);
}
},
};
export default function createHandler(handlerName, propTypes = {}) {
class Handler extends React.Component {
static displayName = handlerName;
static propTypes = propTypes;
componentDidMount() {
if (!handlers[handlerName]) {
console.warn(`${handlerName} is not yet supported on web.`);
}
}
_refHandler = node => {
this._viewNode = node;
};
setNativeProps = (...args) => {
this._viewNode.setNativeProps(...args);
};
render() {
const Handler = handlers[handlerName] || UnimplementedGestureHandler;
return <Handler ref={this._refHandler} {...this.props} />;
}
}
return Handler;
}