-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathdraw-polygon-by-dragging-mode.ts
106 lines (94 loc) · 3.31 KB
/
draw-polygon-by-dragging-mode.ts
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
import throttle from 'lodash.throttle';
import {
ClickEvent,
StartDraggingEvent,
StopDraggingEvent,
DraggingEvent,
ModeProps,
} from '../types';
import { Polygon, FeatureCollection } from '../geojson-types';
import { getPickedEditHandle } from '../utils';
import { DrawPolygonMode } from './draw-polygon-mode';
type DraggingHandler = (event: DraggingEvent, props: ModeProps<FeatureCollection>) => void;
export class DrawPolygonByDraggingMode extends DrawPolygonMode {
handleDraggingThrottled: DraggingHandler | null | undefined = null;
handleClick(event: ClickEvent, props: ModeProps<FeatureCollection>) {
// No-op
}
handleStartDragging(event: StartDraggingEvent, props: ModeProps<FeatureCollection>) {
event.cancelPan();
if (props.modeConfig && props.modeConfig.throttleMs) {
this.handleDraggingThrottled = throttle(this.handleDraggingAux, props.modeConfig.throttleMs);
} else {
this.handleDraggingThrottled = this.handleDraggingAux;
}
}
handleStopDragging(event: StopDraggingEvent, props: ModeProps<FeatureCollection>) {
this.addClickSequence(event);
const clickSequence = this.getClickSequence();
// @ts-expect-error cancel() not typed
if (this.handleDraggingThrottled && this.handleDraggingThrottled.cancel) {
// @ts-expect-error cancel() not typed
this.handleDraggingThrottled.cancel();
}
if (clickSequence.length > 2) {
// Complete the polygon.
const polygonToAdd: Polygon = {
type: 'Polygon',
coordinates: [[...clickSequence, clickSequence[0]]],
};
const editAction = this.getAddFeatureOrBooleanPolygonAction(polygonToAdd, props);
if (editAction) {
props.onEdit(editAction);
}
}
this.resetClickSequence();
}
handleDraggingAux(event: DraggingEvent, props: ModeProps<FeatureCollection>) {
const { picks } = event;
const clickedEditHandle = getPickedEditHandle(picks);
if (!clickedEditHandle) {
// Don't add another point right next to an existing one.
this.addClickSequence(event);
props.onEdit({
updatedData: props.data,
editType: 'addTentativePosition',
editContext: {
position: event.mapCoords,
},
});
}
}
handleDragging(event: DraggingEvent, props: ModeProps<FeatureCollection>) {
if (this.handleDraggingThrottled) {
this.handleDraggingThrottled(event, props);
}
}
handleKeyUp(event: KeyboardEvent, props: ModeProps<FeatureCollection>) {
if (event.key === 'Enter') {
const clickSequence = this.getClickSequence();
if (clickSequence.length > 2) {
const polygonToAdd: Polygon = {
type: 'Polygon',
coordinates: [[...clickSequence, clickSequence[0]]],
};
this.resetClickSequence();
const editAction = this.getAddFeatureOrBooleanPolygonAction(polygonToAdd, props);
if (editAction) {
props.onEdit(editAction);
}
}
} else if (event.key === 'Escape') {
this.resetClickSequence();
if (this.handleDraggingThrottled) {
this.handleDraggingThrottled = null;
}
props.onEdit({
// Because the new drawing feature is dropped, so the data will keep as the same.
updatedData: props.data,
editType: 'cancelFeature',
editContext: {},
});
}
}
}