Skip to content

Commit

Permalink
Simplified the backwards selection code
Browse files Browse the repository at this point in the history
  • Loading branch information
captainbrosset committed Feb 2, 2024
1 parent c692414 commit 18bddb6
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions edit-context/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,6 @@
}

function convertFromOffsetsToSelection(start, end) {
const isBackwards = start > end;
const orderedStart = isBackwards ? end : start;
const orderedEnd = isBackwards ? start : end;

const treeWalker = document.createTreeWalker(editorEl, NodeFilter.SHOW_TEXT);

let offset = 0;
Expand All @@ -463,26 +459,24 @@
while (treeWalker.nextNode()) {
const node = treeWalker.currentNode;

if (!anchorNode && offset + node.textContent.length >= orderedStart) {
if (!anchorNode && offset + node.textContent.length >= start) {
anchorNode = node;
anchorOffset = orderedStart - offset;
anchorOffset = start - offset;
}

if (offset + node.textContent.length >= orderedEnd) {
if (!extentNode && offset + node.textContent.length >= end) {
extentNode = node;
extentOffset = orderedEnd - offset;
extentOffset = end - offset;
}

if (anchorNode && extentNode) {
break;
}

offset += node.textContent.length;
}

return {
anchorNode: isBackwards ? extentNode : anchorNode,
anchorOffset: isBackwards ? extentOffset : anchorOffset,
extentNode: isBackwards ? anchorNode : extentNode,
extentOffset: isBackwards ? anchorOffset : extentOffset
};
return { anchorNode, anchorOffset, extentNode, extentOffset };
}

// Render the initial view.
Expand Down

0 comments on commit 18bddb6

Please sign in to comment.