@@ -4,7 +4,6 @@ import maplibregl from 'maplibre-gl';
4
4
import ReactMapGL , { AttributionControl , ScaleControl } from 'react-map-gl' ;
5
5
import { withTranslation } from 'react-i18next' ;
6
6
import { TFunction } from 'i18next' ;
7
-
8
7
import VirtualLayers from 'applications/osrd/views/OSRDSimulation/VirtualLayers' ;
9
8
import colors from 'common/Map/Consts/colors' ;
10
9
import 'common/Map/Map.scss' ;
@@ -17,6 +16,7 @@ import Platforms from '../../common/Map/Layers/Platforms';
17
16
import osmBlankStyle from '../../common/Map/Layers/osmBlankStyle' ;
18
17
import OrthoPhoto from '../../common/Map/Layers/OrthoPhoto' ;
19
18
import { Viewport } from '../../reducers/map' ;
19
+ import { getMapMouseEventNearestFeature } from '../../utils/mapboxHelper' ;
20
20
import EditorContext from './context' ;
21
21
import {
22
22
CommonToolState ,
@@ -99,26 +99,39 @@ const MapUnplugged: FC<PropsWithChildren<MapProps>> = ({
99
99
onMove = { ( e ) => setViewport ( e . viewState ) }
100
100
onMoveStart = { ( ) => setMapState ( ( prev ) => ( { ...prev , isDragging : true } ) ) }
101
101
onMoveEnd = { ( ) => setMapState ( ( prev ) => ( { ...prev , isDragging : false } ) ) }
102
- onMouseEnter = { ( e ) => {
103
- setMapState ( ( prev ) => ( { ...prev , isHovering : true } ) ) ;
104
- const feature = ( e . features || [ ] ) [ 0 ] ;
105
- if ( activeTool . onHover ) {
106
- activeTool . onHover ( e , extendedContext ) ;
107
- } else if ( feature && feature . properties ) {
108
- const entity = feature ?. properties ?. id
109
- ? editorState . entitiesIndex [ feature . properties . id ]
110
- : undefined ;
111
- setToolState ( {
112
- ...toolState ,
113
- hovered : entity || null ,
114
- } ) ;
102
+ onMouseMove = { ( e ) => {
103
+ const nearestResult = getMapMouseEventNearestFeature ( e ) ;
104
+ const partialToolState : Partial < CommonToolState > = {
105
+ hovered : null ,
106
+ mousePosition : [ e . lngLat . lng , e . lngLat . lat ] ,
107
+ } ;
108
+ const partialMapState : Partial < MapState > = { isHovering : false } ;
109
+
110
+ // if we hover something
111
+ if ( nearestResult ) {
112
+ const { feature } = nearestResult ;
113
+ const eventWithFeature = {
114
+ ...e ,
115
+ preventDefault : e . preventDefault ,
116
+ features : [ feature ] ,
117
+ } ;
118
+ partialMapState . isHovering = true ;
119
+ if ( activeTool . onHover ) {
120
+ activeTool . onHover ( eventWithFeature , extendedContext ) ;
121
+ } else if ( feature && feature . properties ) {
122
+ const entity = feature ?. properties ?. id
123
+ ? editorState . entitiesIndex [ feature . properties . id ]
124
+ : undefined ;
125
+ partialToolState . hovered = entity || null ;
126
+ }
115
127
} else {
116
- setToolState ( { ...toolState , hovered : null } ) ;
128
+ if ( activeTool . onMove ) {
129
+ activeTool . onMove ( e , extendedContext ) ;
130
+ }
117
131
}
118
- } }
119
- onMouseLeave = { ( ) => {
120
- setMapState ( ( prev ) => ( { ...prev , isHovering : false } ) ) ;
121
- setToolState ( { ...toolState , mousePosition : null , hovered : null } ) ;
132
+
133
+ setToolState ( { ...toolState , ...partialToolState } ) ;
134
+ setMapState ( ( prev ) => ( { ...prev , ...partialMapState } ) ) ;
122
135
} }
123
136
onLoad = { ( e ) => {
124
137
// need to call resize, otherwise sometime the canvas doesn't take 100%
@@ -140,17 +153,19 @@ const MapUnplugged: FC<PropsWithChildren<MapProps>> = ({
140
153
}
141
154
cursor = { cursor }
142
155
onClick = { ( e ) => {
156
+ const nearestResult = getMapMouseEventNearestFeature ( e ) ;
157
+ const eventWithFeature = nearestResult
158
+ ? {
159
+ ...e ,
160
+ preventDefault : e . preventDefault ,
161
+ features : [ nearestResult . feature ] ,
162
+ }
163
+ : e ;
143
164
if ( toolState . hovered && activeTool . onClickFeature ) {
144
- activeTool . onClickFeature ( toolState . hovered , e , extendedContext ) ;
165
+ activeTool . onClickFeature ( toolState . hovered , eventWithFeature , extendedContext ) ;
145
166
}
146
167
if ( activeTool . onClickMap ) {
147
- activeTool . onClickMap ( e , extendedContext ) ;
148
- }
149
- } }
150
- onMouseMove = { ( e ) => {
151
- setToolState ( { ...toolState , mousePosition : [ e . lngLat . lng , e . lngLat . lat ] } ) ;
152
- if ( activeTool . onMove ) {
153
- activeTool . onMove ( e , extendedContext ) ;
168
+ activeTool . onClickMap ( eventWithFeature , extendedContext ) ;
154
169
}
155
170
} }
156
171
>
0 commit comments