1- import { Map , Overlay } from 'ol'
2- import { ContextMenuContent } from '@/map/ContextMenuContent'
3- import { useCallback , useContext , useEffect , useRef , useState } from 'react'
4- import { QueryPoint } from '@/stores/QueryStore'
5- import { fromLonLat , toLonLat } from 'ol/proj'
1+ import { Map , Overlay } from 'ol'
2+ import { ContextMenuContent } from '@/map/ContextMenuContent'
3+ import { useCallback , useContext , useEffect , useRef , useState } from 'react'
4+ import { QueryPoint } from '@/stores/QueryStore'
5+ import { fromLonLat , toLonLat } from 'ol/proj'
66import styles from '@/layers/ContextMenu.module.css'
7- import { RouteStoreState } from '@/stores/RouteStore'
8- import { Coordinate } from '@/utils'
9- import Dispatcher from " @/stores/Dispatcher" ;
10- import { AddPoint , SetPoint } from " @/actions/Actions" ;
11- import { coordinateToText } from " @/Converters" ;
12- import { SettingsContext } from " @/contexts/SettingsContext" ;
7+ import { RouteStoreState } from '@/stores/RouteStore'
8+ import { Coordinate } from '@/utils'
9+ import Dispatcher from ' @/stores/Dispatcher'
10+ import { AddPoint , SetPoint } from ' @/actions/Actions'
11+ import { coordinateToText } from ' @/Converters'
12+ import { SettingsContext } from ' @/contexts/SettingsContext'
1313
1414interface ContextMenuProps {
1515 map : Map
@@ -21,56 +21,64 @@ const overlay = new Overlay({
2121 autoPan : true ,
2222} )
2323
24- export default function ContextMenu ( { map, route, queryPoints} : ContextMenuProps ) {
24+ export default function ContextMenu ( { map, route, queryPoints } : ContextMenuProps ) {
2525 const [ menuCoordinate , setMenuCoordinate ] = useState < Coordinate | null > ( null )
2626 const container = useRef < HTMLDivElement | null > ( null )
2727 const settings = useContext ( SettingsContext )
2828
29- const openContextMenu = useCallback ( ( e : any ) => {
30- e . preventDefault ( )
31- const coordinate = map . getEventCoordinate ( e )
32- const lonLat = toLonLat ( coordinate )
33- setMenuCoordinate ( { lng : lonLat [ 0 ] , lat : lonLat [ 1 ] } )
34- } , [ map ] )
29+ const openContextMenu = useCallback (
30+ ( e : any ) => {
31+ e . preventDefault ( )
32+ const coordinate = map . getEventCoordinate ( e )
33+ const lonLat = toLonLat ( coordinate )
34+ setMenuCoordinate ( { lng : lonLat [ 0 ] , lat : lonLat [ 1 ] } )
35+ } ,
36+ [ map ]
37+ )
3538
36- const handleClick = useCallback ( ( e : any ) => {
37- if ( e . dragging ) return
39+ const handleClick = useCallback (
40+ ( e : any ) => {
41+ if ( e . dragging ) return
3842
39- // If click is inside the context menu, do nothing
40- const clickedElement = document . elementFromPoint ( e . pixel [ 0 ] , e . pixel [ 1 ] )
41- if ( container . current ?. contains ( clickedElement ) ) {
42- return
43- }
43+ // If click is inside the context menu, do nothing
44+ const clickedElement = document . elementFromPoint ( e . pixel [ 0 ] , e . pixel [ 1 ] )
45+ if ( container . current ?. contains ( clickedElement ) ) {
46+ return
47+ }
4448
45- if ( menuCoordinate ) {
46- // Context menu is open -> close it and skip adding a point
47- setMenuCoordinate ( null )
48- return
49- }
49+ if ( menuCoordinate ) {
50+ // Context menu is open -> close it and skip adding a point
51+ setMenuCoordinate ( null )
52+ return
53+ }
5054
51- if ( ! settings . addPointOnClick ) return
55+ if ( ! settings . addPointOnClick ) return
5256
53- const lonLat = toLonLat ( e . coordinate )
54- const myCoord = { lng : lonLat [ 0 ] , lat : lonLat [ 1 ] }
57+ const lonLat = toLonLat ( e . coordinate )
58+ const myCoord = { lng : lonLat [ 0 ] , lat : lonLat [ 1 ] }
5559
56- let idx = queryPoints . length
57- if ( idx == 2 ) {
58- if ( ! queryPoints [ 1 ] . isInitialized ) idx -- ;
59- }
60- if ( idx == 1 ) {
61- if ( ! queryPoints [ 0 ] . isInitialized ) idx -- ;
62- }
63- if ( idx < 2 ) {
64- const setPoint = new SetPoint ( {
65- ...queryPoints [ idx ] ,
66- coordinate : myCoord ,
67- queryText : coordinateToText ( myCoord ) ,
68- isInitialized : true
69- } , false ) ;
70- Dispatcher . dispatch ( setPoint )
71- } else
72- Dispatcher . dispatch ( new AddPoint ( idx , myCoord , true , false ) )
73- } , [ menuCoordinate , settings . addPointOnClick , queryPoints ] )
60+ let idx = queryPoints . length
61+ if ( idx == 2 ) {
62+ if ( ! queryPoints [ 1 ] . isInitialized ) idx --
63+ }
64+ if ( idx == 1 ) {
65+ if ( ! queryPoints [ 0 ] . isInitialized ) idx --
66+ }
67+ if ( idx < 2 ) {
68+ const setPoint = new SetPoint (
69+ {
70+ ...queryPoints [ idx ] ,
71+ coordinate : myCoord ,
72+ queryText : coordinateToText ( myCoord ) ,
73+ isInitialized : true ,
74+ } ,
75+ false
76+ )
77+ Dispatcher . dispatch ( setPoint )
78+ } else Dispatcher . dispatch ( new AddPoint ( idx , myCoord , true , false ) )
79+ } ,
80+ [ menuCoordinate , settings . addPointOnClick , queryPoints ]
81+ )
7482
7583 useEffect ( ( ) => {
7684 overlay . setElement ( container . current ! )
@@ -81,8 +89,8 @@ export default function ContextMenu({map, route, queryPoints}: ContextMenuProps)
8189 function onMapTargetChange ( ) {
8290 const targetElement = map . getTargetElement ( )
8391 if ( ! targetElement ) {
84- console . warn ( 'Map target element is null. Delaying event listeners setup.' ) ;
85- return ;
92+ console . warn ( 'Map target element is null. Delaying event listeners setup.' )
93+ return
8694 }
8795
8896 targetElement . addEventListener ( 'contextmenu' , openContextMenu )
0 commit comments