Skip to content

Commit

Permalink
Simplify the backwards selection code in the EditContext demo (#252)
Browse files Browse the repository at this point in the history
Simplified backwards selection in EditContext demo
  • Loading branch information
captainbrosset authored Feb 15, 2024
1 parent bafc336 commit f45152c
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions edit-context/html-editor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,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 @@ -460,26 +456,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 f45152c

Please sign in to comment.