@pdf-viewer/react
Version:
The PDF Viewer component for React and Next.js
53 lines (52 loc) • 1.24 kB
JavaScript
import { useState as l, useCallback as x, useEffect as C } from "react";
import { getHighlightOptionsWithDefaults as H, findMatches as M } from "../highlight.js";
const d = (i) => {
const [g, a] = l(void 0), [u, r] = l([]), m = x(async (t) => {
if (!t)
return {};
const o = t.numPages, s = {};
for (let e = 1; e <= o; e++) {
const h = await t.getPage(e);
s[e.toString()] = await h.getTextContent();
}
return s;
}, []), f = async (t) => {
a(t);
}, c = () => {
r([]), a(void 0);
};
return C(() => {
i && m(i).then((t) => {
let o = [];
for (const { keyword: s, highlightColor: e, options: h } of g) {
const p = H(h);
for (const n of Object.keys(t)) {
const y = M(
[s],
t[n],
Number(n) - 1,
p
).map((b, w) => ({
...b,
page: Number(n),
pageMatchIdx: w,
color: e,
keyword: s
}));
o = [...o, ...y];
}
}
r(o);
}).catch(() => {
c();
});
}, [g, i]), {
highlight: f,
clear: c,
highlightMatches: u,
highlightKeywords: g
};
};
export {
d as useHighlight
};