Selection API: Reading Highlighted Text

Write the paragraph, select with the mouse or the node map; see anchor and focus, direction, per-node offsets, and the Range that is always in order. An…

Why it matters

Text editors, a "quote" button, comment highlights, translate-on-select: they all read the Selection. The bugs are classic: a reversed direction, an offset taken for a position in the paragraph, or a selection that turns out to be outside your element.

The key idea

A selection is two points (node + offset), and its direction follows how you highlighted

Common mistakes

Try it in this playground