import * as React from 'react' ; import { Platform , findNodeHandle as findNodeHandleRN } from 'react-native' ; import { State } from '../State' ; import { EventType } from '../EventType' ; import { ValueOf } from '../typeUtils' ; import { handlerIDToTag } from './handlersRegistry' ; import { toArray } from '../utils' ; const commonProps = [ 'id' , 'enabled' , 'shouldCancelWhenOutside' , 'hitSlop' , ] as const ; const componentInteractionProps = [ 'waitFor' , 'simultaneousHandlers' ] as const ; export const baseGestureHandlerProps = [ ... commonProps , ... componentInteractionProps , 'onBegan' , 'onFailed' , 'onCancelled' , 'onActivated' , 'onEnded' , 'onGestureEvent' , 'onHandlerStateChange' , ] as const ; export const baseGestureHandlerWithMonitorProps = [ ... commonProps , 'needsPointerData' , 'manualActivation' , ] ; export interface GestureEventPayload { handlerTag : number ; numberOfPointers : number ; state : ValueOf < typeof State > ; } export interface HandlerStateChangeEventPayload { handlerTag : number ; numberOfPointers : number ; state : ValueOf < typeof State > ; oldState : ValueOf < typeof State > ; } export type HitSlop = number | Partial < Record < 'left' | 'right' | 'top' | 'bottom' | 'vertical' | 'horizontal' , number > > | Record < 'width' | 'left' , number > | Record < 'width' | 'right' , number > | Record < 'height' | 'top' , number > | Record < 'height' | 'bottom' , number > ; export interface GestureEvent < ExtraEventPayloadT = Record < string , unknown > > { nativeEvent : Readonly < GestureEventPayload & ExtraEventPayloadT > ; } export interface HandlerStateChangeEvent < ExtraEventPayloadT = Record < string , unknown > > { nativeEvent : Readonly < HandlerStateChangeEventPayload & ExtraEventPayloadT > ; } export type TouchData = { id : number ; x : number ; y : number ; absoluteX : number ; absoluteY : number ; } ; export type GestureTouchEvent = { handlerTag : number ; numberOfTouches : number ; state : ValueOf < typeof State > ; eventType : EventType ; allTouches : TouchData [ ] ; changedTouches : TouchData [ ] ; } ; export type GestureUpdateEvent < GestureEventPayloadT = Record < string , unknown > > = GestureEventPayload & GestureEventPayloadT ; export type GestureStateChangeEvent < GestureStateChangeEventPayloadT = Record < string , unknown > > = HandlerStateChangeEventPayload & GestureStateChangeEventPayloadT ; export type CommonGestureConfig = { enabled ? : boolean ; shouldCancelWhenOutside ? : boolean ; hitSlop ? : HitSlop ; } ; export type BaseGestureHandlerProps < ExtraEventPayloadT extends Record < string , unknown > = Record < string , unknown > > = CommonGestureConfig & { id ? : string ; waitFor ? : React . Ref < unknown > | React . Ref < unknown > [ ] ; simultaneousHandlers ? : React . Ref < unknown > | React . Ref < unknown > [ ] ; onBegan ? : ( event : HandlerStateChangeEvent ) => void ; onFailed ? : ( event : HandlerStateChangeEvent ) => void ; onCancelled ? : ( event : HandlerStateChangeEvent ) => void ; onActivated ? : ( event : HandlerStateChangeEvent ) => void ; onEnded ? : ( event : HandlerStateChangeEvent ) => void ; onGestureEvent ? : ( event : GestureEvent < ExtraEventPayloadT > ) => void ; onHandlerStateChange ? : ( event : HandlerStateChangeEvent < ExtraEventPayloadT > ) => void ; } ; function isConfigParam ( param , name ) { return ( param !== undefined && ( param !== Object ( param ) || ! ( '__isNative' in ( param as Record < string , unknown > ) ) ) && name !== 'onHandlerStateChange' && name !== 'onGestureEvent' ) ; } export function filterConfig ( props , validProps , defaults = { } ) { const filteredConfig = { ... defaults } ; for ( const key of validProps ) { let value = props [ key ] ; if ( isConfigParam ( value , key ) ) { if ( key === 'simultaneousHandlers' || key === 'waitFor' ) { value = transformIntoHandlerTags ( props [ key ] ) ; } else if ( key === 'hitSlop' && typeof value !== 'object' ) { value = { top : value , left : value , bottom : value , right : value } ; } filteredConfig [ key ] = value ; } } return filteredConfig ; } function transformIntoHandlerTags ( handlerIDs ) { handlerIDs = toArray ( handlerIDs ) ; if ( Platform . OS === 'web' ) { return handlerIDs . map ( ( { current } ) => current ) . filter ( ( handle ) => handle ) ; } return handlerIDs . map ( ( handlerID ) => handlerIDToTag [ handlerID ] || handlerID . current ?. handlerTag || - 1 ) . filter ( ( handlerTag ) => handlerTag > 0 ) ; } export function findNodeHandle ( node ) { if ( Platform . OS === 'web' ) return node ; return findNodeHandleRN ( node ) ; }