Skip to content

fix(snapping): stop vertex snapping when snappable is turned off in edit mode - #1762

Merged
mscno merged 2 commits into
v3from
fix/snappable-off
Nov 30, 2026
Merged

mscno merged 2 commits into
v3from
fix/snappable-off

Conversation

@mscno

@mscno mscno commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Turning snappable off while a line, polygon or rectangle is in edit mode now stops its vertices from snapping. Before, map.pm.setGlobalOptions({ snappable: false }) set the layer's option to false, but a dragged vertex still snapped and fired pm:snap.

Cause (confirmed): with snappable off, applyOptions() calls _disableSnapping() (Snapping.ts), which only removes the layer's pm:dragstart → _unsnap listener. The drag → _handleSnapping and dragend → _cleanupSnapping listeners that _initSnappableMarkers() → _assignEvents() put on the vertex markers stay, and _handleSnapping doesn't read options.snappable. Line, Polygon (which inherits Line's applyOptions) and Rectangle keep their vertex markers across applyOptions(), so they kept snapping.

Which shapes had it, measured on fe93b0a with the new spec:

  • Polygon, Line, Rectangle: yes, through setGlobalOptions. The vertex ended 4.24 px from the pointer, on the target.
  • Not through enable({ snappable: false }): it rebuilds the vertex markers before applyOptions().
  • Circle, CircleMarker, Marker: no. CircleMarker/Circle rebuild their markers in applyOptions() before deciding on snapping. Marker's own _disableSnapping removes its pm:drag snapping. Text's _disableSnapping does the same as Marker's (by reading; not in the spec).
  • layer.pm.setOptions({ snappable: false }) alone: it stores the option without calling applyOptions() (true for every option), so it doesn't reach this code.

Fix: a new mixin method, _unassignEvents(markers), the inverse of _assignEvents. It walks the nested marker arrays and removes _handleSnapping from drag. dragend → _cleanupSnapping stays bound, so a drag in progress when the option is turned off still clears _snapped, _snapList and its map layerremove listener when it ends. Line's and Rectangle's applyOptions() call it in their snappable-off branch.

  • The shared _disableSnapping() is unchanged. Layer drag (_dragSessionStart) calls it on an editable circle's editor when synced layers are dragged, so removing marker listeners there would have stopped that circle's resize snapping.
  • Turning snappable back on re-binds through _assignEvents (off, then on), so each handler is bound once. For Rectangle, fix(rectangle): snap a dragged corner after applyOptions #1761's applyOptions() order still binds the corner handlers after _handleSnapping.
  • No exports change, and no private member is renamed; _unassignEvents is new.

Tests: new cypress/e2e/snappableOff.cy.ts, 32 tests across Polygon, a MultiPolygon (dragging a vertex of its second polygon), Line, Rectangle, Circle (center marker), CircleMarker and Marker. A vertex or the layer is dragged from (300, 300) to about 4 px from a triangle vertex. For each shape:

  • after setGlobalOptions({ snappable: false }), the point stays at the pointer and no pm:snap fires;
  • the same after enable({ snappable: false });
  • with no options change, it snaps to the target; this run records the pm:snap count;
  • after snappable off then on, it snaps to the target with the same pm:snap count;
  • for the shapes that keep their markers: snappable turned off mid-drag, after the vertex snapped. When the drag ends, the marker's _snapped is false, there is no _snapList, and the map has as many layerremove listeners as before the drag.

Tests that pass on the base are labelled "(control, passes on the base)": the enable({ snappable: false }) cases, and Circle, CircleMarker and Marker.

On fe93b0a, 3 of the first 24 fail ("Polygon/Line/Rectangle: does not snap after setGlobalOptions", each with the point 4.24 px from the pointer: -4.242640686954649) and 21 pass. With the fix all 24 pass. A mutant without Rectangle's _unassignEvents call fails only the Rectangle case. A mutant that unbinds one level deep instead of recursing fails the MultiPolygon case. On 6067372 (the first head, which also unbound dragend), the 4 mid-drag tests fail with _snapped: expected true to equal false. After off then on, each snapping handler is bound once, and Rectangle still runs _handleSnapping before _onMarkerDrag and _cleanupSnapping before _onMarkerDragEnd.

Unchanged and covered by existing specs: the draw self-snap with snappable off (polygon.cy.ts, "allows polygon completion by self-snapping when snappable is false"), snappingOrder, and #1761's rectangle corner tests. The Alt key check is inside _handleSnapping, which this PR doesn't touch.

Checks: build, typecheck, lint, unit 106/106. Full e2e (Electron): base 496/496 (fe93b0a), PR 528/528 (+32).

Review (landing owner and Opus 5.5) at 6067372: one requested change, keeping the snap cleanup for a mid-drag toggle; done in the next commit, with the MultiPolygon test and the control labels.

…dit mode

applyOptions with snappable off only removed the layer's pm:dragstart unsnap
listener; the drag and dragend snapping that _assignEvents put on the vertex
markers stayed, so Line, Polygon and Rectangle kept snapping after
setGlobalOptions({ snappable: false }). Remove them with _unassignEvents.
The shared _disableSnapping is unchanged, since layer drag calls it on an
editable circle's editor.

Amp-Thread-ID: https://ampcode.com/threads/T-4f0176da-b9a1-4ad0-aeca-c1780876e0eb
Unbinding dragend → _cleanupSnapping left a drag in progress with _snapped,
_snapList and a map layerremove listener when it ended. Unbind only the
drag snapping. Tests: the mid-drag case, a MultiPolygon, and controls
labelled.

Amp-Thread-ID: https://ampcode.com/threads/T-4f0176da-b9a1-4ad0-aeca-c1780876e0eb
@mscno
mscno merged commit 790c503 into v3 Nov 30, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants