|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import { ActionType } from './ActionType'; |
| 4 | +import { isExperimentalWebImplementationEnabled } from './EnableExperimentalWebImplementation'; |
| 5 | + |
| 6 | +//GestureHandlers |
| 7 | +import InteractionManager from './web/tools/InteractionManager'; |
| 8 | +import NodeManager from './web/tools/NodeManager'; |
| 9 | +import PanGestureHandler from './web/handlers/PanGestureHandler'; |
| 10 | +import TapGestureHandler from './web/handlers/TapGestureHandler'; |
| 11 | +import LongPressGestureHandler from './web/handlers/LongPressGestureHandler'; |
| 12 | +import PinchGestureHandler from './web/handlers/PinchGestureHandler'; |
| 13 | +import RotationGestureHandler from './web/handlers/RotationGestureHandler'; |
| 14 | +import FlingGestureHandler from './web/handlers/FlingGestureHandler'; |
| 15 | +import NativeViewGestureHandler from './web/handlers/NativeViewGestureHandler'; |
| 16 | +import ManualGestureHandler from './web/handlers/ManualGestureHandler'; |
| 17 | + |
| 18 | +//Hammer Handlers |
| 19 | +import * as HammerNodeManager from './web_hammer/NodeManager'; |
| 20 | +import HammerNativeViewGestureHandler from './web_hammer/NativeViewGestureHandler'; |
| 21 | +import HammerPanGestureHandler from './web_hammer/PanGestureHandler'; |
| 22 | +import HammerTapGestureHandler from './web_hammer/TapGestureHandler'; |
| 23 | +import HammerLongPressGestureHandler from './web_hammer/LongPressGestureHandler'; |
| 24 | +import HammerPinchGestureHandler from './web_hammer/PinchGestureHandler'; |
| 25 | +import HammerRotationGestureHandler from './web_hammer/RotationGestureHandler'; |
| 26 | +import HammerFlingGestureHandler from './web_hammer/FlingGestureHandler'; |
| 27 | +import { Config } from './web/interfaces'; |
| 28 | + |
| 29 | +export const Gestures = { |
| 30 | + NativeViewGestureHandler, |
| 31 | + PanGestureHandler, |
| 32 | + TapGestureHandler, |
| 33 | + LongPressGestureHandler, |
| 34 | + PinchGestureHandler, |
| 35 | + RotationGestureHandler, |
| 36 | + FlingGestureHandler, |
| 37 | + ManualGestureHandler, |
| 38 | +}; |
| 39 | + |
| 40 | +export const HammerGestures = { |
| 41 | + NativeViewGestureHandler: HammerNativeViewGestureHandler, |
| 42 | + PanGestureHandler: HammerPanGestureHandler, |
| 43 | + TapGestureHandler: HammerTapGestureHandler, |
| 44 | + LongPressGestureHandler: HammerLongPressGestureHandler, |
| 45 | + PinchGestureHandler: HammerPinchGestureHandler, |
| 46 | + RotationGestureHandler: HammerRotationGestureHandler, |
| 47 | + FlingGestureHandler: HammerFlingGestureHandler, |
| 48 | +}; |
| 49 | + |
| 50 | +export default { |
| 51 | + handleSetJSResponder(_tag: number, _blockNativeResponder: boolean) { |
| 52 | + // NO-OP |
| 53 | + }, |
| 54 | + handleClearJSResponder() { |
| 55 | + // NO-OP |
| 56 | + }, |
| 57 | + createGestureHandler<T>( |
| 58 | + handlerName: keyof typeof Gestures, |
| 59 | + handlerTag: number, |
| 60 | + config: T |
| 61 | + ) { |
| 62 | + if (isExperimentalWebImplementationEnabled()) { |
| 63 | + if (!(handlerName in Gestures)) { |
| 64 | + throw new Error( |
| 65 | + `react-native-gesture-handler: ${handlerName} is not supported on web.` |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + const GestureClass = Gestures[handlerName]; |
| 70 | + NodeManager.createGestureHandler(handlerTag, new GestureClass()); |
| 71 | + InteractionManager.getInstance().configureInteractions( |
| 72 | + NodeManager.getHandler(handlerTag), |
| 73 | + config as unknown as Config |
| 74 | + ); |
| 75 | + } else { |
| 76 | + if (!(handlerName in HammerGestures)) { |
| 77 | + throw new Error( |
| 78 | + `react-native-gesture-handler: ${handlerName} is not supported on web.` |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + // @ts-ignore If it doesn't exist, the error is thrown |
| 83 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 84 | + const GestureClass = HammerGestures[handlerName]; |
| 85 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-call |
| 86 | + HammerNodeManager.createGestureHandler(handlerTag, new GestureClass()); |
| 87 | + } |
| 88 | + |
| 89 | + this.updateGestureHandler(handlerTag, config as unknown as Config); |
| 90 | + }, |
| 91 | + attachGestureHandler( |
| 92 | + handlerTag: number, |
| 93 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 94 | + newView: any, |
| 95 | + _actionType: ActionType, |
| 96 | + propsRef: React.RefObject<unknown> |
| 97 | + ) { |
| 98 | + if ( |
| 99 | + !(newView instanceof HTMLElement || newView instanceof React.Component) |
| 100 | + ) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if (isExperimentalWebImplementationEnabled()) { |
| 105 | + //@ts-ignore Types should be HTMLElement or React.Component |
| 106 | + NodeManager.getHandler(handlerTag).init(newView, propsRef); |
| 107 | + } else { |
| 108 | + //@ts-ignore Types should be HTMLElement or React.Component |
| 109 | + HammerNodeManager.getHandler(handlerTag).setView(newView, propsRef); |
| 110 | + } |
| 111 | + }, |
| 112 | + updateGestureHandler(handlerTag: number, newConfig: Config) { |
| 113 | + if (isExperimentalWebImplementationEnabled()) { |
| 114 | + NodeManager.getHandler(handlerTag).updateGestureConfig(newConfig); |
| 115 | + |
| 116 | + InteractionManager.getInstance().configureInteractions( |
| 117 | + NodeManager.getHandler(handlerTag), |
| 118 | + newConfig |
| 119 | + ); |
| 120 | + } else { |
| 121 | + HammerNodeManager.getHandler(handlerTag).updateGestureConfig(newConfig); |
| 122 | + } |
| 123 | + }, |
| 124 | + getGestureHandlerNode(handlerTag: number) { |
| 125 | + if (isExperimentalWebImplementationEnabled()) { |
| 126 | + return NodeManager.getHandler(handlerTag); |
| 127 | + } else { |
| 128 | + return HammerNodeManager.getHandler(handlerTag); |
| 129 | + } |
| 130 | + }, |
| 131 | + dropGestureHandler(handlerTag: number) { |
| 132 | + if (isExperimentalWebImplementationEnabled()) { |
| 133 | + NodeManager.dropGestureHandler(handlerTag); |
| 134 | + } else { |
| 135 | + HammerNodeManager.dropGestureHandler(handlerTag); |
| 136 | + } |
| 137 | + }, |
| 138 | + // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 139 | + flushOperations() {}, |
| 140 | +}; |
0 commit comments