1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
---|
4 |
|
---|
5 | var actions_dist_useGestureCoreActions = require('./actions-6579bdef.cjs.dev.js');
|
---|
6 | require('./maths-267f0992.cjs.dev.js');
|
---|
7 |
|
---|
8 | function _objectWithoutPropertiesLoose(source, excluded) {
|
---|
9 | if (source == null) return {};
|
---|
10 | var target = {};
|
---|
11 | var sourceKeys = Object.keys(source);
|
---|
12 | var key, i;
|
---|
13 | for (i = 0; i < sourceKeys.length; i++) {
|
---|
14 | key = sourceKeys[i];
|
---|
15 | if (excluded.indexOf(key) >= 0) continue;
|
---|
16 | target[key] = source[key];
|
---|
17 | }
|
---|
18 | return target;
|
---|
19 | }
|
---|
20 |
|
---|
21 | function _objectWithoutProperties(source, excluded) {
|
---|
22 | if (source == null) return {};
|
---|
23 | var target = _objectWithoutPropertiesLoose(source, excluded);
|
---|
24 | var key, i;
|
---|
25 | if (Object.getOwnPropertySymbols) {
|
---|
26 | var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
---|
27 | for (i = 0; i < sourceSymbolKeys.length; i++) {
|
---|
28 | key = sourceSymbolKeys[i];
|
---|
29 | if (excluded.indexOf(key) >= 0) continue;
|
---|
30 | if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
---|
31 | target[key] = source[key];
|
---|
32 | }
|
---|
33 | }
|
---|
34 | return target;
|
---|
35 | }
|
---|
36 |
|
---|
37 | const sharedConfigResolver = {
|
---|
38 | target(value) {
|
---|
39 | if (value) {
|
---|
40 | return () => 'current' in value ? value.current : value;
|
---|
41 | }
|
---|
42 | return undefined;
|
---|
43 | },
|
---|
44 | enabled(value = true) {
|
---|
45 | return value;
|
---|
46 | },
|
---|
47 | window(value = actions_dist_useGestureCoreActions.SUPPORT.isBrowser ? window : undefined) {
|
---|
48 | return value;
|
---|
49 | },
|
---|
50 | eventOptions({
|
---|
51 | passive = true,
|
---|
52 | capture = false
|
---|
53 | } = {}) {
|
---|
54 | return {
|
---|
55 | passive,
|
---|
56 | capture
|
---|
57 | };
|
---|
58 | },
|
---|
59 | transform(value) {
|
---|
60 | return value;
|
---|
61 | }
|
---|
62 | };
|
---|
63 |
|
---|
64 | const _excluded = ["target", "eventOptions", "window", "enabled", "transform"];
|
---|
65 | function resolveWith(config = {}, resolvers) {
|
---|
66 | const result = {};
|
---|
67 | for (const [key, resolver] of Object.entries(resolvers)) {
|
---|
68 | switch (typeof resolver) {
|
---|
69 | case 'function':
|
---|
70 | if (process.env.NODE_ENV === 'development') {
|
---|
71 | const r = resolver.call(result, config[key], key, config);
|
---|
72 | if (!Number.isNaN(r)) result[key] = r;
|
---|
73 | } else {
|
---|
74 | result[key] = resolver.call(result, config[key], key, config);
|
---|
75 | }
|
---|
76 | break;
|
---|
77 | case 'object':
|
---|
78 | result[key] = resolveWith(config[key], resolver);
|
---|
79 | break;
|
---|
80 | case 'boolean':
|
---|
81 | if (resolver) result[key] = config[key];
|
---|
82 | break;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | return result;
|
---|
86 | }
|
---|
87 | function parse(newConfig, gestureKey, _config = {}) {
|
---|
88 | const _ref = newConfig,
|
---|
89 | {
|
---|
90 | target,
|
---|
91 | eventOptions,
|
---|
92 | window,
|
---|
93 | enabled,
|
---|
94 | transform
|
---|
95 | } = _ref,
|
---|
96 | rest = _objectWithoutProperties(_ref, _excluded);
|
---|
97 | _config.shared = resolveWith({
|
---|
98 | target,
|
---|
99 | eventOptions,
|
---|
100 | window,
|
---|
101 | enabled,
|
---|
102 | transform
|
---|
103 | }, sharedConfigResolver);
|
---|
104 | if (gestureKey) {
|
---|
105 | const resolver = actions_dist_useGestureCoreActions.ConfigResolverMap.get(gestureKey);
|
---|
106 | _config[gestureKey] = resolveWith(actions_dist_useGestureCoreActions._objectSpread2({
|
---|
107 | shared: _config.shared
|
---|
108 | }, rest), resolver);
|
---|
109 | } else {
|
---|
110 | for (const key in rest) {
|
---|
111 | const resolver = actions_dist_useGestureCoreActions.ConfigResolverMap.get(key);
|
---|
112 | if (resolver) {
|
---|
113 | _config[key] = resolveWith(actions_dist_useGestureCoreActions._objectSpread2({
|
---|
114 | shared: _config.shared
|
---|
115 | }, rest[key]), resolver);
|
---|
116 | } else if (process.env.NODE_ENV === 'development') {
|
---|
117 | if (!['drag', 'pinch', 'scroll', 'wheel', 'move', 'hover'].includes(key)) {
|
---|
118 | if (key === 'domTarget') {
|
---|
119 | throw Error(`[@use-gesture]: \`domTarget\` option has been renamed to \`target\`.`);
|
---|
120 | }
|
---|
121 | console.warn(`[@use-gesture]: Unknown config key \`${key}\` was used. Please read the documentation for further information.`);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | return _config;
|
---|
127 | }
|
---|
128 |
|
---|
129 | class EventStore {
|
---|
130 | constructor(ctrl, gestureKey) {
|
---|
131 | actions_dist_useGestureCoreActions._defineProperty(this, "_listeners", new Set());
|
---|
132 | this._ctrl = ctrl;
|
---|
133 | this._gestureKey = gestureKey;
|
---|
134 | }
|
---|
135 | add(element, device, action, handler, options) {
|
---|
136 | const listeners = this._listeners;
|
---|
137 | const type = actions_dist_useGestureCoreActions.toDomEventType(device, action);
|
---|
138 | const _options = this._gestureKey ? this._ctrl.config[this._gestureKey].eventOptions : {};
|
---|
139 | const eventOptions = actions_dist_useGestureCoreActions._objectSpread2(actions_dist_useGestureCoreActions._objectSpread2({}, _options), options);
|
---|
140 | element.addEventListener(type, handler, eventOptions);
|
---|
141 | const remove = () => {
|
---|
142 | element.removeEventListener(type, handler, eventOptions);
|
---|
143 | listeners.delete(remove);
|
---|
144 | };
|
---|
145 | listeners.add(remove);
|
---|
146 | return remove;
|
---|
147 | }
|
---|
148 | clean() {
|
---|
149 | this._listeners.forEach(remove => remove());
|
---|
150 | this._listeners.clear();
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | class TimeoutStore {
|
---|
155 | constructor() {
|
---|
156 | actions_dist_useGestureCoreActions._defineProperty(this, "_timeouts", new Map());
|
---|
157 | }
|
---|
158 | add(key, callback, ms = 140, ...args) {
|
---|
159 | this.remove(key);
|
---|
160 | this._timeouts.set(key, window.setTimeout(callback, ms, ...args));
|
---|
161 | }
|
---|
162 | remove(key) {
|
---|
163 | const timeout = this._timeouts.get(key);
|
---|
164 | if (timeout) window.clearTimeout(timeout);
|
---|
165 | }
|
---|
166 | clean() {
|
---|
167 | this._timeouts.forEach(timeout => void window.clearTimeout(timeout));
|
---|
168 | this._timeouts.clear();
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | class Controller {
|
---|
173 | constructor(handlers) {
|
---|
174 | actions_dist_useGestureCoreActions._defineProperty(this, "gestures", new Set());
|
---|
175 | actions_dist_useGestureCoreActions._defineProperty(this, "_targetEventStore", new EventStore(this));
|
---|
176 | actions_dist_useGestureCoreActions._defineProperty(this, "gestureEventStores", {});
|
---|
177 | actions_dist_useGestureCoreActions._defineProperty(this, "gestureTimeoutStores", {});
|
---|
178 | actions_dist_useGestureCoreActions._defineProperty(this, "handlers", {});
|
---|
179 | actions_dist_useGestureCoreActions._defineProperty(this, "config", {});
|
---|
180 | actions_dist_useGestureCoreActions._defineProperty(this, "pointerIds", new Set());
|
---|
181 | actions_dist_useGestureCoreActions._defineProperty(this, "touchIds", new Set());
|
---|
182 | actions_dist_useGestureCoreActions._defineProperty(this, "state", {
|
---|
183 | shared: {
|
---|
184 | shiftKey: false,
|
---|
185 | metaKey: false,
|
---|
186 | ctrlKey: false,
|
---|
187 | altKey: false
|
---|
188 | }
|
---|
189 | });
|
---|
190 | resolveGestures(this, handlers);
|
---|
191 | }
|
---|
192 | setEventIds(event) {
|
---|
193 | if (actions_dist_useGestureCoreActions.isTouch(event)) {
|
---|
194 | this.touchIds = new Set(actions_dist_useGestureCoreActions.touchIds(event));
|
---|
195 | return this.touchIds;
|
---|
196 | } else if ('pointerId' in event) {
|
---|
197 | if (event.type === 'pointerup' || event.type === 'pointercancel') this.pointerIds.delete(event.pointerId);else if (event.type === 'pointerdown') this.pointerIds.add(event.pointerId);
|
---|
198 | return this.pointerIds;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | applyHandlers(handlers, nativeHandlers) {
|
---|
202 | this.handlers = handlers;
|
---|
203 | this.nativeHandlers = nativeHandlers;
|
---|
204 | }
|
---|
205 | applyConfig(config, gestureKey) {
|
---|
206 | this.config = parse(config, gestureKey, this.config);
|
---|
207 | }
|
---|
208 | clean() {
|
---|
209 | this._targetEventStore.clean();
|
---|
210 | for (const key of this.gestures) {
|
---|
211 | this.gestureEventStores[key].clean();
|
---|
212 | this.gestureTimeoutStores[key].clean();
|
---|
213 | }
|
---|
214 | }
|
---|
215 | effect() {
|
---|
216 | if (this.config.shared.target) this.bind();
|
---|
217 | return () => this._targetEventStore.clean();
|
---|
218 | }
|
---|
219 | bind(...args) {
|
---|
220 | const sharedConfig = this.config.shared;
|
---|
221 | const props = {};
|
---|
222 | let target;
|
---|
223 | if (sharedConfig.target) {
|
---|
224 | target = sharedConfig.target();
|
---|
225 | if (!target) return;
|
---|
226 | }
|
---|
227 | if (sharedConfig.enabled) {
|
---|
228 | for (const gestureKey of this.gestures) {
|
---|
229 | const gestureConfig = this.config[gestureKey];
|
---|
230 | const bindFunction = bindToProps(props, gestureConfig.eventOptions, !!target);
|
---|
231 | if (gestureConfig.enabled) {
|
---|
232 | const Engine = actions_dist_useGestureCoreActions.EngineMap.get(gestureKey);
|
---|
233 | new Engine(this, args, gestureKey).bind(bindFunction);
|
---|
234 | }
|
---|
235 | }
|
---|
236 | const nativeBindFunction = bindToProps(props, sharedConfig.eventOptions, !!target);
|
---|
237 | for (const eventKey in this.nativeHandlers) {
|
---|
238 | nativeBindFunction(eventKey, '', event => this.nativeHandlers[eventKey](actions_dist_useGestureCoreActions._objectSpread2(actions_dist_useGestureCoreActions._objectSpread2({}, this.state.shared), {}, {
|
---|
239 | event,
|
---|
240 | args
|
---|
241 | })), undefined, true);
|
---|
242 | }
|
---|
243 | }
|
---|
244 | for (const handlerProp in props) {
|
---|
245 | props[handlerProp] = actions_dist_useGestureCoreActions.chain(...props[handlerProp]);
|
---|
246 | }
|
---|
247 | if (!target) return props;
|
---|
248 | for (const handlerProp in props) {
|
---|
249 | const {
|
---|
250 | device,
|
---|
251 | capture,
|
---|
252 | passive
|
---|
253 | } = actions_dist_useGestureCoreActions.parseProp(handlerProp);
|
---|
254 | this._targetEventStore.add(target, device, '', props[handlerProp], {
|
---|
255 | capture,
|
---|
256 | passive
|
---|
257 | });
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 | function setupGesture(ctrl, gestureKey) {
|
---|
262 | ctrl.gestures.add(gestureKey);
|
---|
263 | ctrl.gestureEventStores[gestureKey] = new EventStore(ctrl, gestureKey);
|
---|
264 | ctrl.gestureTimeoutStores[gestureKey] = new TimeoutStore();
|
---|
265 | }
|
---|
266 | function resolveGestures(ctrl, internalHandlers) {
|
---|
267 | if (internalHandlers.drag) setupGesture(ctrl, 'drag');
|
---|
268 | if (internalHandlers.wheel) setupGesture(ctrl, 'wheel');
|
---|
269 | if (internalHandlers.scroll) setupGesture(ctrl, 'scroll');
|
---|
270 | if (internalHandlers.move) setupGesture(ctrl, 'move');
|
---|
271 | if (internalHandlers.pinch) setupGesture(ctrl, 'pinch');
|
---|
272 | if (internalHandlers.hover) setupGesture(ctrl, 'hover');
|
---|
273 | }
|
---|
274 | const bindToProps = (props, eventOptions, withPassiveOption) => (device, action, handler, options = {}, isNative = false) => {
|
---|
275 | var _options$capture, _options$passive;
|
---|
276 | const capture = (_options$capture = options.capture) !== null && _options$capture !== void 0 ? _options$capture : eventOptions.capture;
|
---|
277 | const passive = (_options$passive = options.passive) !== null && _options$passive !== void 0 ? _options$passive : eventOptions.passive;
|
---|
278 | let handlerProp = isNative ? device : actions_dist_useGestureCoreActions.toHandlerProp(device, action, capture);
|
---|
279 | if (withPassiveOption && passive) handlerProp += 'Passive';
|
---|
280 | props[handlerProp] = props[handlerProp] || [];
|
---|
281 | props[handlerProp].push(handler);
|
---|
282 | };
|
---|
283 |
|
---|
284 | const RE_NOT_NATIVE = /^on(Drag|Wheel|Scroll|Move|Pinch|Hover)/;
|
---|
285 | function sortHandlers(_handlers) {
|
---|
286 | const native = {};
|
---|
287 | const handlers = {};
|
---|
288 | const actions = new Set();
|
---|
289 | for (let key in _handlers) {
|
---|
290 | if (RE_NOT_NATIVE.test(key)) {
|
---|
291 | actions.add(RegExp.lastMatch);
|
---|
292 | handlers[key] = _handlers[key];
|
---|
293 | } else {
|
---|
294 | native[key] = _handlers[key];
|
---|
295 | }
|
---|
296 | }
|
---|
297 | return [handlers, native, actions];
|
---|
298 | }
|
---|
299 | function registerGesture(actions, handlers, handlerKey, key, internalHandlers, config) {
|
---|
300 | if (!actions.has(handlerKey)) return;
|
---|
301 | if (!actions_dist_useGestureCoreActions.EngineMap.has(key)) {
|
---|
302 | if (process.env.NODE_ENV === 'development') {
|
---|
303 | console.warn(`[@use-gesture]: You've created a custom handler that that uses the \`${key}\` gesture but isn't properly configured.\n\nPlease add \`${key}Action\` when creating your handler.`);
|
---|
304 | }
|
---|
305 | return;
|
---|
306 | }
|
---|
307 | const startKey = handlerKey + 'Start';
|
---|
308 | const endKey = handlerKey + 'End';
|
---|
309 | const fn = state => {
|
---|
310 | let memo = undefined;
|
---|
311 | if (state.first && startKey in handlers) handlers[startKey](state);
|
---|
312 | if (handlerKey in handlers) memo = handlers[handlerKey](state);
|
---|
313 | if (state.last && endKey in handlers) handlers[endKey](state);
|
---|
314 | return memo;
|
---|
315 | };
|
---|
316 | internalHandlers[key] = fn;
|
---|
317 | config[key] = config[key] || {};
|
---|
318 | }
|
---|
319 | function parseMergedHandlers(mergedHandlers, mergedConfig) {
|
---|
320 | const [handlers, nativeHandlers, actions] = sortHandlers(mergedHandlers);
|
---|
321 | const internalHandlers = {};
|
---|
322 | registerGesture(actions, handlers, 'onDrag', 'drag', internalHandlers, mergedConfig);
|
---|
323 | registerGesture(actions, handlers, 'onWheel', 'wheel', internalHandlers, mergedConfig);
|
---|
324 | registerGesture(actions, handlers, 'onScroll', 'scroll', internalHandlers, mergedConfig);
|
---|
325 | registerGesture(actions, handlers, 'onPinch', 'pinch', internalHandlers, mergedConfig);
|
---|
326 | registerGesture(actions, handlers, 'onMove', 'move', internalHandlers, mergedConfig);
|
---|
327 | registerGesture(actions, handlers, 'onHover', 'hover', internalHandlers, mergedConfig);
|
---|
328 | return {
|
---|
329 | handlers: internalHandlers,
|
---|
330 | config: mergedConfig,
|
---|
331 | nativeHandlers
|
---|
332 | };
|
---|
333 | }
|
---|
334 |
|
---|
335 | exports.Controller = Controller;
|
---|
336 | exports.parseMergedHandlers = parseMergedHandlers;
|
---|