Skip to content

Commit

Permalink
Sort snapping layers by priority in a radius of 5px (#1454) (patch)
Browse files Browse the repository at this point in the history
  • Loading branch information
Falke-Design authored Feb 9, 2024
1 parent 1d54501 commit 7f388cc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
49 changes: 49 additions & 0 deletions cypress/integration/polygon.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1278,4 +1278,53 @@ describe('Draw & Edit Poly', () => {
expect(hintLine.options.color).to.eql('red');
});
});

it('snap to start marker instead of to the layer below', () => {
cy.window().then(({ map, L }) => {
// it was not possible to create this test with creating the polygon by clicking
const polygon = L.polygon([
[
[20.53507732696281, 71.98242187500001],
[19.87005983797396, 71.97143554687501],
[19.782211275967995, 73.35021972656251],
[20.55565240377338, 73.48754882812501],
[20.53507732696281, 71.98242187500001],
],
]);
polygon.addTo(map);
map.fitBounds(polygon.getBounds(), { animate: false });
map.setZoom(8, { animate: false });

map.pm.enableDraw('Polygon');

map.pm.Draw.Polygon._hintMarker.setLatLng([
20.53837097209846, 72.22334801861803,
]);
map.pm.Draw.Polygon._createVertex({
latlng: [20.53837097209846, 72.22334801861803],
});

map.pm.Draw.Polygon._hintMarker.setLatLng([
20.21581109239457, 72.13073730468751,
]);
map.pm.Draw.Polygon._createVertex({
latlng: [20.21581109239457, 72.13073730468751],
});

map.pm.Draw.Polygon._hintMarker.setLatLng([
20.205501205844214, 72.77893066406251,
]);
map.pm.Draw.Polygon._createVertex({
latlng: [20.205501205844214, 72.77893066406251],
});
});

cy.get(mapSelector).trigger('mousemove', 413, 180);

cy.window().then(({ map }) => {
const hintMarker = map.pm.Draw.Polygon._hintMarker;
expect(hintMarker.getLatLng().lat).to.eq(20.53837097209846);
expect(hintMarker.getLatLng().lng).to.eq(72.22334801861803);
});
});
});
5 changes: 3 additions & 2 deletions src/js/Mixins/Snapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,10 @@ const SnapMixin = {
if (
amount === 1 &&
(closestLayer.distance === undefined ||
results.distance <= closestLayer.distance)
results.distance - 5 <= closestLayer.distance)
) {
if (results.distance < closestLayer.distance) {
// if the layer is less then 5 pixels away, we treat it as same distance and sort it based on priority
if (results.distance + 5 < closestLayer.distance) {
closestLayers = [];
}
closestLayer = results;
Expand Down

0 comments on commit 7f388cc

Please sign in to comment.