Search code examples
javascriptdombrowserselectiontextselection

What are some scenarios in which you'd care the direction of a browser selection?


The browser DOM Selection API provides properties showing what node and offset a selection begins at (Selection.anchorNode and Selection.anchorOffset), and what node and offset the selection ends at (Selection.focusNode and Selection.focusOffset). In some of my selection handling methods, I need to normalize these values to a consistent direction. It got me wondering, in what situations would the direction of a selection matter? It's not for handling RTL languages; users arbitrarily select in either direction in any language.


Solution

  • I asked the question on the ProseMirror support board.There are common behaviors that make use of the directionality of a DOM selection:

    • Shift-click, which uses the anchor end of a selection to determine (when a user clicks outside of an existing selection) how the selection should be extended;
    • Shift-arrow, which uses the anchor end similarly to shift-click but with arrow keys instead of a mouse click to determine how the selection should be extended.
    • Displaying a tooltip at the location closest to the user’s mouse after a selection.