import type { ReactElementFiber } from '../element/types'; import { parseTouchEvent, trackClick } from '../click/trackClick'; const InjectSign = '__$bd_inject_props_onpress$__'; function findTouchableNode( node: ReactElementFiber, depth: number = 0 ): ReactElementFiber | null { if (!node.child) { // console.info(`depth: ${depth}`) return null; } if (node.child.memoizedProps?.hasOwnProperty('accessible')) { return node.child; } if (depth > 9) { return null; } return findTouchableNode(node.child, ++depth); } export function injectPropsPress(node: ReactElementFiber) { const touchableNode = findTouchableNode(node); if (!touchableNode) { return; } const stateNode = touchableNode.stateNode; if (!stateNode) { return; } const props = { ...stateNode.props }; if (!props.onPress) { return; } if (props[InjectSign]) { return; } props.onPress = new Proxy(props.onPress, { apply: (target, thisArgument, argumentsList) => { const event = argumentsList[0]; try { parseTouchEvent(event, (touch) => { try { if (touch) { trackClick(node, touch); } } catch (e: any) { console.warn(e.message); } Reflect.apply(target, thisArgument, argumentsList); }); } catch (e: any) { console.warn(e.message); Reflect.apply(target, thisArgument, argumentsList); } }, }); props[InjectSign] = 1; stateNode.props = props; Object.freeze(props); }