forked from toggle-corp/re-map
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapShapeEditor.tsx
More file actions
179 lines (148 loc) · 4.33 KB
/
Copy pathMapShapeEditor.tsx
File metadata and controls
179 lines (148 loc) · 4.33 KB
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
import { useContext, useEffect, useState } from 'react';
import MapboxDraw from '@mapbox/mapbox-gl-draw/dist/mapbox-gl-draw';
import { MapChildContext } from './context';
// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};
type Mode = 'simple_select' | 'direct_select' | 'draw_point' | 'draw_line_string' | 'draw_polygon';
interface EditEvent {
features: mapboxgl.MapboxGeoJSONFeature[];
}
interface ModeChangeEvent {
mode: Mode;
}
interface Props {
geoJsons: mapboxgl.MapboxGeoJSONFeature[];
onCreate?: (geojsons: mapboxgl.MapboxGeoJSONFeature[]) => void;
onDelete?: (geojsons: mapboxgl.MapboxGeoJSONFeature[]) => void;
onUpdate?: (geojsons: mapboxgl.MapboxGeoJSONFeature[]) => void;
onModeChange?: (mode: Mode) => void;
}
const MapShapeEditor = (props: Props) => {
const {
onCreate,
onDelete,
onUpdate,
onModeChange,
geoJsons,
} = props;
const {
map,
mapStyle,
isMapDestroyed,
} = useContext(MapChildContext);
const [initialGeoJsons] = useState(geoJsons);
// Create and destroy control
useEffect(
() => {
if (!map || !mapStyle) {
return noop;
}
const draw = new MapboxDraw({
displayControlsDefault: false,
controls: {
point: true,
polygon: true,
trash: true,
},
});
map.addControl(
draw,
);
// Load geojsons
initialGeoJsons.forEach((geoJson) => {
draw.add(geoJson);
});
return () => {
if (!isMapDestroyed()) {
map.removeControl(draw);
}
};
},
[map, mapStyle, isMapDestroyed, initialGeoJsons],
);
// Handle change in draw.create
useEffect(
() => {
if (!map || !mapStyle) {
return noop;
}
const handleCreate = (e: EditEvent) => {
if (onCreate) {
onCreate(e.features);
}
};
map.on('draw.create', handleCreate);
return () => {
if (!isMapDestroyed()) {
map.off('draw.create', handleCreate);
}
};
},
[map, mapStyle, onCreate, isMapDestroyed],
);
// Handle change in draw.update
useEffect(
() => {
if (!map || !mapStyle) {
return noop;
}
const handleUpdate = (e: EditEvent) => {
if (onUpdate) {
onUpdate(e.features);
}
};
map.on('draw.update', handleUpdate);
return () => {
if (!isMapDestroyed()) {
map.off('draw.update', handleUpdate);
}
};
},
[map, mapStyle, onUpdate, isMapDestroyed],
);
// Handle change in draw.delete
useEffect(
() => {
if (!map || !mapStyle) {
return noop;
}
const handleDelete = (e: EditEvent) => {
if (onDelete) {
onDelete(e.features);
}
};
map.on('draw.delete', handleDelete);
return () => {
if (!isMapDestroyed()) {
map.off('draw.delete', handleDelete);
}
};
},
[map, mapStyle, onDelete, isMapDestroyed],
);
// Handle change in draw.modechange
useEffect(
() => {
if (!map || !mapStyle) {
return noop;
}
const handleModeChange = (e: ModeChangeEvent) => {
if (onModeChange) {
onModeChange(e.mode);
}
};
map.on('draw.modechange', handleModeChange);
return () => {
if (!isMapDestroyed) {
map.off('draw.modechange', handleModeChange);
}
};
},
[map, mapStyle, onModeChange, isMapDestroyed],
);
return null;
};
MapShapeEditor.defaultProps = {
geoJsons: [],
};
export default MapShapeEditor;