import { useState , useEffect } from 'react' ; import { useEventProperties } from '@uiw/react-amap-utils' ; import { ContextMenuProps } from '.' ; export interface UseContextMenu extends ContextMenuProps { } export const useContextMenu = ( props = { } as UseContextMenu ) => { const { map , position , ... other } = props ; const [ contextMenu , setContextMenu ] = useState < AMap . ContextMenu > ( ) ; useEffect ( ( ) => { if ( ! AMap || ! map ) return ; if ( ! contextMenu ) { let instance = new AMap . ContextMenu ( { ... other } ) ; setContextMenu ( instance ) ; const rightclick = ( e ) => instance . open ( map , position || e . lnglat ) ; map . on ( 'rightclick' , rightclick ) ; return ( ) => { if ( instance ) { map . off ( 'rightclick' , rightclick ) ; map && map . removeLayer ( instance ) ; setContextMenu ( undefined ) ; } } ; } } , [ map ] ) ; useEventProperties < AMap . ContextMenu , UseContextMenu > ( contextMenu ! , props , [ 'onOpen' , 'onClose' ] ) ; return { contextMenu , setContextMenu , } ; } ;