Skip to content

Commit

Permalink
fix(range): Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingze Xiao committed Sep 3, 2020
1 parent ba3487f commit 0dcb97d
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/store/highlight/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ declare global {
}
}

export const getRects = (range: Range): DOMRect[] => {
export const getClientRects = (range: Range): DOMRect[] => {
const iterator = document.createNodeIterator(
range.commonAncestorContainer,
NodeFilter.SHOW_TEXT, // only show Text nodes
NodeFilter.SHOW_ALL,
function acceptNode() {
return NodeFilter.FILTER_ACCEPT;
},
Expand All @@ -44,22 +44,24 @@ export const getRects = (range: Range): DOMRect[] => {
continue;
}

newRange.selectNodeContents(currentNode);
if (currentNode === range.startContainer) {
newRange.setStart(currentNode, range.startOffset);
// Only highlight Text nodes
if (currentNode.nodeType === Node.TEXT_NODE) {
newRange.selectNodeContents(currentNode);
if (currentNode === range.startContainer) {
newRange.setStart(currentNode, range.startOffset);
}
if (currentNode === range.endContainer) {
newRange.setEnd(currentNode, range.endOffset);
}
rects.push(newRange.getBoundingClientRect());
}
if (currentNode === range.endContainer) {
newRange.setEnd(currentNode, range.endOffset);
}
rects.push(newRange.getBoundingClientRect());

if (currentNode === range.endContainer) {
break;
}

currentNode = iterator.nextNode();
}
newRange.detach();

return rects;
};
Expand All @@ -85,7 +87,7 @@ export const setSelectionAction = createAction(
let rects = Array.from(range.getClientRects());
// getClientRects on IE/Edge might return 0 rects
if (!rects.length) {
rects = getRects(range);
rects = getClientRects(range);
}

return {
Expand Down

0 comments on commit 0dcb97d

Please sign in to comment.