mapbox-gl-draw-passing-mode
Version:
passing drawing mode for mapbox-gl-draw
61 lines (50 loc) • 1.66 kB
JavaScript
import * as MapboxDraw from "@mapbox/mapbox-gl-draw";
const { draw_line_string } = MapboxDraw.modes;
const { doubleClickZoom } = MapboxDraw.lib;
const Constants = MapboxDraw.constants;
const {
onSetup: originOnSetup,
onMouseMove: originOnMouseMove,
...restOriginMethods
} = draw_line_string;
const passing_draw_line_string = {
originOnSetup,
originOnMouseMove,
...restOriginMethods,
};
passing_draw_line_string.onSetup = function (opt) {
const state = this.originOnSetup();
const { onDraw, onCancel } = opt;
state.onDraw = onDraw;
state.onCancel = onCancel;
return state;
};
passing_draw_line_string.onMouseMove = function (state, e) {
this.updateUIClasses({ mouse: Constants.cursors.ADD });
this.originOnMouseMove(state, e);
};
passing_draw_line_string.onStop = function (state) {
const f = state.line;
this.updateUIClasses({ mouse: Constants.cursors.NONE });
doubleClickZoom.enable(this);
this.activateUIButton();
/// check to see if we've deleted this feature
const drawnFeature = this.getFeature(f.id);
if (drawnFeature === undefined) {
/// Call `onCancel` if exists.
if (typeof state.onCancel === "function") state.onCancel();
return;
}
/// remove last added coordinate
else f.removeCoordinate(`${state.currentVertexPosition}`);
if (f.isValid()) {
if (typeof state.onDraw === "function") state.onDraw(f.toGeoJSON());
else
this.map.fire("draw.passing-create", {
features: [f.toGeoJSON()],
});
}
this.deleteFeature([f.id], { silent: true });
this.changeMode(Constants.modes.SIMPLE_SELECT, {}, { silent: true });
};
export default passing_draw_line_string;