[Feature Request] Add Native Touch-Based Text Selection Support for Mobile Devices

Open
#5,538 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active

Research direction

Start by reviewing the editor pointer and selection handling entry points and the public APIs cited in the issue: getTargetAtClientPoint, setSelection, and executeEdits. The issue describes desired mobile interactions but does not identify files, tests, or a settled design; first determine how touch input and native selection currently interact. Done means providing a supported touch selection experience or API that works in embedded mobile browsers while preserving normal editing behavior.

Written by the indexing model from the issue text.

Description

feature-request
Context
  • This issue is not a bug report. (please use a different template for reporting a bug)
  • This issue is not a duplicate of an existing issue. (please use the search to find existing issues)
Description
Context

Monaco Editor is difficult to use for text selection on mobile devices, particularly in Android WebView and other touch-based environments.

On desktop, users can select text by clicking and dragging with a mouse. However, on mobile devices, the standard touch-based text selection experience, including draggable selection handles, is not consistently available.

This makes common editing operations, such as deleting a specific block of text, difficult to perform on mobile devices.

Feature Request

I would like to request built-in touch-based text selection support for mobile devices.

Ideally, Monaco Editor would support native-style text selection handles that allow users to adjust the beginning and end of a selection by dragging them.

Alternatively, a built-in tap-to-select mode would also be very useful. For example:

  1. Enable selection mode through a toolbar button or API.
  2. Tap the desired starting position.
  3. Tap the desired ending position.
  4. Monaco selects the text between the two positions.
  5. Perform an editing operation, such as deleting the selected text.
  6. Exit selection mode and return to normal editing.

This interaction would be especially useful in mobile applications that embed Monaco Editor inside an Android WebView or another embedded browser.

Current Workaround

I have implemented a temporary two-point selection and deletion mechanism in my application using Monaco's public APIs.

The workflow is:

Enable two-point selection → Tap point A → Tap point B → Press Backspace to delete the selected text → Disable selection mode.

The implementation uses:

  • editor.getDomNode() to attach a pointerup listener.
  • editor.getTargetAtClientPoint() to map touch coordinates to Monaco model positions.
  • monaco.Selection and editor.setSelection() to select the text between the two points.
  • editor.executeEdits() to delete the selected range.
  • A capturing-phase keydown listener to intercept Backspace before Monaco's default keyboard handling.

Here is a simplified example of the core selection logic:

let selectionStart = null;

const editorDom = editor.getDomNode();

editorDom.addEventListener('pointerup', (event) => {
    if (event.pointerType !== 'touch' &&
        event.pointerType !== 'mouse') {
        return;
    }

    const target = editor.getTargetAtClientPoint(
        event.clientX,
        event.clientY
    );

    const position = target?.position;
    if (!position) {
        return;
    }

    if (!selectionStart) {
        selectionStart = position;
        return;
    }

    const selection = new monaco.Selection(
        selectionStart.lineNumber,
        selectionStart.column,
        position.lineNumber,
        position.column
    );

    // Defer selection until the current pointer event has finished.
    setTimeout(() => {
        editor.setSelection(selection);
    }, 0);

    selectionStart = null;
}, { passive: true });

window.addEventListener('keydown', (event) => {
    if (event.key !== 'Backspace') {
        return;
    }

    const selection = editor.getSelection();
    if (!selection || selection.isEmpty()) {
        return;
    }

    event.preventDefault();
    event.stopPropagation();

    const model = editor.getModel();
    if (!model) {
        return;
    }

    editor.executeEdits('touch-selection-delete', [{
        range: selection,
        text: ''
    }]);
}, true);

This is only a simplified proof of concept. A production implementation would also need explicit selection-mode state, cleanup when the mode is disabled, handling for reversed selections, and safeguards to avoid intercepting Backspace during normal editing.

Expected Benefits
  • Makes text selection and editing more practical on mobile devices.
  • Improves usability in Android WebView and other embedded browser environments.
  • Provides a consistent interaction model across touch-based devices.
  • Reduces the need for applications to implement custom selection logic and keyboard-event handling.

I understand that Monaco Editor is primarily designed for desktop use, and I appreciate that mobile interaction may require additional design considerations. However, even an optional, API-controlled touch selection mode would be a valuable improvement.

Would the team consider adding native touch selection support or providing a supported API for implementing tap-to-select interactions?

Thank you for your work on Monaco Editor.

Monaco Editor Playground Link

No response

Monaco Editor Playground Code

Dominant language
JavaScript
Stars
47k
Forks
4.2k
Avg merge
3d 13h
Merged PRs (30d)
13

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/monaco-editor

All issues in microsoft/monaco-editor

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.