Skip to content

feat: add exitModeOnEscape option to exit active modes via Escape key#1612

Merged
mscno merged 3 commits intodevelopfrom
feature/exit-mode-on-escape
Dec 12, 2025
Merged

feat: add exitModeOnEscape option to exit active modes via Escape key#1612
mscno merged 3 commits intodevelopfrom
feature/exit-mode-on-escape

Conversation

@mscno
Copy link
Contributor

@mscno mscno commented Dec 12, 2025

Summary

Adds two new global options for keyboard-based interactions:

  1. exitModeOnEscape - Exit any active mode (draw, edit, drag, rotate, remove, cut) by pressing the Escape key
  2. finishOnEnter - Finish drawing the current shape by pressing the Enter key

This implements the feature request from #1586.

Usage

map.pm.setGlobalOptions({ 
  exitModeOnEscape: true,
  finishOnEnter: true 
});

Changes

Escape Key (exitModeOnEscape)

  • Added _handleEscapeKey() method to the Keyboard mixin that disables all active modes
  • Added exitModeOnEscape: false to default global options (disabled by default for backward compatibility)

Enter Key (finishOnEnter)

  • Added _handleEnterKey() method to the Keyboard mixin that finishes the current shape
  • Added _canFinishShape() helper to check if a shape has enough vertices to be finished
  • Added finishOnEnter: false to default global options (disabled by default for backward compatibility)
  • Fixed Rectangle and CircleMarker _finishShape() methods to handle being called without an event object

Supported Shapes for finishOnEnter

  • Polygon (requires 3+ vertices)
  • Line (requires 2+ vertices)
  • Rectangle (requires first corner placed)
  • Circle (requires center placed)
  • Cut (requires 3+ vertices)

Not supported (by design)

  • Marker (single-click placement)
  • CircleMarker (single-click placement)
  • Text (single-click placement)

TypeScript

  • Updated TypeScript definitions with both new options

Tests

  • Added comprehensive Cypress E2E tests for both features
  • 17 tests for exitModeOnEscape covering all modes
  • 14 tests for finishOnEnter covering all supported shapes

Test plan

  • All new E2E tests pass (31 tests total)
  • Existing tests pass
  • ESLint and Prettier checks pass

Closes #1586

Adds a new global option `exitModeOnEscape` that allows users to exit
any active mode (draw, edit, drag, rotate, remove, cut) by pressing
the Escape key.

Usage:
  map.pm.setGlobalOptions({ exitModeOnEscape: true });

- Option is disabled by default for backward compatibility
- Handles all mode types: draw, edit, drag, removal, rotate, and cut
- Includes TypeScript definitions
- Includes comprehensive Cypress E2E tests

Closes #1586
Adds a new global option `finishOnEnter` that allows users to finish
drawing shapes (Line, Polygon) by pressing Enter when enough vertices
are placed.

Also fixes Escape key handling to prevent default browser behavior
(blue focus ring) when exiting modes.

Usage:
  map.pm.setGlobalOptions({
    exitModeOnEscape: true,  // Exit any mode with Escape key
    finishOnEnter: true,     // Finish drawing with Enter key
  });

- finishOnEnter works for Line (2+ vertices) and Polygon (3+ vertices)
- Escape key now prevents default browser behavior when active
- Both options are disabled by default for backward compatibility
- Includes TypeScript definitions
- Includes comprehensive Cypress E2E tests

Related to #1586
Introduces Cypress E2E tests to verify that drawing rectangles and circles can be completed by pressing the Enter key after placing the necessary markers. Also includes tests to ensure that pressing Enter before placing markers does not finish the drawing. Additionally, updates the CircleMarker and Rectangle classes to handle click events more robustly by checking for valid latlng coordinates.

- Tests added for rectangle and circle drawing scenarios
- Ensures correct behavior when Enter is pressed at various stages of drawing
- Enhancements to CircleMarker and Rectangle classes for better event handling
@mscno mscno merged commit 130ac5c into develop Dec 12, 2025
10 checks passed
@denizmaral
Copy link

Amazing! Thank you very much!!

@mscno mscno deleted the feature/exit-mode-on-escape branch February 8, 2026 10:21
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.

Feature Request: Add support for exiting active modes via Escape key with optional toggle flag

2 participants