-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathMap.tsx
224 lines (212 loc) · 7.72 KB
/
Map.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import React, { FC, PropsWithChildren, useContext, useMemo, useState } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import maplibregl from 'maplibre-gl';
import ReactMapGL, { AttributionControl, ScaleControl } from 'react-map-gl';
import { withTranslation } from 'react-i18next';
import { TFunction } from 'i18next';
import VirtualLayers from 'applications/osrd/views/OSRDSimulation/VirtualLayers';
import colors from 'common/Map/Consts/colors';
import 'common/Map/Map.scss';
/* Main data & layers */
import { LAYER_GROUPS_ORDER, LAYERS } from 'config/layerOrder';
import TracksOSM from 'common/Map/Layers/TracksOSM';
import { RootState } from 'reducers';
import Background from '../../common/Map/Layers/Background';
import OSM from '../../common/Map/Layers/OSM';
import Hillshade from '../../common/Map/Layers/Hillshade';
import Platforms from '../../common/Map/Layers/Platforms';
import osmBlankStyle from '../../common/Map/Layers/osmBlankStyle';
import IGN_BD_ORTHO from '../../common/Map/Layers/IGN_BD_ORTHO';
import { Viewport } from '../../reducers/map';
import { getMapMouseEventNearestFeature } from '../../utils/mapboxHelper';
import EditorContext from './context';
import {
CommonToolState,
EditorContextType,
EditorState,
ExtendedEditorContextType,
OSRDConf,
Tool,
} from './tools/types';
import IGN_SCAN25 from 'common/Map/Layers/IGN_SCAN25';
import IGN_CADASTRE from 'common/Map/Layers/IGN_CADASTRE';
interface MapProps<S extends CommonToolState = CommonToolState> {
t: TFunction;
toolState: S;
setToolState: (newState: S) => void;
activeTool: Tool<S>;
mapStyle: string;
viewport: Viewport;
setViewport: (newViewport: Partial<Viewport>) => void;
}
interface MapState {
isLoaded: boolean;
isDragging: boolean;
isHovering: boolean;
}
const MapUnplugged: FC<PropsWithChildren<MapProps>> = ({
toolState,
setToolState,
activeTool,
mapStyle,
viewport,
setViewport,
children,
}) => {
const dispatch = useDispatch();
const [mapState, setMapState] = useState<MapState>({
isLoaded: true,
isDragging: false,
isHovering: false,
});
const context = useContext(EditorContext) as EditorContextType<CommonToolState>;
const osrdConf = useSelector((state: { osrdconf: OSRDConf }) => state.osrdconf);
const editorState = useSelector((state: { editor: EditorState }) => state.editor);
const { showOSM } = useSelector((state: RootState) => state.map);
const extendedContext = useMemo<ExtendedEditorContextType<CommonToolState>>(
() => ({
...context,
dispatch,
editorState,
osrdConf,
mapState: {
viewport,
mapStyle,
},
}),
[context, dispatch, editorState, mapStyle, osrdConf, viewport]
);
const cursor = useMemo(
() => (activeTool.getCursor ? activeTool.getCursor(extendedContext, mapState) : 'default'),
[activeTool, extendedContext, mapState]
);
return (
<>
<div
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
tabIndex={0}
role="none"
className="w-100 h-100"
onKeyDown={(e) => {
if (activeTool.onKeyDown) activeTool.onKeyDown(e.nativeEvent, extendedContext);
}}
>
<ReactMapGL
{...viewport}
mapLib={maplibregl}
style={{ width: '100%', height: '100%' }}
mapStyle={osmBlankStyle}
onMove={(e) => setViewport(e.viewState)}
onMoveStart={() => setMapState((prev) => ({ ...prev, isDragging: true }))}
onMoveEnd={() => setMapState((prev) => ({ ...prev, isDragging: false }))}
onMouseMove={(e) => {
const nearestResult = getMapMouseEventNearestFeature(e);
const partialToolState: Partial<CommonToolState> = {
hovered: null,
mousePosition: [e.lngLat.lng, e.lngLat.lat],
};
const partialMapState: Partial<MapState> = { isHovering: false };
// if we hover something
if (nearestResult) {
const { feature } = nearestResult;
const eventWithFeature = {
...e,
preventDefault: e.preventDefault,
features: [feature],
};
partialMapState.isHovering = true;
if (activeTool.onHover) {
activeTool.onHover(eventWithFeature, extendedContext);
} else if (feature && feature.properties) {
const entity = feature?.properties?.id
? editorState.entitiesIndex[feature.properties.id]
: undefined;
partialToolState.hovered = entity || null;
setToolState({ ...toolState, ...partialToolState });
}
} else if (activeTool.onMove) {
activeTool.onMove(e, extendedContext);
}
setMapState((prev) => ({ ...prev, ...partialMapState }));
}}
onLoad={(e) => {
// need to call resize, otherwise sometime the canvas doesn't take 100%
e.target.resize();
setMapState((prev) => ({ ...prev, isLoaded: false }));
}}
onResize={(e) => {
setViewport({
width: e.target.getContainer().offsetWidth,
height: e.target.getContainer().offsetHeight,
});
}}
attributionControl={false}
touchZoomRotate
doubleClickZoom={false}
interactive
interactiveLayerIds={
activeTool.getInteractiveLayers ? activeTool.getInteractiveLayers(extendedContext) : []
}
cursor={cursor}
onClick={(e) => {
const nearestResult = getMapMouseEventNearestFeature(e);
const eventWithFeature = nearestResult
? {
...e,
preventDefault: e.preventDefault,
features: [nearestResult.feature],
}
: e;
if (toolState.hovered && activeTool.onClickFeature) {
activeTool.onClickFeature(toolState.hovered, eventWithFeature, extendedContext);
}
if (activeTool.onClickMap) {
activeTool.onClickMap(eventWithFeature, extendedContext);
}
}}
>
<VirtualLayers />
<AttributionControl position="bottom-right" customAttribution="©SNCF Réseau" />
<ScaleControl
maxWidth={100}
unit="metric"
style={{
left: 20,
bottom: 20,
}}
/>
{/* Common layers */}
<Background
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]}
/>
<TracksOSM
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.TRACKS_OSM.GROUP]}
/>
<IGN_BD_ORTHO layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]} />
<IGN_SCAN25 layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]} />
<IGN_CADASTRE layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]} />
{!showOSM ? null : (
<>
<OSM mapStyle={mapStyle} layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]} />
<Hillshade
mapStyle={mapStyle}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.BACKGROUND.GROUP]}
/>
</>
)}
<Platforms
colors={colors[mapStyle]}
layerOrder={LAYER_GROUPS_ORDER[LAYERS.PLATFORMS.GROUP]}
/>
{/* Tool specific layers */}
{activeTool.layersComponent && <activeTool.layersComponent />}
</ReactMapGL>
</div>
{children}
</>
);
};
const Map = withTranslation()(MapUnplugged);
export default Map;