-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathModifying.js
More file actions
401 lines (372 loc) · 14.1 KB
/
Copy pathModifying.js
File metadata and controls
401 lines (372 loc) · 14.1 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import Modify from "ol/interaction/Modify";
import Menu from "../Controls/ContextMenu/SimpleMenu";
import LongTouch from "./LongTouch.js";
import { selectStyle } from "./selectFlatStyle";
/** Modifying interaction
* @events delete, cut, copy, paste
*/
class ModifyingInteraction extends Modify {
/**
* Initialize modifying interaction
* @param {*} options extend Openlayers modifying options
* @param {ol.interaction.Select} [options.select] - Selecting interaction linked to modifying interaction
* @param {Function} [options.modifyCondition] - Function to determine if modifying interaction should be activated
* @param {Number} [options.hitTolerance] - hitTolerance of point vertices (default 3)
*/
constructor (options) {
options = options || {};
// if (!options.style) {
// options.style = selectStyle();
// }
// Use features from select interaction if provided
options.features = options.select ? options.select.getFeatures() : options.features;
super(options);
this._menu = new Menu();
this._select = options.select;
// Long touch interaction
this.longtouch = new LongTouch({
pixelTolerance : 6,
delay: 600,
handleLongTouchEvent : (e) => {
if (this.getActive()) {
this._showContextMenu(e, this._isOnPoint(e));
this._islongtouch = true;
}
}
});
this.longtouch.setActive(true);
// Activate modify interaction on select event
this._modifyCondition = options.modifyCondition || (e => true);
this._select.on("select", (e) => {
this.setActive(this._modifyCondition({ type : "activate" }));
});
// refresh style
this.on("change:active", (e) => {
this._select.getFeatures().forEach( f => {
f.changed();
});
});
// Activate on select activation change
this._select.on("change:active", (e) => {
if (this._select.getActive()) {
this.setActive(this._modifyCondition({ type : "activate" }));
} else {
this.setActive(false);
}
});
this._select.on("dblclick", (e) => {
this.setActive(this._modifyCondition(e));
});
this.set("hitTolerance", options.hitTolerance || this._select.hitTolerance_ || 2);
let copyFeatures = [];
// Do something on keyup (delete features)
this._onKey = (evt) => {
if (!this.getActive()) {
return;
}
// Ignore if focused on input
if (evt.target.tagName === "INPUT" || evt.target.tagName === "TEXTAREA" || evt.target.isContentEditable) {
return;
}
// Handle key
if (evt.key === "Delete" || evt.key === "Backspace") {
// get deleted features
const deleted = [];
this._select.getFeatures().forEach( f => {
deleted.push({
feature : f,
layer : this._select.getLayer(f)
});
});
// Delete features
this.deleteSelection();
// To notify others
if (deleted.length !== 0) {
this.dispatchEvent({
type : "delete",
deleted : deleted
});
}
}
if ((evt.key === "c" || evt.key === "x") && (evt.ctrlKey || evt.metaKey)) {
// Get Features to copy/cut
copyFeatures = [];
this._select.getFeatures().forEach( f => {
// Restore style for clonning
this._select.restorePreviousStyle_(f);
copyFeatures.push({
feature : f.clone(),
layer : this._select.getLayer(f)
});
// Reapply selected style
this._select.applySelectedStyle_(f);
});
// Clear selection
if (evt.key === "x") {
// Delete features
this.deleteSelection();
}
// Dispatch copy/cut event
if (copyFeatures.length !== 0) {
this.dispatchEvent({
type : (evt.key === "x") ? "cut" : "copy",
features : copyFeatures,
});
}
}
if (evt.key === "v" && (evt.ctrlKey || evt.metaKey) && copyFeatures.length !== 0) {
this.dispatchEvent({
type : "paste",
features : copyFeatures
});
}
};
}
/** Chek if a feature is selected at pixel
* @param {ol.Pixel} pixel Pixel to check
* @return {Array<Feature>|Boolean} Found feature or false
*/
selectedAtPixel (pixel) {
const features = [];
this.getMap()?.forEachFeatureAtPixel(
pixel,
(feature) => {
if (this._select.getLayer(feature)) {
features.push(feature);
}
},
{
layerFilter : this._select.layerFilter_,
hitTolerance : this._select.hitTolerance_,
},
);
return features.length ? features : false;
}
/**
* Overwrite OpenLayers setMap method
*
* @param {Map} map - Map.
*/
setMap (map) {
if (this.getMap()) {
this.getMap().removeOverlay(this._menu);
this.getMap().removeInteraction(this.longtouch);
}
super.setMap(map);
if (map) {
this.getMap().addOverlay(this._menu);
this.getMap().getTargetElement().setAttribute("tabindex", "0");
this.getMap().getTargetElement().addEventListener("keydown", this._onKey);
this.getMap().addInteraction(this.longtouch);
}
this._menu.hide();
}
/** Delete selected features */
deleteSelection () {
this._select.getFeatures().forEach(f => {
const layer = this._select.getLayer(f);
if (layer) {
const source = layer.getSource();
source.removeFeature(f);
}
});
this._select.clear();
}
/**
* Overwrite OpenLayers setActive method / hide menu
*
* @param {Boolean} active Active state
*/
setActive (active) {
super.setActive(active);
if (this._menu) {
this._menu.hide();
}
}
/** Overwrite OpenLayers Handle event on the map
* @param {ol.MapBrowserEvent} e Event
* @returns {Boolean} Whether to propagate the event further
*/
handleEvent (e) {
// On a point && adding point
let isPoint = this._isOnPoint(e);
const isAdd = !!this.vertexFeature_;
// Handle parent events
const resp = super.handleEvent(e, isPoint);
// Handle context menu
if (this._islongtouch) {
if (e.type === "singleclick") {
setTimeout(() => {
this._islongtouch = false;
});
return;
} else if (e.type === "click" || e.type === "pointerup") {
return true;
}
}
if (e.type === "contextmenu") {
e.originalEvent.preventDefault();
} else if (e.type === "pointerup" && e.originalEvent.button === 2) {
e.originalEvent.preventDefault();
this._showContextMenu(e, isPoint);
}
// Dragging feature : show crosshair cursor
if (e.type==="pointerdrag" && isAdd) {
e.map.getTargetElement().style.cursor = "crosshair";
return resp;
}
// Handle hover effect
if (isPoint) {
if (e.originalEvent.altKey) {
this.getMap().getTargetElement().style.cursor = "alias";
} else {
this.getMap().getTargetElement().style.cursor = "pointer";
}
} else if (isAdd) {
this.getMap().getTargetElement().style.cursor = "copy";
} else {
this.getMap().getTargetElement().style.cursor = "";
}
return resp;
}
_showContextMenu (e, isPoint) {
const currentFeatures = this.selectedAtPixel(e.pixel);
// TODO : Check if can remove point
let canRemove = false;
if (isPoint) {
const position = isPoint.getGeometry().getCoordinates();
isPoint = false;
(currentFeatures || []).forEach(f => {
// Check if current point is a vertex
const getFlatCoordinates = f.getGeometry().getFlatCoordinates();
for (let i=0; i<getFlatCoordinates.length; i+=2) {
if (getFlatCoordinates[i] === position[0] && getFlatCoordinates[i+1] === position[1]) {
isPoint = true;
break;
}
};
// Check if can remove vertex
switch (f.getGeometry().getType()) {
case "LineString": {
if (f.getGeometry().getCoordinates().length > 2 ) {
canRemove = true;
}
break;
}
case "Polygon": {
const geom = f.getGeometry().getCoordinates();
geom.forEach(ring => {
if (ring.length > 4 ) {
canRemove = true;
}
});
break;
}
case "MultiLineString":
case "MultiPolygon": {
canRemove = true;
console.log("todo");
}
default: {
break;
}
}
});
}
// Show menu
if (isPoint && canRemove) {
this._menu.setMenu([
{
text : "Supprimer le point",
className : "fr-icon-delete-line",
callback : () => {
this.removePoint(e.coordinate);
},
hide : true
},
]);
this._menu.show(e.coordinate);
if (e.stopPropagation) {
e.stopPropagation();
}
} else if (this._select && currentFeatures) {
this._menu.setMenu([
{
text : "Dupliquer",
className : "fr-icon-file-copy-line",
callback : () => {
const features = this._select.getFeatures().getArray().slice();
const sel = [];
const tab = [];
features.forEach(f => {
const layer = this._select.getLayer(f);
if (layer) {
// Restore style before cloning
this._select.restorePreviousStyle_(f);
const f2 = f.clone();
// Reapply selected style
this._select.applySelectedStyle_(f);
// Add cloned feature
layer.getSource().addFeature(f2);
sel.push(f2);
tab.push({
feature : f2,
layer : layer
});
} else {
sel.push(f);
}
});
this._select.clear();
sel.forEach(f => this._select.getFeatures().push(f));
if (tab.length) {
this.dispatchEvent({
type : "duplicate",
features : tab
});
}
},
hide : true
},{
text : "Supprimer",
className : "fr-icon-delete-line",
callback : () => {
const deleted = [];
this._select.getFeatures().forEach( f => {
deleted.push({
feature : f,
layer : this._select.getLayer(f)
});
});
this.deleteSelection();
if (deleted.length !== 0) {
this.dispatchEvent({
type : "delete",
deleted : deleted
});
}
},
hide : true
},
]);
this._menu.show(e.coordinate);
if (e.stopPropagation) {
e.stopPropagation();
}
} else {
this._menu.hide();
}
}
_isOnPoint (e) {
// 3 pixels tolerance
const res = this.get("hitTolerance") * this.getMap().getView().getResolution();
const extent = [e.coordinate[0] - res, e.coordinate[1] - res, e.coordinate[0] + res, e.coordinate[1] + res];
// On a point && adding point
return this.getOverlay().getSource().getFeaturesInExtent(extent)[0];
}
}
export default ModifyingInteraction;
// Expose ModifyingInteraction as ol.interaction.Modifying (for a build bundle)
if (window.ol && window.ol.interaction) {
window.ol.interaction.Modifying = ModifyingInteraction;
}