source: imaps-frontend/node_modules/@use-gesture/core/src/parser.ts

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: 2.6 KB
Line 
1import { FullGestureState, GestureHandlers, GestureKey, InternalHandlers, UserGestureConfig } from './types'
2import { EngineMap } from './actions'
3
4const RE_NOT_NATIVE = /^on(Drag|Wheel|Scroll|Move|Pinch|Hover)/
5
6function sortHandlers(_handlers: GestureHandlers) {
7 const native: any = {}
8 const handlers: InternalHandlers = {}
9 const actions = new Set()
10
11 for (let key in _handlers) {
12 if (RE_NOT_NATIVE.test(key)) {
13 actions.add(RegExp.lastMatch)
14 // @ts-ignore
15 handlers[key] = _handlers[key]
16 } else {
17 // @ts-ignore
18 native[key] = _handlers[key]
19 }
20 }
21
22 return [handlers, native, actions]
23}
24
25type HandlerKey = 'onDrag' | 'onPinch' | 'onWheel' | 'onMove' | 'onScroll' | 'onHover'
26
27function registerGesture(
28 actions: Set<unknown>,
29 handlers: GestureHandlers,
30 handlerKey: HandlerKey,
31 key: GestureKey,
32 internalHandlers: any,
33 config: any
34) {
35 if (!actions.has(handlerKey)) return
36
37 if (!EngineMap.has(key)) {
38 if (process.env.NODE_ENV === 'development') {
39 // eslint-disable-next-line no-console
40 console.warn(
41 `[@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.`
42 )
43 }
44 return
45 }
46
47 const startKey = handlerKey + 'Start'
48 const endKey = handlerKey + 'End'
49
50 const fn = (state: FullGestureState<GestureKey>) => {
51 let memo = undefined
52 // @ts-ignore
53 if (state.first && startKey in handlers) handlers[startKey](state)
54 // @ts-ignore
55 if (handlerKey in handlers) memo = handlers[handlerKey](state)
56 // @ts-ignore
57 if (state.last && endKey in handlers) handlers[endKey](state)
58 return memo
59 }
60
61 internalHandlers[key] = fn
62 config[key] = config[key] || {}
63}
64
65export function parseMergedHandlers(mergedHandlers: GestureHandlers, mergedConfig: UserGestureConfig) {
66 const [handlers, nativeHandlers, actions] = sortHandlers(mergedHandlers)
67
68 const internalHandlers = {}
69
70 registerGesture(actions, handlers, 'onDrag', 'drag', internalHandlers, mergedConfig)
71 registerGesture(actions, handlers, 'onWheel', 'wheel', internalHandlers, mergedConfig)
72 registerGesture(actions, handlers, 'onScroll', 'scroll', internalHandlers, mergedConfig)
73 registerGesture(actions, handlers, 'onPinch', 'pinch', internalHandlers, mergedConfig)
74 registerGesture(actions, handlers, 'onMove', 'move', internalHandlers, mergedConfig)
75 registerGesture(actions, handlers, 'onHover', 'hover', internalHandlers, mergedConfig)
76
77 return { handlers: internalHandlers, config: mergedConfig, nativeHandlers }
78}
Note: See TracBrowser for help on using the repository browser.