Currently i am writing an editor that is supposed to insert HTML at the caret position. This is supposed to happen after a choice has been made in the context menu.
Because opening the context menu removes the caret i'm saving it as following
this.range = caret.cloneRange();
After the choice has been made i'm restoring the caret position and inserting html as following
const sel = window.getSelection()!;
sel.removeAllRanges();
sel.addRange(this.range);
const node = document.createTextNode(text);
range!.insertNode(node);
Unfortunately this causes the following error
HierarchyRequestError: The operation would yield an incorrect node tree.
Is there any way i can save and restore the caret position and insert html after that?