no comment nodes without react,so can bring this back to simpler version

pull/21833/head
Bruno Windels 2019-05-10 14:25:13 +02:00
parent 7a85dd4e61
commit 9f597c7ec0
1 changed files with 13 additions and 26 deletions

View File

@ -68,35 +68,22 @@ function isVisibleNode(node) {
return node.nodeType === Node.ELEMENT_NODE || node.nodeType === Node.TEXT_NODE;
}
function untilVisibleNode(node) {
// need to ignore comment nodes that react uses
while (node && !isVisibleNode(node)) {
node = node.nextSibling;
}
return node;
}
export function setCaretPosition(editor, caretPosition) {
let node = untilVisibleNode(editor.firstChild);
if (!node) {
node = editor;
} else {
let {index} = caretPosition;
while (node && index) {
node = untilVisibleNode(node.nextSibling);
--index;
}
if (!node) {
node = editor;
} else if (node.nodeType === Node.ELEMENT_NODE) {
// make sure we have a text node
node = node.childNodes[0];
}
}
const sel = document.getSelection();
sel.removeAllRanges();
const range = document.createRange();
range.setStart(node, caretPosition.offset);
range.collapse(true);
let focusNode = editor.childNodes[caretPosition.index];
// node not found, set caret at end
if (!focusNode) {
range.selectNodeContents(editor);
range.collapse(false);
} else {
// make sure we have a text node
if (focusNode.nodeType === Node.ELEMENT_NODE) {
focusNode = focusNode.childNodes[0];
}
range.setStart(focusNode, caretPosition.offset);
range.collapse(true);
}
sel.addRange(range);
}