-
Notifications
You must be signed in to change notification settings - Fork 166
/
extend-line-string-mode.ts
87 lines (73 loc) · 2.75 KB
/
extend-line-string-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
import { Position, LineString, FeatureCollection } from '../geojson-types';
import { ClickEvent, PointerMoveEvent, ModeProps, GuideFeatureCollection } from '../types';
import { GeoJsonEditMode } from './geojson-edit-mode';
import { ImmutableFeatureCollection } from './immutable-feature-collection';
export class ExtendLineStringMode extends GeoJsonEditMode {
getSingleSelectedLineString(props: ModeProps<FeatureCollection>): LineString | null | undefined {
const selectedGeometry = this.getSelectedGeometry(props);
if (selectedGeometry && selectedGeometry.type === 'LineString') {
return selectedGeometry;
}
return null;
}
handleClick(event: ClickEvent, props: ModeProps<FeatureCollection>) {
const { selectedIndexes } = props;
const selectedLineString = this.getSingleSelectedLineString(props);
if (!selectedLineString) {
console.warn(`ExtendLineStringMode only supported for single LineString selection`); // eslint-disable-line
return;
}
// Extend the LineString
let positionIndexes = [selectedLineString.coordinates.length];
const modeConfig = props.modeConfig;
if (modeConfig && modeConfig.drawAtFront) {
positionIndexes = [0];
}
const featureIndex = selectedIndexes[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,
},
});
}
getGuides(props: ModeProps<FeatureCollection>): GuideFeatureCollection {
const guides: GuideFeatureCollection = {
type: 'FeatureCollection',
features: [],
};
const selectedLineString = this.getSingleSelectedLineString(props);
if (!selectedLineString) {
return guides;
}
const mapCoords = props.lastPointerMoveEvent && props.lastPointerMoveEvent.mapCoords;
// Draw an extension line starting from one end of the selected LineString to the cursor
let startPosition: Position | null | undefined = null;
const { modeConfig } = props;
if (modeConfig && modeConfig.drawAtFront) {
startPosition = selectedLineString.coordinates[0];
} else {
startPosition = selectedLineString.coordinates[selectedLineString.coordinates.length - 1];
}
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');
}
}