UNPKG

@huridocs/react-text-selection-handler

Version:

React pdf handler allows to render a PDF and handle text selection and highlights.

35 lines 1.77 kB
export const rangeToTextRects = (range) => { var _a, _b, _c; const textNodeIterator = document.createNodeIterator(range.commonAncestorContainer, NodeFilter.SHOW_TEXT); const selection = window.getSelection(); if (selection === null) return []; const extended = ((_b = (_a = selection.focusNode) === null || _a === void 0 ? void 0 : _a.childNodes) === null || _b === void 0 ? void 0 : _b.length) || 0 > 1; const position = ((_c = selection.anchorNode) === null || _c === void 0 ? void 0 : _c.compareDocumentPosition(selection.focusNode)) === 4 ? 'FOLLOWING' : 'PRECEDING'; const extentNode = extended || position === 'PRECEDING' ? selection.anchorNode : selection.focusNode; const nodes = []; while (textNodeIterator.nextNode()) { const isValidNode = !(nodes.length === 0 && textNodeIterator.referenceNode !== range.startContainer); if (isValidNode) { nodes.push(textNodeIterator.referenceNode); } if (extentNode === textNodeIterator.referenceNode || extentNode === textNodeIterator.referenceNode.parentElement) break; } return nodes.reduce((rects, n, index) => { const myRange = document.createRange(); myRange.selectNode(n); if (index === 0) { myRange.setStart(n, range.startOffset); } if (index === nodes.length - 1) { // @ts-expect-error - TS cannot now if Node is a text node, but this in handling text selections, so it's safe to assume it's a text node myRange.setEnd(n, !extended ? range.endOffset : n.length); } return [...rects, ...Array.from(myRange.getClientRects())]; }, []); }; //# sourceMappingURL=rangeToTextRects.js.map