Skip to content

Selection API (window.getSelection, Range) for text selection #37

@wangnaihe

Description

@wangnaihe

Context

Per docs/vscode-compat.md Phase A (P0). Text selection is fundamental to any text editor. Monaco Editor and every text-based UI depends on `window.getSelection()` and the `Range` API.

Requirements

Selection API

  • `window.getSelection()` → `Selection` object
  • `Selection.anchorNode` / `anchorOffset` — where selection starts
  • `Selection.focusNode` / `focusOffset` — where selection ends
  • `Selection.isCollapsed` — true if no text is selected (cursor only)
  • `Selection.toString()` — selected text content
  • `Selection.collapse(node, offset)` — set cursor position
  • `Selection.selectAllChildren(node)`
  • `Selection.removeAllRanges()` / `addRange(range)`

Range API

  • `document.createRange()` → `Range`
  • `Range.setStart(node, offset)` / `setEnd(node, offset)`
  • `Range.startContainer` / `endContainer` / `startOffset` / `endOffset`
  • `Range.collapsed`
  • `Range.getBoundingClientRect()` → position of selected text (for cursor/highlight rendering)
  • `Range.cloneContents()` / `extractContents()` / `deleteContents()`

Rendering

  • Render selection highlight (blue background) over selected text
  • Render text cursor (blinking caret) at collapsed selection position
  • Handle mouse drag to create selection
  • Handle Shift+Arrow keys to extend selection
  • Handle Ctrl/Cmd+A to select all
  • Handle Ctrl/Cmd+C to copy selected text (integrates with Clipboard API)

Implementation Notes

This is tightly coupled with text layout — need to map pixel coordinates (mouse click) to text node + character offset. Requires glyph-level hit testing from the font/layout engine.

References

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions