Skip to content

Commit ae7a731

Browse files
committed
feat: add DrawLineStringByDraggingMode
1 parent b316c63 commit ae7a731

File tree

7 files changed

+88
-0
lines changed

7 files changed

+88
-0
lines changed

docs/api-reference/modes/overview.md

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ User can draw a new `Polygon` feature with 90 degree corners (right angle) by cl
113113

114114
User can draw a new `Polygon` feature by dragging (similar to the lasso tool commonly found in photo editing software).
115115

116+
## [DrawLineStringByDraggingMode](https://github.com/uber/nebula.gl/blob/master/modules/edit-modes/src/lib/draw-line-string-by-dragging-mode.ts)
117+
118+
User can draw a new `LineString` feature by dragging (similar to the pencil tool commonly found in sketching software).
119+
116120
### ModeConfig
117121

118122
The following options can be provided in the `modeConfig` object:

examples/advanced/src/example.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
DrawRectangleUsingThreePointsMode,
3535
Draw90DegreePolygonMode,
3636
DrawPolygonByDraggingMode,
37+
DrawLineStringByDraggingMode,
3738
MeasureDistanceMode,
3839
MeasureAreaMode,
3940
MeasureAngleMode,
@@ -105,6 +106,7 @@ const ALL_MODES: any = [
105106
{ label: 'Draw Polygon', mode: DrawPolygonMode },
106107
{ label: 'Draw 90° Polygon', mode: Draw90DegreePolygonMode },
107108
{ label: 'Draw Polygon By Dragging', mode: DrawPolygonByDraggingMode },
109+
{ label: 'Draw LineString By Dragging', mode: DrawLineStringByDraggingMode },
108110
{ label: 'Draw Rectangle', mode: DrawRectangleMode },
109111
{ label: 'Draw Rectangle From Center', mode: DrawRectangleFromCenterMode },
110112
{ label: 'Draw Rectangle Using 3 Points', mode: DrawRectangleUsingThreePointsMode },
@@ -966,6 +968,11 @@ export default class Example extends React.Component<
966968
...modeConfig,
967969
throttleMs: 100,
968970
};
971+
} else if (mode === DrawLineStringByDraggingMode) {
972+
modeConfig = {
973+
...modeConfig,
974+
throttleMs: 100,
975+
};
969976
}
970977

971978
// Demonstrate how to override sub layer properties

modules/edit-modes/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export { DrawEllipseUsingThreePointsMode } from './lib/draw-ellipse-using-three-
3636
export { DrawRectangleUsingThreePointsMode } from './lib/draw-rectangle-using-three-points-mode';
3737
export { Draw90DegreePolygonMode } from './lib/draw-90degree-polygon-mode';
3838
export { DrawPolygonByDraggingMode } from './lib/draw-polygon-by-dragging-mode';
39+
export { DrawLineStringByDraggingMode } from './lib/draw-line-string-by-dragging-mode';
3940
export { ImmutableFeatureCollection } from './lib/immutable-feature-collection';
4041

4142
// Other modes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import throttle from 'lodash.throttle';
2+
import { DebouncedFunc } from 'lodash';
3+
import {
4+
ClickEvent,
5+
StartDraggingEvent,
6+
StopDraggingEvent,
7+
DraggingEvent,
8+
ModeProps,
9+
} from '../types';
10+
import { LineString, FeatureCollection } from '../geojson-types';
11+
import { getPickedEditHandle } from '../utils';
12+
import { DrawLineStringMode } from './draw-line-string-mode';
13+
14+
type DragHandler = (event: DraggingEvent, props: ModeProps<FeatureCollection>) => void;
15+
16+
type ThrottledDragHandler = DebouncedFunc<DragHandler>;
17+
18+
export class DrawLineStringByDraggingMode extends DrawLineStringMode {
19+
handleDraggingThrottled: ThrottledDragHandler | DragHandler | null | undefined = null;
20+
21+
// Override the default behavior of DrawLineStringMode to not add a point when the user clicks on the map
22+
handleClick(event: ClickEvent, props: ModeProps<FeatureCollection>) {
23+
return;
24+
}
25+
26+
handleStartDragging(event: StartDraggingEvent, props: ModeProps<FeatureCollection>) {
27+
event.cancelPan();
28+
if (props.modeConfig && props.modeConfig.throttleMs) {
29+
this.handleDraggingThrottled = throttle(this.handleDraggingAux, props.modeConfig.throttleMs);
30+
} else {
31+
this.handleDraggingThrottled = this.handleDraggingAux;
32+
}
33+
}
34+
35+
handleStopDragging(event: StopDraggingEvent, props: ModeProps<FeatureCollection>) {
36+
this.addClickSequence(event);
37+
const clickSequence = this.getClickSequence();
38+
if (this.handleDraggingThrottled && 'cancel' in this.handleDraggingThrottled) {
39+
this.handleDraggingThrottled.cancel();
40+
}
41+
42+
if (clickSequence.length > 2) {
43+
const lineStringToAdd: LineString = {
44+
type: 'LineString',
45+
coordinates: clickSequence,
46+
};
47+
48+
this.resetClickSequence();
49+
50+
const editAction = this.getAddFeatureAction(lineStringToAdd, props.data);
51+
if (editAction) {
52+
props.onEdit(editAction);
53+
}
54+
}
55+
}
56+
57+
handleDraggingAux(event: DraggingEvent, props: ModeProps<FeatureCollection>) {
58+
const { picks } = event;
59+
const clickedEditHandle = getPickedEditHandle(picks);
60+
61+
if (!clickedEditHandle) {
62+
// Don't add another point right next to an existing one.
63+
this.addClickSequence(event);
64+
}
65+
}
66+
67+
handleDragging(event: DraggingEvent, props: ModeProps<FeatureCollection>) {
68+
if (this.handleDraggingThrottled) {
69+
this.handleDraggingThrottled(event, props);
70+
}
71+
}
72+
}

modules/layers/src/layers/editable-geojson-layer.ts

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
DrawEllipseUsingThreePointsMode,
2727
Draw90DegreePolygonMode,
2828
DrawPolygonByDraggingMode,
29+
DrawLineStringByDraggingMode,
2930
SnappableMode,
3031
TransformMode,
3132
EditAction,
@@ -260,6 +261,7 @@ const modeNameMapping = {
260261
drawEllipseUsing3Points: DrawEllipseUsingThreePointsMode,
261262
draw90DegreePolygon: Draw90DegreePolygonMode,
262263
drawPolygonByDragging: DrawPolygonByDraggingMode,
264+
drawLineStringByDragging: DrawLineStringByDraggingMode,
263265
};
264266

265267
// type State = {

modules/main/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export { DrawEllipseUsingThreePointsMode } from '@nebula.gl/edit-modes';
5757
export { DrawRectangleUsingThreePointsMode } from '@nebula.gl/edit-modes';
5858
export { Draw90DegreePolygonMode } from '@nebula.gl/edit-modes';
5959
export { DrawPolygonByDraggingMode } from '@nebula.gl/edit-modes';
60+
export { DrawLineStringByDraggingMode } from '@nebula.gl/edit-modes';
6061
export { ImmutableFeatureCollection } from '@nebula.gl/edit-modes';
6162

6263
// Other modes

modules/react-map-gl-draw/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export {
1414
DrawPolygonMode,
1515
DrawRectangleMode,
1616
DrawPolygonByDraggingMode,
17+
DrawLineStringByDraggingMode,
1718
MeasureDistanceMode,
1819
MeasureAreaMode,
1920
MeasureAngleMode,

0 commit comments

Comments
 (0)