only increase offset if caret hasn't been found yet

also rename caret away as this isn't used for the caret solely anymore
This commit is contained in:
Bruno Windels 2019-09-04 15:58:13 +02:00
parent 7a01d1407f
commit 4691108a16

View file

@ -80,13 +80,13 @@ function getCaret(node, offsetToNode, offsetWithinNode) {
// all ZWS from caret nodes are filtered out // all ZWS from caret nodes are filtered out
function getTextAndOffsetToNode(editor, selectionNode) { function getTextAndOffsetToNode(editor, selectionNode) {
let offsetToNode = 0; let offsetToNode = 0;
let foundCaret = false; let foundNode = false;
let text = ""; let text = "";
function enterNodeCallback(node) { function enterNodeCallback(node) {
if (!foundCaret) { if (!foundNode) {
if (node === selectionNode) { if (node === selectionNode) {
foundCaret = true; foundNode = true;
} }
} }
// usually newlines are entered as new DIV elements, // usually newlines are entered as new DIV elements,
@ -94,12 +94,14 @@ function getTextAndOffsetToNode(editor, selectionNode) {
// converted to BRs, so also take these into account when they // converted to BRs, so also take these into account when they
// are not the last element in the DIV. // are not the last element in the DIV.
if (node.tagName === "BR" && node.nextSibling) { if (node.tagName === "BR" && node.nextSibling) {
if (!foundNode) {
offsetToNode += 1;
}
text += "\n"; text += "\n";
offsetToNode += 1;
} }
const nodeText = node.nodeType === Node.TEXT_NODE && getTextNodeValue(node); const nodeText = node.nodeType === Node.TEXT_NODE && getTextNodeValue(node);
if (nodeText) { if (nodeText) {
if (!foundCaret) { if (!foundNode) {
offsetToNode += nodeText.length; offsetToNode += nodeText.length;
} }
text += nodeText; text += nodeText;
@ -114,7 +116,7 @@ function getTextAndOffsetToNode(editor, selectionNode) {
// whereas you just want it to be appended to the current line // whereas you just want it to be appended to the current line
if (node.tagName === "DIV" && node.nextSibling && node.nextSibling.tagName === "DIV") { if (node.tagName === "DIV" && node.nextSibling && node.nextSibling.tagName === "DIV") {
text += "\n"; text += "\n";
if (!foundCaret) { if (!foundNode) {
offsetToNode += 1; offsetToNode += 1;
} }
} }