source: imaps-frontend/node_modules/@use-gesture/core/dist/use-gesture-core.esm.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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