Skip to content

fix(rectangle): snap a dragged corner after applyOptions - #1761

Merged
mscno merged 2 commits into
v3from
fix/rectangle-corner-snap
Nov 30, 2026
Merged

mscno merged 2 commits into
v3from
fix/rectangle-corner-snap

Conversation

@mscno

@mscno mscno commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes the rectangle corner snap in edit mode. After map.pm.setGlobalOptions(...), layer.pm.applyOptions(), a snappable toggle or a pinning toggle, a dragged corner marker snapped but the rectangle's corner stayed at the pointer.

Cause (confirmed): EditRectangle.applyOptions() calls _initSnappableMarkers(). That reaches _assignEvents, which does marker.off('drag', _handleSnapping) then marker.on('drag', _handleSnapping), moving _handleSnapping to the end of each corner's drag listeners. The corner handlers (_onMarkerDrag and the rest) were bound before it, and _addMarkerEvents()'s second on is a no-op because Leaflet skips a duplicate listener. So _onMarkerDrag resized the rectangle from the unsnapped position, and only then did _handleSnapping move the marker. setOptions and a second enable() don't hit it, because enable() rebuilds the markers before applyOptions(). The veto reset in _onMarkerDragEnd (_initMarkers() then applyOptions()) doesn't hit it either, since its markers are new. Polygon and Line bind no handlers in applyOptions.

Fix: _addMarkerEvents() keeps the handlers it bound in _boundCornerHandlers. applyOptions() first unbinds those from the corners (not a fresh unstable_cornerDragHandlers() result, since an override may return new functions on each call), then applies snapping, then _addMarkerEvents() binds them after _handleSnapping. The markers are kept rather than rebuilt (unlike Pro 2.x's #65), so marker identity and a drag in progress are unaffected. No exports or private member names change.

Tests: a new Edit Rectangle corner snapping block in cypress/e2e/rectangle.cy.ts. A corner at (300, 300) is dragged to about 4 px from a triangle vertex, and the test asserts that the rectangle's ring corner equals the target and that a corner marker sits there.

  • triggers: setGlobalOptions({ continueDrawing: true }), applyOptions(), snappable off and on, pinning on and off;
  • controls: no options change, setOptions, a second enable();
  • the veto-reset path (_initMarkers() then applyOptions()), then a snapping drag;
  • an override returning new closures on each call, after applyOptions() twice: its drag handler runs once per move and sees the snapped marker (on 90f6729 it ran 6 times for 2 moves);
  • after two setGlobalOptions and an applyOptions, a two-move drag fires pm:markerdrag twice and pm:snap once.

On 774ddcc (unchanged) the 4 triggers and the listener-count test fail, with the corner at the pointer (expected 51.49869827721546 to be close to 51.498377681772325). The controls and the veto path pass. With the fix, all 9 pass.

Checks: build, typecheck, lint, unit 106/106. Full e2e (Electron): base 486/486 (774ddcc), PR 496/496 (+10).

Review (landing owner and Opus 5.5) at 90f6729: one requested change, unbinding the handlers actually bound; done in the next commit.

applyOptions re-bound _handleSnapping with off/on, which moved it behind
the corner drag handlers, so the rectangle followed the unsnapped pointer
after setGlobalOptions, applyOptions, a snappable toggle or a pinning
toggle. Unbind the corner handlers first and bind them after snapping.

Amp-Thread-ID: https://ampcode.com/threads/T-4f0176da-b9a1-4ad0-aeca-c1780876e0eb
An unstable_cornerDragHandlers override may return new functions on each
call, so applyOptions unbinds what _addMarkerEvents stored instead of a fresh
call's result.

Amp-Thread-ID: https://ampcode.com/threads/T-4f0176da-b9a1-4ad0-aeca-c1780876e0eb
@mscno
mscno merged commit fe93b0a 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