-
Notifications
You must be signed in to change notification settings - Fork 166
/
translate-mode.ts
160 lines (135 loc) · 4.73 KB
/
translate-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
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
import turfBearing from '@turf/bearing';
import turfDistance from '@turf/distance';
import clone from '@turf/clone';
import { point, Feature as TurfFeature, Geometry as TurfGeometry } from '@turf/helpers';
import WebMercatorViewport from 'viewport-mercator-project';
import { FeatureCollection, Position, Geometry } from '../geojson-types';
import {
PointerMoveEvent,
StartDraggingEvent,
StopDraggingEvent,
DraggingEvent,
ModeProps,
} from '../types';
import { mapCoords } from '../utils';
import { translateFromCenter } from '../translateFromCenter';
import { GeoJsonEditMode, GeoJsonEditAction } from './geojson-edit-mode';
import { ImmutableFeatureCollection } from './immutable-feature-collection';
export class TranslateMode extends GeoJsonEditMode {
_geometryBeforeTranslate: FeatureCollection | null | undefined;
_isTranslatable: boolean;
handleDragging(event: DraggingEvent, props: ModeProps<FeatureCollection>) {
if (!this._isTranslatable) {
// Nothing to do
return;
}
if (this._geometryBeforeTranslate) {
// Translate the geometry
const editAction = this.getTranslateAction(
event.pointerDownMapCoords,
event.mapCoords,
'translating',
props
);
if (editAction) {
props.onEdit(editAction);
}
}
// cancel map panning
event.cancelPan();
}
handlePointerMove(event: PointerMoveEvent, props: ModeProps<FeatureCollection>) {
this._isTranslatable = this.isSelectionPicked(event.pointerDownPicks || event.picks, props);
this.updateCursor(props);
}
handleStartDragging(event: StartDraggingEvent, props: ModeProps<FeatureCollection>) {
if (!this._isTranslatable) {
return;
}
this._geometryBeforeTranslate = this.getSelectedFeaturesAsFeatureCollection(props);
}
handleStopDragging(event: StopDraggingEvent, props: ModeProps<FeatureCollection>) {
if (this._geometryBeforeTranslate) {
// Translate the geometry
const editAction = this.getTranslateAction(
event.pointerDownMapCoords,
event.mapCoords,
'translated',
props
);
if (editAction) {
props.onEdit(editAction);
}
this._geometryBeforeTranslate = null;
}
}
updateCursor(props: ModeProps<FeatureCollection>) {
if (this._isTranslatable) {
props.onUpdateCursor('move');
} else {
props.onUpdateCursor(null);
}
}
getTranslateAction(
startDragPoint: Position,
currentPoint: Position,
editType: string,
props: ModeProps<FeatureCollection>
): GeoJsonEditAction | null | undefined {
if (!this._geometryBeforeTranslate) {
return null;
}
let updatedData = new ImmutableFeatureCollection(props.data);
const selectedIndexes = props.selectedIndexes;
const { viewport: viewportDesc, screenSpace } = props.modeConfig || {};
// move features without adapting to mercator projection
if (viewportDesc && screenSpace) {
const viewport = viewportDesc.project ? viewportDesc : new WebMercatorViewport(viewportDesc);
const from = viewport.project(startDragPoint);
const to = viewport.project(currentPoint);
const dx = to[0] - from[0];
const dy = to[1] - from[1];
for (let i = 0; i < selectedIndexes.length; i++) {
const selectedIndex = selectedIndexes[i];
const feature = this._geometryBeforeTranslate.features[i];
let coordinates = feature.geometry.coordinates;
if (coordinates) {
coordinates = mapCoords(coordinates, (coord) => {
const pixels = viewport.project(coord);
if (pixels) {
pixels[0] += dx;
pixels[1] += dy;
return viewport.unproject(pixels);
}
return null;
});
// @ts-expect-error turf types
updatedData = updatedData.replaceGeometry(selectedIndex, {
type: feature.geometry.type,
coordinates,
});
}
}
} else {
const p1 = point(startDragPoint);
const p2 = point(currentPoint);
const distanceMoved = turfDistance(p1, p2);
const direction = turfBearing(p1, p2);
const movedFeatures = this._geometryBeforeTranslate.features.map((feature) =>
translateFromCenter(clone(feature as TurfFeature<TurfGeometry>), distanceMoved, direction)
);
for (let i = 0; i < selectedIndexes.length; i++) {
const selectedIndex = selectedIndexes[i];
const movedFeature = movedFeatures[i];
updatedData = updatedData.replaceGeometry(selectedIndex, movedFeature.geometry as Geometry);
}
}
return {
updatedData: updatedData.getObject(),
editType,
editContext: {
featureIndexes: selectedIndexes,
},
};
}
}