-
Notifications
You must be signed in to change notification settings - Fork 166
/
draw-line-string-mode.js
112 lines (93 loc) · 3.53 KB
/
draw-line-string-mode.js
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
// @flow
import type { Position, LineString, FeatureCollection } from '../geojson-types.js';
import type { ClickEvent, PointerMoveEvent, ModeProps, GuideFeatureCollection } from '../types.js';
import { GeoJsonEditMode } from './geojson-edit-mode.js';
import { ImmutableFeatureCollection } from './immutable-feature-collection.js';
export class DrawLineStringMode extends GeoJsonEditMode {
handleClick(event: ClickEvent, props: ModeProps<FeatureCollection>) {
const selectedFeatureIndexes = props.selectedIndexes;
const selectedGeometry = this.getSelectedGeometry(props);
if (
selectedFeatureIndexes.length > 1 ||
(selectedGeometry && selectedGeometry.type !== 'LineString')
) {
console.warn(`drawLineString mode only supported for single LineString selection`); // eslint-disable-line
return;
}
this.addClickSequence(event);
const tentativeFeature = this.getTentativeGuide(props);
const clickSequence = this.getClickSequence();
if (selectedGeometry && selectedGeometry.type === 'LineString') {
// Extend the LineString
const lineString: LineString = selectedGeometry;
let positionIndexes = [lineString.coordinates.length];
const modeConfig = props.modeConfig;
if (modeConfig && modeConfig.drawAtFront) {
positionIndexes = [0];
}
const featureIndex = selectedFeatureIndexes[0];
const updatedData = new ImmutableFeatureCollection(props.data)
.addPosition(featureIndex, positionIndexes, event.mapCoords)
.getObject();
props.onEdit({
updatedData,
editType: 'addPosition',
editContext: {
featureIndexes: [featureIndex],
positionIndexes,
position: event.mapCoords
}
});
this.resetClickSequence();
} else if (clickSequence.length === 2 && tentativeFeature) {
// Add a new LineString
const { geometry } = tentativeFeature;
props.onEdit(this.getAddFeatureAction(geometry, props.data));
this.resetClickSequence();
}
}
getGuides(props: ModeProps<FeatureCollection>): GuideFeatureCollection {
const guides = {
type: 'FeatureCollection',
features: []
};
const clickSequence = this.getClickSequence();
const mapCoords = props.lastPointerMoveEvent && props.lastPointerMoveEvent.mapCoords;
let startPosition: ?Position = null;
const selectedFeatureIndexes = props.selectedIndexes;
const selectedGeometry = this.getSelectedGeometry(props);
if (
selectedFeatureIndexes.length > 1 ||
(selectedGeometry && selectedGeometry.type !== 'LineString')
) {
// unsupported
return guides;
}
if (selectedGeometry && selectedGeometry.type === 'LineString') {
// Draw an extension line starting from one end of the selected LineString
startPosition = selectedGeometry.coordinates[selectedGeometry.coordinates.length - 1];
const modeConfig = props.modeConfig;
if (modeConfig && modeConfig.drawAtFront) {
startPosition = selectedGeometry.coordinates[0];
}
} else if (clickSequence.length > 0) {
startPosition = clickSequence[0];
}
if (startPosition) {
guides.features.push({
type: 'Feature',
properties: {
guideType: 'tentative'
},
geometry: {
type: 'LineString',
coordinates: [startPosition, mapCoords]
}
});
}
return guides;
}
handlePointerMove(event: PointerMoveEvent, props: ModeProps<FeatureCollection>) {
props.onUpdateCursor('cell');
}
}