@pdf-viewer/react
Version:
A react-pdf-viewer component for React and Next.js. Suitable for react-pdf document.
26 lines (25 loc) • 1.4 kB
JavaScript
import { useRef as u, useCallback as g, useEffect as f } from "react";
const m = () => {
const i = u(null), a = u(/firefox/i.test(navigator.userAgent)), o = g(() => {
var c, d;
const s = document.getSelection();
if (!s || s.rangeCount === 0 || a.current)
return;
const t = s.getRangeAt(0), l = i.current && (t.compareBoundaryPoints(Range.END_TO_END, i.current) === 0 || t.compareBoundaryPoints(Range.START_TO_END, i.current) === 0);
let e = l ? t.startContainer : t.endContainer;
if ((e == null ? void 0 : e.nodeType) === Node.TEXT_NODE && (e = e.parentNode), !l && t.endOffset === 0)
do {
for (; !(e != null && e.previousSibling); )
e = e == null ? void 0 : e.parentNode;
e = e.previousSibling;
} while (!e.childNodes.length);
const n = (c = e == null ? void 0 : e.parentElement) == null ? void 0 : c.closest("[data-rp-text-layer]"), r = n == null ? void 0 : n.querySelector(".endOfContent");
r && (r.style.width = n.style.width, r.style.height = n.style.height, (d = e == null ? void 0 : e.parentElement) == null || d.insertBefore(r, l ? e : e.nextSibling)), i.current = t.cloneRange();
}, []);
f(() => (document == null || document.addEventListener("selectionchange", o), () => {
document == null || document.removeEventListener("selectionchange", o);
}), [o]);
};
export {
m as useFlickerSelectText
};