Created
April 10, 2023 07:52
-
-
Save zhiqingchen/59aa50e9e1b781ff676f416eb0061943 to your computer and use it in GitHub Desktop.
replace PanResponder with react-native-gesture-handler by chatGPT-4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/wuba/react-native-echarts/blob/v1.0.0/src/utils/events.ts | |
// replace PanResponder with react-native-gesture-handler by chatGPT-4 | |
import React, { useRef, useMemo } from 'react'; | |
import { PanGestureHandler, PinchGestureHandler, State } from 'react-native-gesture-handler'; | |
import { getInstance } from 'zrender/lib/zrender'; | |
declare type HandlerName = | |
| 'click' | |
| 'dblclick' | |
| 'mousewheel' | |
| 'mouseout' | |
| 'mouseup' | |
| 'mousedown' | |
| 'mousemove' | |
| 'contextmenu'; | |
const noop = () => {}; | |
function wrapTouch(event: any) { | |
for (let i = 0; i < event.touches.length; ++i) { | |
const touch = event.touches[i]; | |
touch.offsetX = touch.x; | |
touch.offsetY = touch.y; | |
} | |
event.preventDefault = noop; | |
event.stopPropagation = noop; | |
event.stopImmediatePropagation = noop; | |
return event; | |
} | |
function dispatchEvent( | |
zrenderId: number, | |
types: HandlerName[], | |
nativeEvent: any, | |
stage: 'start' | 'end' | 'change' | undefined, | |
props: any = { | |
zrX: nativeEvent.x, | |
zrY: nativeEvent.y, | |
} | |
) { | |
if (zrenderId) { | |
var handler = getInstance(zrenderId).handler; | |
types.forEach(function (type) { | |
handler.dispatch(type, { | |
preventDefault: noop, | |
stopImmediatePropagation: noop, | |
stopPropagation: noop, | |
...props, | |
}); | |
stage && handler.processGesture(wrapTouch(nativeEvent), stage); | |
}); | |
} | |
} | |
export const GestureHandler = ({ zrenderId, children }) => { | |
const panRef = useRef(); | |
const pinchRef = useRef(); | |
const onGestureEvent = useMemo( | |
() => ({ nativeEvent }) => { | |
dispatchEvent(zrenderId, ['mousemove'], nativeEvent, 'change'); | |
}, | |
[zrenderId] | |
); | |
const onHandlerStateChange = useMemo( | |
() => ({ nativeEvent }) => { | |
const { state } = nativeEvent; | |
if (state === State.BEGAN) { | |
dispatchEvent(zrenderId, ['mousedown', 'mousemove'], nativeEvent, 'start'); | |
} else if (state === State.END) { | |
dispatchEvent(zrenderId, ['mouseup', 'click'], nativeEvent, 'end'); | |
} | |
}, | |
[zrenderId] | |
); | |
const onPinchGestureEvent = useMemo( | |
() => ({ nativeEvent }) => { | |
const { scale, focalX, focalY } = nativeEvent; | |
dispatchEvent(zrenderId, ['mousewheel'], nativeEvent, undefined, { | |
zrX: focalX, | |
zrY: focalY, | |
zrDelta: (scale - 1) / 120, | |
}); | |
}, | |
[zrenderId] | |
); | |
return ( | |
<PanGestureHandler | |
ref={panRef} | |
simultaneousHandlers={pinchRef} | |
onGestureEvent={onGestureEvent} | |
onHandlerStateChange={onHandlerStateChange} | |
> | |
<PinchGestureHandler | |
ref={pinchRef} | |
simultaneousHandlers={panRef} | |
onGestureEvent={onPinchGestureEvent} | |
> | |
{children} | |
</PinchGestureHandler> | |
</PanGestureHandler> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment