Typix LogoTypix
CoreUtilities

getSelectedNode

Get the currently selected node from a range selection

getSelectedNode returns the most relevant node in the current selection. Handles forward and backward selections correctly.

Import

import { getSelectedNode } from '@typix-editor/react';

Usage

import { $getSelection, $isRangeSelection } from '@typix-editor/react';

editor.read(() => {
  const selection = $getSelection();
  if ($isRangeSelection(selection)) {
    const node = getSelectedNode(selection);
    console.log(node.getType()); // 'text', 'paragraph', etc.
  }
});

Parameters

ParameterTypeDescription
selectionRangeSelectionA Lexical range selection

Return value

TypeDescription
TextNode | ElementNodeThe selected node

Behavior

  • If anchor and focus are the same node, returns that node
  • For forward selections, returns the node at the anchor end
  • For backward selections, returns the node at the focus end
  • Uses $isAtNodeEnd to determine the boundary node

On this page