-
Notifications
You must be signed in to change notification settings - Fork 56
/
DigitizeButton.tsx
1162 lines (1041 loc) · 30 KB
/
DigitizeButton.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as React from 'react';
import { Modal, Input } from 'antd';
const TextArea = Input.TextArea;
import OlMap from 'ol/Map';
import OlLayerVector from 'ol/layer/Vector';
import OlSourceVector from 'ol/source/Vector';
import OlCollection from 'ol/Collection';
import OlStyleStyle from 'ol/style/Style';
import OlStyleStroke from 'ol/style/Stroke';
import OlStyleFill from 'ol/style/Fill';
import OlStyleCircle from 'ol/style/Circle';
import OlStyleText from 'ol/style/Text';
import OlInteraction from 'ol/interaction/Interaction';
import OlInteractionDraw, { createBox } from 'ol/interaction/Draw';
import OlInteractionSelect from 'ol/interaction/Select';
import OlInteractionModify from 'ol/interaction/Modify';
import OlInteractionTranslate from 'ol/interaction/Translate';
import OlFeature from 'ol/Feature';
import { never, singleClick } from 'ol/events/condition';
const _isFunction = require('lodash/isFunction');
import ToggleButton from '../ToggleButton/ToggleButton';
import MapUtil from '@terrestris/ol-util/dist/MapUtil/MapUtil';
import StringUtil from '@terrestris/base-util/dist/StringUtil/StringUtil';
import AnimateUtil from '@terrestris/ol-util/dist/AnimateUtil/AnimateUtil';
import Logger from '@terrestris/base-util/dist/Logger';
import { CSS_PREFIX } from '../../constants';
interface DigitizeButtonDefaultProps {
/**
* Name of system vector layer which will be used for digitize features.
*/
digitizeLayerName: string;
/**
* Fill color of selected digitize feature.
*/
selectFillColor: string;
/**
* Stroke color of selected digitize feature.
*/
selectStrokeColor: string;
/**
* Title for modal used for input of labels for digitize features.
*/
modalPromptTitle: string;
/**
* Text string for `OK` button of the modal.
*/
modalPromptOkButtonText: string;
/**
* Text string for `Cancel` button of the modal.
*/
modalPromptCancelButtonText: string;
/**
* Additional configuration object to apply to the ol.interaction.Draw.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Draw-Draw.html
* for more information
*
* Note: The keys source, type, geometryFunction, style and freehandCondition
* are handled internally and shouldn't be overwritten without any
* specific cause.
*/
drawInteractionConfig: any;
/**
* Additional configuration object to apply to the ol.interaction.Select.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Select-Select.html
* for more information
*
* Note: The keys condition, hitTolerance and style are handled internally
* and shouldn't be overwritten without any specific cause.
*/
selectInteractionConfig: any;
/**
* Additional configuration object to apply to the ol.interaction.Modify.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Modify-Modify.html
* for more information
*
* Note: The keys features, deleteCondition and style are handled internally
* and shouldn't be overwritten without any specific cause.
*/
modifyInteractionConfig: any;
/**
* Additional configuration object to apply to the ol.interaction.Translate.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Translate-Translate.html
* for more information
*
* Note: The key feature is handled internally and shouldn't be overwritten
* without any specific cause.
*/
translateInteractionConfig: any;
/**
* A custom onToogle function that will be called if button is toggled.
*/
onToggle?: (event: any) => void;
}
type DrawType = 'Point' | 'LineString' | 'Polygon' | 'Circle' | 'Rectangle' | 'Text';
export interface DigitizeButtonProps extends Partial<DigitizeButtonDefaultProps> {
/**
* The className which should be added.
*/
className?: string;
/**
* Instance of OL map this component is bound to.
*/
map: OlMap;
/**
* Whether the line, point, polygon, circle, rectangle or text shape should
* be drawn.
*/
drawType?: DrawType;
/**
* Whether the digitize feature should be deleted, copied or modified.
* be drawn.
*/
editType?: 'Copy' | 'Edit' | 'Delete';
/**
* Style object / style function for drawn feature.
*/
drawStyle?: OlStyleStyle | ((feature: OlFeature) => OlStyleStyle);
/**
* Listener function for the 'drawend' event of an ol.interaction.Draw.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Draw-DrawEvent.html
* for more information.
*/
onDrawEnd?: (event: any) => void;
/**
* Listener function for the 'drawstart' event of an ol.interaction.Draw.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Draw-DrawEvent.html
* for more information.
*/
onDrawStart?: (event: any) => void;
/**
* Listener function for the 'modifystart' event of an ol.interaction.Modify.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Modify-ModifyEvent.html
* for more information.
*/
onModifyStart?: (event: any) => void;
/**
* Listener function for the 'modifyend' event of an ol.interaction.Modify.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Modify-ModifyEvent.html
* for more information.
*/
onModifyEnd?: (event: any) => void;
/**
* Listener function for the 'translatestart' event of an ol.interaction.Translate.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Translate-TranslateEvent.html
* for more information.
*/
onTranslateStart?: (event: any) => void;
/**
* Listener function for the 'translateend' event of an ol.interaction.Translate.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Translate-TranslateEvent.html
* for more information.
*/
onTranslateEnd?: (event: any) => void;
/**
* Listener function for the 'translating' event of an ol.interaction.Translate.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Translate-TranslateEvent.html
* for more information.
*/
onTranslating?: (event: any) => void;
/**
* Listener function for the 'select' event of the ol.interaction.Select
* if in `Delete` mode.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Select-SelectEvent.html
* for more information.
*/
onFeatureRemove?: (event: any) => void;
/**
* Listener function for the 'select' event of the ol.interaction.Select
* if in `Copy` mode.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Select-SelectEvent.html
* for more information.
*/
onFeatureCopy?: (event: any) => void;
/**
* Callback function that will be called when the ok-button of the modal was clicked
*/
onModalLabelOk?: (event: any) => void;
/**
* Callback function that will be called
* when the cancel-button of the modal was clicked
*/
onModalLabelCancel?: (event: any) => void;
/**
* Listener function for the 'select' event of the ol.interaction.Select
* if in `Edit` mode.
* Can be also called inside of 'select' listener function of
* the ol.interaction.Select in `Copy` and `Delete` mode if provided.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Select.html
* for more information.
*/
onFeatureSelect?: (event: any) => void;
/**
* Maximal length of feature label.
* If exceeded label will be divided into multiple lines. Optional.
*/
maxLabelLineLength?: number;
}
interface DigitizeButtonState {
digitizeLayer: OlLayerVector;
interactions: OlInteraction[];
showLabelPrompt: boolean;
textLabel: string;
}
/**
* The DigitizeButton.
*
* @class The DigitizeButton
* @extends React.Component
*
*/
class DigitizeButton extends React.Component<DigitizeButtonProps, DigitizeButtonState> {
/**
* The className added to this component.
*
* @type {String}
* @private
*/
className = `${CSS_PREFIX}digitizebutton`;
/**
* Currently existing digitize features as collection.
*
* @type {OlCollection}
* @private
*/
_digitizeFeatures = null;
/**
* Currently drawn feature which should be represent as label or postit.
* @type {OlFeature}
* @private
*/
_digitizeTextFeature = null;
/**
* Reference to OL select interaction which will be used in edit mode.
* @type {OlInteractionSelect}
* @private
*/
_selectInteraction = null;
/**
* Name of point draw type.
* @private
*/
static POINT_DRAW_TYPE = 'Point';
/**
* Name of line string draw type.
* @private
*/
static LINESTRING_DRAW_TYPE = 'LineString';
/**
* Name of polygon draw type.
* @private
*/
static POLYGON_DRAW_TYPE = 'Polygon';
/**
* Name of circle draw type.
* @private
*/
static CIRCLE_DRAW_TYPE = 'Circle';
/**
* Name of rectangle draw type.
* @private
*/
static RECTANGLE_DRAW_TYPE = 'Rectangle';
/**
* Name of text draw type.
* @private
*/
static TEXT_DRAW_TYPE = 'Text';
/**
* Name of copy edit type.
* @private
*/
static COPY_EDIT_TYPE = 'Copy';
/**
* Name of edit edit type.
* @private
*/
static EDIT_EDIT_TYPE = 'Edit';
/**
* Name of delete edit type.
* @private
*/
static DELETE_EDIT_TYPE = 'Delete';
/**
* Default fill color used in style object of drawn features.
*
* @type {String}
*/
static DEFAULT_FILL_COLOR = 'rgba(154, 26, 56, 0.5)';
/**
* Default stroke color used in style object of drawn features.
*
* @type {String}
*/
static DEFAULT_STROKE_COLOR = 'rgba(154, 26, 56, 0.8)';
/**
* Hit detection in pixels used for select interaction.
*
* @type {Number}
*/
static HIT_TOLERANCE = 5;
/**
* Default style for digitized points.
*
* @type {OlStyleStyle}
*/
static DEFAULT_POINT_STYLE = new OlStyleStyle({
image: new OlStyleCircle({
radius: 7,
fill: new OlStyleFill({
color: DigitizeButton.DEFAULT_FILL_COLOR
}),
stroke: new OlStyleStroke({
color: DigitizeButton.DEFAULT_STROKE_COLOR
})
})
});
/**
* Default style for digitized lines.
*
* @type {OlStyleStyle}
*/
static DEFAULT_LINESTRING_STYLE = new OlStyleStyle({
stroke: new OlStyleStroke({
color: DigitizeButton.DEFAULT_STROKE_COLOR,
width: 2
})
});
/**
* Default style for digitized polygons or circles.
*
* @type {OlStyleStyle}
*/
static DEFAULT_POLYGON_STYLE = new OlStyleStyle({
fill: new OlStyleFill({
color: DigitizeButton.DEFAULT_FILL_COLOR
}),
stroke: new OlStyleStroke({
color: DigitizeButton.DEFAULT_STROKE_COLOR,
width: 2
})
});
/**
* Default style for digitized labels.
*
* @type {OlStyleStyle}
*/
static DEFAULT_TEXT_STYLE = new OlStyleStyle({
text: new OlStyleText({
text: '',
offsetX: 5,
offsetY: 5,
font: '12px sans-serif',
fill: new OlStyleFill({
color: DigitizeButton.DEFAULT_FILL_COLOR
}),
stroke: new OlStyleStroke({
color: DigitizeButton.DEFAULT_STROKE_COLOR
})
})
});
/**
* The properties.
* @type {Object}
*/
static propTypes = {
};
/**
* The default properties.
* @type {Object}
*/
static defaultProps: DigitizeButtonDefaultProps = {
digitizeLayerName: 'react-geo_digitize',
selectFillColor: 'rgba(240, 240, 90, 0.5)',
selectStrokeColor: 'rgba(220, 120, 20, 0.8)',
modalPromptTitle: 'Label',
modalPromptOkButtonText: 'Ok',
modalPromptCancelButtonText: 'Cancel',
drawInteractionConfig: {},
selectInteractionConfig: {},
modifyInteractionConfig: {},
translateInteractionConfig: {},
onToggle: () => undefined
};
/**
* Creates the DigitizeButton.
*
* @constructs DigitizeButton
*/
constructor(props: DigitizeButtonProps) {
super(props);
if (!props.drawType && !props.editType) {
Logger.warn('Neither "drawType" nor "editType" was provided. Digitize ' +
'button won\'t work properly!');
}
this.state = {
digitizeLayer: null,
interactions: [],
showLabelPrompt: false,
textLabel: ''
};
}
/**
* `componentDidMount` method of the DigitizeButton. Just calls
* `createDigitizeLayer` method.
*/
componentDidMount() {
this.createDigitizeLayer();
}
/**
* Called on componentWillUnmount lifecycle.
*/
componentWillUnmount() {
const {
map
} = this.props;
const {
interactions
} = this.state;
interactions.forEach(i => map.removeInteraction(i));
}
/**
* Called when the digitize button is toggled. If the button state is pressed,
* the appropriate draw, modify or select interaction will be created.
* Otherwise, by untoggling, the same previously created interaction will be
* removed from the map.
*
* @param {Boolean} pressed Whether the digitize button is pressed or not.
*/
onToggle = pressed => {
const {
map,
drawType,
editType,
drawStyle,
onFeatureSelect
} = this.props;
const {
digitizeLayer,
interactions
} = this.state;
this.props.onToggle(pressed);
this._digitizeFeatures = digitizeLayer.getSource().getFeaturesCollection();
if (pressed) {
if (drawStyle) {
digitizeLayer.setStyle(this.getDigitizeStyleFunction);
}
if (drawType) {
this.createDrawInteraction(pressed);
} else if (editType) {
this.createSelectOrModifyInteraction();
}
} else {
interactions.forEach(i => map.removeInteraction(i));
if (drawType === DigitizeButton.TEXT_DRAW_TYPE) {
this._digitizeFeatures.un('add', this.handleTextAdding);
} else {
if (this._selectInteraction) {
this._selectInteraction.getFeatures().clear();
}
if (editType === DigitizeButton.DELETE_EDIT_TYPE) {
this._selectInteraction.un('select', this.onFeatureRemove);
}
if (editType === DigitizeButton.COPY_EDIT_TYPE) {
this._selectInteraction.un('select', this.onFeatureCopy);
}
if (_isFunction(onFeatureSelect) && editType === DigitizeButton.EDIT_EDIT_TYPE) {
this._selectInteraction.un('select', onFeatureSelect);
}
map.un('pointermove', this.onPointerMove);
}
}
}
/**
* Creates digitize vector layer and adds this to the map.
*/
createDigitizeLayer = () => {
const {
digitizeLayerName,
map
} = this.props;
let digitizeLayer = MapUtil.getLayerByName(map, digitizeLayerName);
if (!digitizeLayer) {
digitizeLayer = new OlLayerVector({
name: digitizeLayerName,
source: new OlSourceVector({
features: new OlCollection(),
})
});
map.addLayer(digitizeLayer);
}
digitizeLayer.setStyle(this.getDigitizeStyleFunction);
this.setState({digitizeLayer});
}
/**
* The styling function for the digitize vector layer, which considers
* different geometry types of drawn features.
*
* @param {OlFeature} feature The feature which is being styled.
* @return {OlStyleStyle} The style to use.
*/
getDigitizeStyleFunction = feature => {
const {
drawStyle,
} = this.props;
let styleObj;
if (!feature.getGeometry()) {
return;
}
switch (feature.getGeometry().getType()) {
case DigitizeButton.POINT_DRAW_TYPE: {
if (!feature.get('isLabel')) {
styleObj = drawStyle || new OlStyleStyle({
image: new OlStyleCircle({
radius: 7,
fill: new OlStyleFill({
color: DigitizeButton.DEFAULT_FILL_COLOR
}),
stroke: new OlStyleStroke({
color: DigitizeButton.DEFAULT_STROKE_COLOR
})
})
});
} else {
styleObj = drawStyle || new OlStyleStyle({
text: new OlStyleText({
text: feature.get('label'),
offsetX: 5,
offsetY: 5,
font: '12px sans-serif',
fill: new OlStyleFill({
color: DigitizeButton.DEFAULT_FILL_COLOR
}),
stroke: new OlStyleStroke({
color: DigitizeButton.DEFAULT_STROKE_COLOR
})
})
});
}
return styleObj;
}
case DigitizeButton.LINESTRING_DRAW_TYPE: {
styleObj = drawStyle || new OlStyleStyle({
stroke: new OlStyleStroke({
color: DigitizeButton.DEFAULT_STROKE_COLOR,
width: 2
})
});
return styleObj;
}
case DigitizeButton.POLYGON_DRAW_TYPE:
case DigitizeButton.CIRCLE_DRAW_TYPE: {
styleObj = drawStyle || new OlStyleStyle({
fill: new OlStyleFill({
color: DigitizeButton.DEFAULT_FILL_COLOR
}),
stroke: new OlStyleStroke({
color: DigitizeButton.DEFAULT_STROKE_COLOR,
width: 2
})
});
return styleObj;
}
default: {
break;
}
}
}
/**
* The OL style for selected digitized features.
*
* @param {OlFeature} feature The selected feature.
* @param {Number} View resolution.
* @param {String} text Text for labeled feature (optional).
* @return {OlStyleStyle} The style to use.
*/
getSelectedStyleFunction = (feature, res, text) => {
const {
selectFillColor,
selectStrokeColor
} = this.props;
if (feature.get('label')) {
text = feature.get('label');
}
return new OlStyleStyle({
image: new OlStyleCircle({
radius: 7,
fill: new OlStyleFill({
color: selectFillColor
}),
stroke: new OlStyleStroke({
color: selectStrokeColor
})
}),
text: new OlStyleText({
text: text ? text : '',
offsetX: 5,
offsetY: 5,
font: '12px sans-serif',
fill: new OlStyleFill({
color: selectFillColor
}),
stroke: new OlStyleStroke({
color: selectStrokeColor
})
}),
stroke: new OlStyleStroke({
color: selectStrokeColor,
width: 2
}),
fill: new OlStyleFill({
color: selectFillColor
})
});
}
/**
* Creates a correctly configured OL draw interaction depending on given
* drawType and adds this to the map.
*
* @param {Boolean} pressed Whether the digitize button is pressed or not.
* Will be used to handle active state of the draw interaction.
*/
createDrawInteraction = pressed => {
const {
drawType,
map,
onDrawEnd,
onDrawStart,
digitizeLayerName,
drawInteractionConfig
} = this.props;
let geometryFunction;
let type = drawType;
// check whether the digitizeLayer is in map and set it from state if not
let digitizeLayer = MapUtil.getLayerByName(map, digitizeLayerName);
if (!digitizeLayer) {
map.addLayer(this.state.digitizeLayer);
}
if (drawType === DigitizeButton.RECTANGLE_DRAW_TYPE) {
geometryFunction = createBox();
type = DigitizeButton.CIRCLE_DRAW_TYPE as DrawType;
} else if (drawType === DigitizeButton.TEXT_DRAW_TYPE) {
type = DigitizeButton.POINT_DRAW_TYPE as DrawType;
this._digitizeFeatures.on('add', this.handleTextAdding);
}
const drawInteraction = new OlInteractionDraw({
source: this.state.digitizeLayer.getSource(),
type: type,
geometryFunction: geometryFunction,
style: this.getDigitizeStyleFunction,
freehandCondition: never,
...drawInteractionConfig
});
if (onDrawEnd) {
drawInteraction.on('drawend', onDrawEnd);
}
if (onDrawStart) {
drawInteraction.on('drawstart', onDrawStart);
}
map.addInteraction(drawInteraction);
this.setState({
interactions: [drawInteraction]
}, () => {
drawInteraction.setActive(pressed);
});
}
/**
* Creates a correctly configured OL select and/or modify and/or translate
* interaction(s) depending on given editType and adds this/these to the map.
*/
createSelectOrModifyInteraction = () => {
const {
editType,
map,
selectInteractionConfig,
modifyInteractionConfig,
translateInteractionConfig,
onFeatureSelect
} = this.props;
this._selectInteraction = new OlInteractionSelect({
condition: singleClick,
hitTolerance: DigitizeButton.HIT_TOLERANCE,
style: this.getSelectedStyleFunction,
...selectInteractionConfig
});
if (editType === DigitizeButton.DELETE_EDIT_TYPE) {
this._selectInteraction.on('select', this.onFeatureRemove);
} else if (editType === DigitizeButton.COPY_EDIT_TYPE) {
this._selectInteraction.on('select', this.onFeatureCopy);
}
if (_isFunction(onFeatureSelect) && editType === DigitizeButton.EDIT_EDIT_TYPE) {
this._selectInteraction.on('select', onFeatureSelect);
}
let interactions = [this._selectInteraction];
if (editType === DigitizeButton.EDIT_EDIT_TYPE) {
const edit = new OlInteractionModify({
features: this._selectInteraction.getFeatures(),
deleteCondition: singleClick,
style: this.getSelectedStyleFunction,
...modifyInteractionConfig
});
edit.on('modifystart', this.onModifyStart);
edit.on('modifyend', this.onModifyEnd);
const translate = new OlInteractionTranslate({
features: this._selectInteraction.getFeatures(),
...translateInteractionConfig
});
translate.on('translatestart', this.onTranslateStart);
translate.on('translateend', this.onTranslateEnd);
translate.on('translating', this.onTranslating);
interactions.push(edit, translate);
}
interactions.forEach(i => map.addInteraction(i));
map.on('pointermove', this.onPointerMove);
this.setState({interactions});
}
/**
* Listener for `select` event of OL select interaction in `Delete` mode.
* Removes selected feature from the vector source and map.
*
* @param {ol.interaction.Select.Event} evt The interaction event.
*/
onFeatureRemove = evt => {
const {
onFeatureRemove,
onFeatureSelect
} = this.props;
if (_isFunction(onFeatureRemove)) {
onFeatureRemove(evt);
}
if (_isFunction(onFeatureSelect)) {
onFeatureSelect(evt);
}
const feat = evt.selected[0];
this._selectInteraction.getFeatures().remove(feat);
this.state.digitizeLayer.getSource().removeFeature(feat);
this.props.map.renderSync();
}
/**
* Listener for `select` event of OL select interaction in `Copy` mode.
* Creates a clone of selected feature and calls utility method to move it
* beside the original to avoid overlapping.
*
* @param {ol.interaction.Select.Event} evt The interaction event.
*/
onFeatureCopy = evt => {
const {
map,
onFeatureCopy,
onFeatureSelect
} = this.props;
const {
digitizeLayer
} = this.state;
const feat = evt.selected[0];
if (!feat) {
return;
}
if (_isFunction(onFeatureCopy)) {
onFeatureCopy(evt);
}
if (_isFunction(onFeatureSelect)) {
onFeatureSelect(evt);
}
const copy = feat.clone();
copy.setStyle(this.getDigitizeStyleFunction(feat));
this._digitizeFeatures.push(copy);
AnimateUtil.moveFeature(
map,
digitizeLayer,
copy,
500,
50,
this.getDigitizeStyleFunction(feat)
);
}
/**
* Checks if a labeled feature is being modified. If yes, opens prompt to
* input a new label.
*
* @param {ol.interaction.Modify.Event} evt The interaction event.
*/
onModifyStart = evt => {
const {
onModifyStart
} = this.props;
if (_isFunction(onModifyStart)) {
onModifyStart(evt);
}
const feature = evt.features.getArray()[0];
if (feature.get('isLabel')) {
this._digitizeTextFeature = feature;
let textLabel = '';
const featureStyle = this.getDigitizeStyleFunction(feature);
if (featureStyle && featureStyle.getText()) {
textLabel = featureStyle.getText().getText();
} else if (feature.get('label')) {
textLabel = feature.get('label');
}
this.setState({
showLabelPrompt: true,
textLabel
});
}
}
/**
* Called on modifyend of the ol.interaction.Modify.
*
* @param {ol.interaction.Modify.Event} evt The interaction event.
*/
onModifyEnd = evt => {
const {
onModifyEnd
} = this.props;
if (_isFunction(onModifyEnd)) {
onModifyEnd(evt);
}
}
/**
* Called on translatestart of the ol.interaction.Translate.
*
* @param {ol.interaction.Translate.Event} evt The interaction event.
*/
onTranslateStart = evt => {
const {
onTranslateStart
} = this.props;
if (_isFunction(onTranslateStart)) {
onTranslateStart(evt);
}
}
/**
* Called on translateend of the ol.interaction.Translate.
*
* @param {ol.interaction.Translate.Event} evt The interaction event.
*/
onTranslateEnd = evt => {
const {
onTranslateEnd
} = this.props;
if (_isFunction(onTranslateEnd)) {
onTranslateEnd(evt);
}
}
/**
* Called on translating of the ol.interaction.Translate.
*
* @param {ol.interaction.Translate.Event} evt The interaction event.
*/
onTranslating = evt => {
const {
onTranslating
} = this.props;
if (_isFunction(onTranslating)) {
onTranslating(evt);
}
}
/**
* Changes state for showLabelPrompt to make modal for label input visible.
*
* @param {Event} evt Click event on adding feature to the digitize layer.
*
* @method
*/
handleTextAdding = evt => {
this.setState({
showLabelPrompt: true
});
this._digitizeTextFeature = evt.element;
this._digitizeTextFeature.set('isLabel', true);
}
/**
* Callback function after `Ok` button of label input modal was clicked.
* Turns visibility of modal off and call `setTextOnFeature` method.
*/
onModalLabelOk = () => {
const {
onModalLabelOk
} = this.props;
this.setState({
showLabelPrompt: false
}, () => {
this.setTextOnFeature(this._digitizeTextFeature, onModalLabelOk);
});
}
/**
* Callback function after `Cancel` button of label input modal was clicked.
* Turns visibility of modal off and removes last drawn feature from the
* digitize layer.
*/
onModalLabelCancel = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
const {
onModalLabelCancel
} = this.props;