当前,我正在编写一个编辑器,该编辑器应该在插入符号位置插入HTML。在上下文菜单中做出选择后,应该会发生这种情况。
因为打开上下文菜单会删除插入符,所以我将其保存为以下内容
this.range = caret.cloneRange();
做出选择后,我将恢复插入符号的位置并按如下所示插入html
const sel = window.getSelection()!;
sel.removeAllRanges();
sel.addRange(this.range);
const node = document.createTextNode(text);
range!.insertNode(node);
不幸的是,这会导致以下错误
HierarchyRequestError: The operation would yield an incorrect node tree.
有什么方法可以保存和恢复插入符号的位置,然后再插入html吗?