-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathnav.tsx
183 lines (176 loc) · 5.66 KB
/
nav.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
import React from 'react';
import { Dispatch } from 'redux';
import { IconType } from 'react-icons';
import { BiTargetLock } from 'react-icons/bi';
import { BsFillExclamationOctagonFill } from 'react-icons/bs';
import { FaCompass } from 'react-icons/fa';
import { GiRailway } from 'react-icons/gi';
import { isNil } from 'lodash';
import { NavigateFunction } from 'react-router-dom';
import { MapRef } from 'react-map-gl/maplibre';
import { Viewport } from 'reducers/map';
import { selectLayers } from 'reducers/editor';
import { Shortcut } from 'utils/hooks/useKeyboardShortcuts';
import { ModalContextType } from 'common/BootstrapSNCF/ModalSNCF/ModalProvider';
import InfraSelectorModal from 'common/InfraSelector/InfraSelectorModal';
import { GoSearch, GoStack, GoZoomIn, GoZoomOut } from 'react-icons/go';
import { EditorState, EDITOAST_TO_LAYER_DICT, EditoastType } from './tools/types';
import LayersModal from './components/LayersModal';
import { SelectionState } from './tools/selection/types';
import { EditorContextType, Tool } from './tools/editorContextTypes';
const ZOOM_DEFAULT = 5;
const ZOOM_DELTA = 1.5;
const DEFAULT_VIEWPORT = {
latitude: 47.3,
longitude: 2.0,
zoom: 5.0,
bearing: 0,
pitch: 0,
};
export interface NavButton {
id: string;
icon: IconType;
labelTranslationKey: string;
shortcut?: Omit<Shortcut, 'handler'>;
// Tool appearance:
isActive?: (editorState: EditorState) => boolean;
isHidden?: (editorState: EditorState) => boolean;
isDisabled?: (editorState: EditorState) => boolean;
isBlink?: (editorState: EditorState, infraId?: string | number) => boolean;
// On click button:
onClick?: <S = unknown>(
context: {
navigate: NavigateFunction;
dispatch: Dispatch;
viewport: Viewport;
editorState: EditorState;
setViewport: (newViewport: Partial<Viewport>) => void;
openModal: ModalContextType['openModal'];
closeModal: ModalContextType['closeModal'];
setIsSearchToolOpened: React.Dispatch<React.SetStateAction<boolean>>;
mapRef: MapRef;
},
toolContext: {
activeTool: Tool<S>;
toolState: S;
setToolState: (newState: S) => void;
switchTool: EditorContextType['switchTool'];
}
) => void;
}
const NavButtons: NavButton[][] = [
[
{
id: 'search',
icon: GoSearch,
labelTranslationKey: 'common.search',
onClick({ setIsSearchToolOpened }) {
setIsSearchToolOpened((state) => !state);
},
},
{
id: 'zoom-in',
icon: GoZoomIn,
labelTranslationKey: 'common.zoom-in',
onClick({ setViewport, viewport }) {
setViewport({
...viewport,
zoom: (viewport.zoom || ZOOM_DEFAULT) + ZOOM_DELTA,
});
},
},
{
id: 'zoom-out',
icon: GoZoomOut,
labelTranslationKey: 'common.zoom-out',
onClick({ setViewport, viewport }) {
setViewport({
...viewport,
zoom: (viewport.zoom || ZOOM_DEFAULT) - ZOOM_DELTA,
});
},
},
{
id: 'recenter',
icon: BiTargetLock,
labelTranslationKey: 'Editor.nav.recenter',
onClick({ setViewport, viewport }) {
setViewport({
...viewport,
...DEFAULT_VIEWPORT,
});
},
},
{
id: 'reset-viewport',
icon: FaCompass,
labelTranslationKey: 'common.reset-north',
onClick({ setViewport, viewport }) {
setViewport({
...viewport,
bearing: 0,
pitch: 0,
});
},
},
],
[
{
id: 'layers',
icon: GoStack,
labelTranslationKey: 'Editor.nav.toggle-layers',
shortcut: { code: 'KeyL', optionalKeys: { ctrlKey: true, shiftKey: true } },
onClick({ openModal, editorState }, { activeTool, toolState, setToolState }) {
openModal(
<LayersModal
initialLayers={editorState.editorLayers}
frozenLayers={activeTool.requiredLayers}
selection={
activeTool.id === 'select-items'
? (toolState as unknown as SelectionState).selection
: undefined
}
onChange={({ newLayers }) => {
if (activeTool.id === 'select-items') {
const currentState = toolState as unknown as SelectionState;
(setToolState as unknown as (newState: SelectionState) => void)({
...currentState,
selection: currentState.selection.filter((entity) =>
EDITOAST_TO_LAYER_DICT[entity.objType as EditoastType].every((layer) =>
newLayers.has(layer)
)
),
} as SelectionState);
}
}}
/>,
'lg'
);
},
},
{
id: 'infras',
icon: GiRailway,
labelTranslationKey: 'Editor.nav.select-infra',
shortcut: { code: 'KeyI', optionalKeys: { ctrlKey: true, shiftKey: true } },
isBlink: (_editorState, infraId) => isNil(infraId),
async onClick({ navigate, openModal }) {
openModal(<InfraSelectorModal onInfraChange={(id) => navigate(`/editor/${id}`)} />, 'lg');
},
},
{
id: 'infra-errors',
icon: BsFillExclamationOctagonFill,
labelTranslationKey: 'Editor.nav.infra-errors-map',
shortcut: { code: 'KeyE', optionalKeys: { ctrlKey: true, shiftKey: true } },
isActive: (state) => state.editorLayers.has('errors'),
onClick({ dispatch, editorState }) {
const newSet = new Set(editorState.editorLayers);
if (newSet.has('errors')) newSet.delete('errors');
else newSet.add('errors');
dispatch(selectLayers(newSet));
},
},
],
];
export default NavButtons;