Repository navigation
fix(snapping): stop vertex snapping when snappable is turned off in edit mode - #1762
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Turning
snappableoff 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 tofalse, but a dragged vertex still snapped and firedpm:snap.Cause (confirmed): with
snappableoff,applyOptions()calls_disableSnapping()(Snapping.ts), which only removes the layer'spm:dragstart→_unsnaplistener. Thedrag→_handleSnappinganddragend→_cleanupSnappinglisteners that_initSnappableMarkers()→_assignEvents()put on the vertex markers stay, and_handleSnappingdoesn't readoptions.snappable. Line, Polygon (which inherits Line'sapplyOptions) and Rectangle keep their vertex markers acrossapplyOptions(), so they kept snapping.Which shapes had it, measured on fe93b0a with the new spec:
setGlobalOptions. The vertex ended 4.24 px from the pointer, on the target.enable({ snappable: false }): it rebuilds the vertex markers beforeapplyOptions().applyOptions()before deciding on snapping. Marker's own_disableSnappingremoves itspm:dragsnapping. Text's_disableSnappingdoes the same as Marker's (by reading; not in the spec).layer.pm.setOptions({ snappable: false })alone: it stores the option without callingapplyOptions()(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_handleSnappingfromdrag.dragend→_cleanupSnappingstays bound, so a drag in progress when the option is turned off still clears_snapped,_snapListand its maplayerremovelistener when it ends. Line's and Rectangle'sapplyOptions()call it in theirsnappable-off branch._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.snappableback 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'sapplyOptions()order still binds the corner handlers after_handleSnapping._unassignEventsis 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:setGlobalOptions({ snappable: false }), the point stays at the pointer and nopm:snapfires;enable({ snappable: false });pm:snapcount;snappableoff then on, it snaps to the target with the samepm:snapcount;snappableturned off mid-drag, after the vertex snapped. When the drag ends, the marker's_snappedis false, there is no_snapList, and the map has as manylayerremovelisteners 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_unassignEventscall 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 unbounddragend), 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_handleSnappingbefore_onMarkerDragand_cleanupSnappingbefore_onMarkerDragEnd.Unchanged and covered by existing specs: the draw self-snap with
snappableoff (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.