Skip to content

Commit

Permalink
Merge pull request #21 from tohu12/feature/add-snapGetFeatures
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsattarian authored Dec 31, 2024
2 parents 01df2b8 + 0816ee4 commit 94b852d
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 50 deletions.
8 changes: 6 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ const draw = new MapboxDraw({
snapPx: 15, // defaults to 15
snapToMidPoints: true, // defaults to false
snapVertexPriorityDistance: 0.0025, // defaults to 1.25
snapGetFeatures: (map, draw) => [
...map.queryRenderedFeatures({ layers: ["not-editable-layer-name"] }),
...draw.getAll().features,
], // defaults to all features from the draw layer (draw.getAll().features)
},
guides: false,
});
Expand All @@ -65,7 +69,7 @@ draw.changeMode("draw_polygon");

#### `snapPx`

The min distnace (in pixels) where snapping to the line/segments would take effect.
The min distance (in pixels) where snapping to the line/segments would take effect.

#### `snapToMidPoints`

Expand All @@ -81,7 +85,7 @@ Defaults to `true`. When creating polygons, if `false`, will use `turf.differenc

### Changing settings

Changing settings would take effect while snapping imidiately, so you can control snapping behaivior using `draw.options.snap`, like so:
Changing settings would take effect while snapping immediately, so you can control snapping behavior using `draw.options.snap`, like so:

```js
// turn snapping off
Expand Down
2 changes: 1 addition & 1 deletion dist/mapbox-gl-draw-snap-mode.cjs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mapbox-gl-draw-snap-mode.cjs.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mapbox-gl-draw-snap-mode.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mapbox-gl-draw-snap-mode.esm.js.map

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,13 @@
// snapOptions: {
// snapPx: 15,
// snapToMidPoints: true,
// snapVertexPriorityDistance: 0.0025
// snapVertexPriorityDistance: 0.0025,
// snapGetFeatures: (map, draw) => [
// ...map.queryRenderedFeatures({
// layers: ["not-editable-layer-name"],
// }),
// ...draw.getAll().features,
// ],
// },
guides: false,
});
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export { default as SnapPolygonMode } from "./modes/snap_polygon.js";
export { default as SnapDirectSelect } from "./modes/snap_direct_select.js";

export { default as SnapModeDrawStyles } from "./utils/customDrawStyles.js";
export * as Utils from "./utils/index.js"
export * as Utils from "./utils/index.js";
25 changes: 18 additions & 7 deletions src/modes/snap_direct_select.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import MapboxDraw from "@mapbox/mapbox-gl-draw";
import { createSnapList, getGuideFeature, IDS, snap } from "./../utils/index.js";
import {
createSnapList,
getGuideFeature,
IDS,
snap,
} from "./../utils/index.js";

const { doubleClickZoom } = MapboxDraw.lib;
const DirectSelect = MapboxDraw.modes.direct_select;
const Constants = MapboxDraw.constants;
const DirectSelect = MapboxDraw.modes.direct_select;
const SnapDirectSelect = { ...DirectSelect };

SnapDirectSelect.onSetup = function (opts) {
Expand All @@ -17,7 +23,12 @@ SnapDirectSelect.onSetup = function (opts) {
throw new TypeError("direct_select mode doesn't handle point features");
}

const [snapList, vertices] = createSnapList(this.map, this._ctx.api, feature);
const [snapList, vertices] = createSnapList(
this.map,
this._ctx.api,
feature,
this._ctx.options.snapOptions?.snapGetFeatures
);

const verticalGuide = this.newFeature(getGuideFeature(IDS.VERTICAL_GUIDE));
const horizontalGuide = this.newFeature(
Expand Down Expand Up @@ -53,13 +64,13 @@ SnapDirectSelect.onSetup = function (opts) {
trash: true,
});

const optionsChangedCallBAck = (options) => {
const optionsChangedCallback = (options) => {
state.options = options;
};

// for removing listener later on close
state["optionsChangedCallBAck"] = optionsChangedCallBAck;
this.map.on("draw.snap.options_changed", optionsChangedCallBAck);
state["optionsChangedCallback"] = optionsChangedCallback;
this.map.on("draw.snap.options_changed", optionsChangedCallback);

return state;
};
Expand All @@ -76,7 +87,7 @@ SnapDirectSelect.onStop = function (state) {

// remove moveend callback
// this.map.off("moveend", state.moveendCallback);
this.map.off("draw.snap.options_changed", state.optionsChangedCallBAck);
this.map.off("draw.snap.options_changed", state.optionsChangedCallback);

// This relies on the the state of SnapPolygonMode being similar to DrawPolygon
DirectSelect.onStop.call(this, state);
Expand Down
31 changes: 18 additions & 13 deletions src/modes/snap_line.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import MapboxDraw from "@mapbox/mapbox-gl-draw";
const {
geojsonTypes,
modes,
cursors,
} = MapboxDraw.constants;
const { doubleClickZoom } = MapboxDraw.lib;
const DrawLine = MapboxDraw.modes.draw_line_string;

import {
addPointToVertices,
createSnapList,
Expand All @@ -16,6 +8,9 @@ import {
snap,
} from "./../utils/index.js";

const { doubleClickZoom } = MapboxDraw.lib;
const { geojsonTypes, modes, cursors } = MapboxDraw.constants;
const DrawLine = MapboxDraw.modes.draw_line_string;
const SnapLineMode = { ...DrawLine };

SnapLineMode.onSetup = function (options) {
Expand All @@ -41,7 +36,12 @@ SnapLineMode.onSetup = function (options) {
this.clearSelectedFeatures();
doubleClickZoom.disable(this);

const [snapList, vertices] = createSnapList(this.map, this._ctx.api, line);
const [snapList, vertices] = createSnapList(
this.map,
this._ctx.api,
line,
this._ctx.options.snapOptions?.snapGetFeatures
);

const state = {
map: this.map,
Expand All @@ -58,21 +58,26 @@ SnapLineMode.onSetup = function (options) {
state.options = this._ctx.options;

const moveendCallback = () => {
const [snapList, vertices] = createSnapList(this.map, this._ctx.api, line);
const [snapList, vertices] = createSnapList(
this.map,
this._ctx.api,
line,
this._ctx.options.snapOptions?.snapGetFeatures
);
state.vertices = vertices;
state.snapList = snapList;
};
// for removing listener later on close
state["moveendCallback"] = moveendCallback;

const optionsChangedCallBAck = (options) => {
const optionsChangedCallback = (options) => {
state.options = options;
};
// for removing listener later on close
state["optionsChangedCallBAck"] = optionsChangedCallBAck;
state["optionsChangedCallback"] = optionsChangedCallback;

this.map.on("moveend", moveendCallback);
this.map.on("draw.snap.options_changed", optionsChangedCallBAck);
this.map.on("draw.snap.options_changed", optionsChangedCallback);

return state;
};
Expand Down
24 changes: 16 additions & 8 deletions src/modes/snap_point.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import MapboxDraw from "@mapbox/mapbox-gl-draw";
import {
addPointToVertices,
createSnapList,
getGuideFeature,
IDS,
Expand All @@ -9,9 +8,8 @@ import {
} from "./../utils/index.js";

const { doubleClickZoom } = MapboxDraw.lib;
const DrawPoint = MapboxDraw.modes.draw_point;
const { geojsonTypes, cursors } = MapboxDraw.constants;

const DrawPoint = MapboxDraw.modes.draw_point;
const SnapPointMode = { ...DrawPoint };

SnapPointMode.onSetup = function (options) {
Expand All @@ -37,7 +35,12 @@ SnapPointMode.onSetup = function (options) {
this.clearSelectedFeatures();
doubleClickZoom.disable(this);

const [snapList, vertices] = createSnapList(this.map, this._ctx.api, point);
const [snapList, vertices] = createSnapList(
this.map,
this._ctx.api,
point,
this._ctx.options.snapOptions?.snapGetFeatures
);

const state = {
map: this.map,
Expand All @@ -52,21 +55,26 @@ SnapPointMode.onSetup = function (options) {
state.options = this._ctx.options;

const moveendCallback = () => {
const [snapList, vertices] = createSnapList(this.map, this._ctx.api, point);
const [snapList, vertices] = createSnapList(
this.map,
this._ctx.api,
point,
this._ctx.options.snapOptions?.snapGetFeatures
);
state.vertices = vertices;
state.snapList = snapList;
};
// for removing listener later on close
state["moveendCallback"] = moveendCallback;

const optionsChangedCallBAck = (options) => {
const optionsChangedCallback = (options) => {
state.options = options;
};
// for removing listener later on close
state["optionsChangedCallBAck"] = optionsChangedCallBAck;
state["optionsChangedCallback"] = optionsChangedCallback;

this.map.on("moveend", moveendCallback);
this.map.on("draw.snap.options_changed", optionsChangedCallBAck);
this.map.on("draw.snap.options_changed", optionsChangedCallback);

return state;
};
Expand Down
25 changes: 15 additions & 10 deletions src/modes/snap_polygon.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import MapboxDraw from "@mapbox/mapbox-gl-draw";
const { geojsonTypes, modes, cursors } = MapboxDraw.constants;
const { doubleClickZoom } = MapboxDraw.lib;
const DrawPolygon = MapboxDraw.modes.draw_polygon;

import {
addPointToVertices,
createSnapList,
Expand All @@ -13,6 +9,9 @@ import {
} from "./../utils/index.js";
import booleanIntersects from "@turf/boolean-intersects";

const { doubleClickZoom } = MapboxDraw.lib;
const { geojsonTypes, modes, cursors } = MapboxDraw.constants;
const DrawPolygon = MapboxDraw.modes.draw_polygon;
const SnapPolygonMode = { ...DrawPolygon };

SnapPolygonMode.onSetup = function (options) {
Expand All @@ -38,7 +37,12 @@ SnapPolygonMode.onSetup = function (options) {
this.clearSelectedFeatures();
doubleClickZoom.disable(this);

const [snapList, vertices] = createSnapList(this.map, this._ctx.api, polygon);
const [snapList, vertices] = createSnapList(
this.map,
this._ctx.api,
polygon,
this._ctx.options.snapOptions?.snapGetFeatures
);

const state = {
map: this.map,
Expand All @@ -60,23 +64,24 @@ SnapPolygonMode.onSetup = function (options) {
const [snapList, vertices] = createSnapList(
this.map,
this._ctx.api,
polygon
polygon,
this._ctx.options.snapOptions?.snapGetFeatures
);
state.vertices = vertices;
state.snapList = snapList;
};
// for removing listener later on close
state["moveendCallback"] = moveendCallback;

const optionsChangedCallBAck = (options) => {
const optionsChangedCallback = (options) => {
state.options = options;
};

// for removing listener later on close
state["optionsChangedCallBAck"] = optionsChangedCallBAck;
state["optionsChangedCallback"] = optionsChangedCallback;

this.map.on("moveend", moveendCallback);
this.map.on("draw.snap.options_changed", optionsChangedCallBAck);
this.map.on("draw.snap.options_changed", optionsChangedCallback);

return state;
};
Expand Down Expand Up @@ -151,7 +156,7 @@ SnapPolygonMode.onStop = function (state) {

// remove moveend callback
this.map.off("moveend", state.moveendCallback);
this.map.off("draw.snap.options_changed", state.optionsChangedCallBAck);
this.map.off("draw.snap.options_changed", state.optionsChangedCallback);

var userPolygon = state.polygon;
if (state.options.overlap) {
Expand Down
17 changes: 13 additions & 4 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,18 @@ export const addPointToVertices = (
}
};

export const createSnapList = (map, draw, currentFeature) => {
// Get all drawn features
const features = draw.getAll().features;
export const createSnapList = (map, draw, currentFeature, getFeatures) => {
// Get all features
let features = [];

if (typeof getFeatures === "function") {
features = getFeatures(map, draw);
}

if (!Array.isArray(features) || features.length === 0) {
features = draw.getAll().features;
}

const snapList = [];

// Get current bbox as polygon
Expand Down Expand Up @@ -321,7 +330,7 @@ function snapToLineOrPolygon(
// the distance that needs to be undercut to trigger priority
const priorityDistance = snapVertexPriorityDistance;

// the latlng we ultemately want to snap to
// the latlng we ultimately want to snap to
let snapLatlng;

// if C is closer to the closestVertexLatLng (A, B or M) than the snapDistance,
Expand Down

0 comments on commit 94b852d

Please sign in to comment.