How can I programmatically apply a selection to a range of mixed nodes (list nodes, paragraph nodes, etc.)?

25 views Asked by At

eg. enter image description here

//works for textnodes but breaks the moment it encounters listnodes
const rangeSelection = $createRangeSelection();

const firstChild = firstNode.getTopLevelElement().getFirstChild();
let lastChild = lastNode.getTopLevelElement().getLastChild();

const initialFocusOffSet = prevText.length || 0;
const initialAnchorOffSet = lastChild?.getTextContentSize();

rangeSelection.setTextNodeRange(
  firstChild,
  initialFocusOffSet,
  lastChild,
  initialAnchorOffSet
);
$setSelection(rangeSelection);

But I want to achieve this for other nodes as well, eg. lists, etc.

I've tried the createDOMRange in the following way, but it doesn't seem to work as expected. I get range as null for some reason.

 //doesn't work!
const range = createDOMRange(
  activeEditor,
  firstNode,
  initialFocusOffSet,
  lastNode,
  initialAnchorOffSet
);

applyDomRange(range);
0

There are 0 answers