-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuseLeafletEditable.js
More file actions
150 lines (133 loc) · 4.09 KB
/
useLeafletEditable.js
File metadata and controls
150 lines (133 loc) · 4.09 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
import { useCallback, useEffect } from "react";
import { Polyline, Marker, Polygon, Circle, Rectangle } from "leaflet";
import { useMap } from "react-leaflet";
const eventHandlers = Object.freeze({
onCreated: "editable:created",
onEnable: "editable:enable",
onDisable: "editable:disable",
onEditing: "editable:editing",
onDragStart: "editable:dragstart",
onDrag: "editable:drag",
onDragEnd: "editable:dragend",
onDrawingStart: "editable:drawing:start",
onDrawingEnd: "editable:drawing:end",
onDrawingCancel: "editable:drawing:cancel",
onDrawingCommit: "editable:drawing:commit",
onDrawingMousedown: "editable:drawing:mousedown",
onDrawingMouseup: "editable:drawing:mouseup",
onDrawingClick: "editable:drawing:click",
onDrawingMove: "editable:drawing:move",
onDrawingClicked: "editable:drawing:clicked",
onVertexNew: "editable:vertex:new",
onVertexClick: "editable:vertex:click",
onVertexClicked: "editable:vertex:clicked",
onVertexRawclick: "editable:vertex:rawclick",
onVertexDeleted: "editable:vertex:deleted",
onVertexCtrlclick: "editable:vertex:ctrlclick",
onVertexShiftclick: "editable:vertex:shiftclick",
onVertexMetakeyclick: "editable:vertex:metakeyclick",
onVertexAltclick: "editable:vertex:altclick",
onVertexContextmenu: "editable:vertex:contextmenu",
onVertexMousedown: "editable:vertex:mousedown",
onVertexDrag: "editable:vertex:drag",
onVertexDragstart: "editable:vertex:dragstart",
onVertexDragend: "editable:vertex:dragend",
onMiddlemarkerMousedown: "editable:middlemarker:mousedown",
onShapeNew: "editable:shape:new",
onShapeDelete: "editable:shape:delete",
onShapeDeleted: "editable:shape:deleted",
});
function useLeafletEditable({ events }) {
const map = useMap();
useEffect(() => {
const keys = Object.keys(eventHandlers);
keys.forEach((key) => {
const handler = events[key];
if (handler) {
map.on(eventHandlers[key], handler);
}
});
return () => {
keys.forEach((key) => {
const handler = events[key];
if (handler) {
map.off(eventHandlers[key], handler);
}
});
};
}, [map, events]);
const drawing = useCallback(() => map.editTools.drawing(), [map]);
const stopDrawing = useCallback(() => map.editTools.stopDrawing(), [map]);
const commitDrawing = useCallback(
(event) => map.editTools.commitDrawing(event),
[map],
);
const startPolyline = useCallback(
(latLng, options) => map.editTools.startPolyline(latLng, options),
[map],
);
const startPolygon = useCallback(
(latLng, options) => map.editTools.startPolygon(latLng, options),
[map],
);
const startMarker = useCallback(
(latLng, options) => map.editTools.startMarker(latLng, options),
[map],
);
const startRectangle = useCallback(
(latLng, options) => map.editTools.startRectangle(latLng, options),
[map],
);
const startCircle = useCallback(
(latLng, options) => map.editTools.startCircle(latLng, options),
[map],
);
const clearAll = useCallback(() => map.editTools.clearLayers(), [map]);
// BaseEditor related methods
const enableEdit = useCallback(
(feature) => {
if (
feature instanceof Marker ||
feature instanceof Polyline ||
feature instanceof Polygon ||
feature instanceof Rectangle ||
feature instanceof Circle
) {
feature.enableEdit();
} else {
console.error("Feature is not editable", feature);
}
},
[map],
);
const disableEdit = useCallback(
(feature) => {
if (
feature instanceof Marker ||
feature instanceof Polyline ||
feature instanceof Polygon ||
feature instanceof Rectangle ||
feature instanceof Circle
) {
feature.disableEdit();
} else {
console.error("Feature is not editable", feature);
}
},
[map],
);
return {
drawing,
stopDrawing,
commitDrawing,
startPolyline,
startPolygon,
startMarker,
startRectangle,
startCircle,
clearAll,
enableEdit,
disableEdit,
};
}
export default useLeafletEditable;