@pdf-viewer/react
Version:
A react-pdf-viewer component for React and Next.js. Suitable for react-pdf document.
222 lines (221 loc) • 7.2 kB
JavaScript
import { getCharacterType as D } from "./charators.js";
function _(r, c, m, i) {
const s = [];
for (const n of r) {
const o = y(c, n, i);
s.push(...R(o, c, m));
}
return s;
}
const L = (r, c) => {
const m = ["g"];
c.matchCase || m.push("i");
let s = r.replace(/[.^$*+?()[{|\\]/g, (n) => `\\${n}`).trim();
return new RegExp(s, m.join(""));
};
function y(r, c, m) {
const i = [];
for (const t of r.items)
if (t.hasEOL)
if (t.str.endsWith("-")) {
const d = t.str.lastIndexOf("-");
i.push(t.str.substring(0, d));
} else
i.push(t.str, `
`);
else
i.push(t.str);
const s = i.join("").replace(/\n/g, " ");
let n;
c instanceof RegExp ? n = c.flags.indexOf("g") === -1 ? new RegExp(c, `${c.flags}g`) : c : n = L(c, m);
const o = [];
let e;
for (; (e = n.exec(s)) !== null; )
m.wholeWords && !W(s, e.index, e[0].length) || o.push([e.index, e[0].length, e[0]]);
return o;
}
function R(r, c, m) {
function i(d) {
return d.hasEOL ? d.str.endsWith("-") ? -1 : 1 : 0;
}
let s = 0, n = 0;
const o = c.items, e = o.length - 1, t = [];
for (let d = 0; d < r.length; d++) {
let h = r[d][0];
for (; s !== e && h >= n + o[s].str.length; ) {
const x = o[s];
n += x.str.length + i(x), s++;
}
const g = {
idx: s,
offset: h - n
};
for (h += r[d][1]; s !== e && h > n + o[s].str.length; ) {
const x = o[s];
n += x.str.length + i(x), s++;
}
const l = {
idx: s,
offset: h - n
};
t.push({
start: g,
end: l,
str: r[d][2],
oIndex: r[d][0],
pageIndex: m,
rect: { left: 0, bottom: 0, width: 0, height: 0 },
rects: []
});
}
for (const d of t) {
const h = [];
for (let g = d.start.idx; g <= d.end.idx; g++) {
const l = o[g], x = l.transform[4], C = l.transform[5], a = l.str.length > 0 ? l.width / l.str.length : 0, M = g === d.start.idx ? d.start.offset : 0, N = g === d.end.idx ? d.end.offset : l.str.length, f = x + M * a, E = (N - M) * a, u = (c.styles ?? {})[l.fontName], T = (u == null ? void 0 : u.ascent) ?? 1, v = (u == null ? void 0 : u.descent) != null ? Math.abs(u.descent) : 1 - T, b = C - v * l.height;
(E > 0 || l.height > 0) && h.push({
left: f,
bottom: b,
width: E,
height: l.height
});
}
d.rect = h[0], d.rects = h;
}
return t;
}
function W(r, c, m) {
let i = r.slice(0, c).match(/([^\p{M}])\p{M}*$/u);
if (i) {
const s = r.charCodeAt(c), n = i[1].charCodeAt(0);
if (D(s) === D(n))
return !1;
}
if (i = r.slice(c + m).match(/^\p{M}*([^\p{M}])/u), i) {
const s = r.charCodeAt(c + m - 1), n = i[1].charCodeAt(0);
if (D(s) === D(n))
return !1;
}
return !0;
}
function I(r, c, m) {
const i = [];
function s(n, o, e = -1, t = -1, d = "") {
const h = c.items[n], g = [];
let l = "", x = "", C = "", a = m[n];
if (!a)
return;
if (a.nodeType === Node.TEXT_NODE) {
const f = document.createElement("span");
a.before(f), f.append(a), m[n] = f, a = f;
}
e >= 0 && t >= 0 ? l = h.str.substring(e, t) : e < 0 && t < 0 ? l = h.str : e >= 0 ? l = h.str.substring(e) : t >= 0 && (l = h.str.substring(0, t));
const M = document.createTextNode(l), N = document.createElement("span");
if (N.className = "highlight appended " + d, N.setAttribute("data-match-index", `${o}`), N.append(M), g.push(N), i.push({ element: N, index: o }), e > 0)
if (a.childNodes.length === 1 && a.childNodes[0].nodeType === Node.TEXT_NODE) {
x = h.str.substring(0, e);
const f = document.createTextNode(x);
g.unshift(f);
} else {
let f = 0;
const E = [];
for (const p of a.childNodes) {
const u = p.nodeType === Node.TEXT_NODE ? p.nodeValue : p.firstChild.nodeValue;
f += u.length, f <= e ? E.push(p) : e >= f - u.length && t <= f && E.push(
document.createTextNode(
u.substring(0, e - (f - u.length))
)
);
}
g.unshift(...E);
}
if (t > 0) {
C = h.str.substring(t);
const f = document.createTextNode(C);
g.push(f);
}
a.replaceChildren(...g);
}
for (const [n, o] of r.entries())
if (o.start.idx === o.end.idx)
s(o.start.idx, n, o.start.offset, o.end.offset);
else
for (let e = o.start.idx, t = o.end.idx; e <= t; e++)
e === o.start.idx ? s(e, n, o.start.offset, -1, "begin") : e === o.end.idx ? s(e, n, -1, o.end.offset, "end") : s(e, n, -1, -1, "middle");
return i;
}
function V(r, c) {
const m = r.items.map((i) => i.str);
for (let i = 0; i < c.length; i++) {
const s = c[i];
if (s && s.nodeType !== Node.TEXT_NODE) {
const n = document.createTextNode(m[i]);
s.replaceChildren(n);
}
}
}
function H(r = {}) {
return { matchCase: !1, wholeWords: !1, ...r };
}
function S(r, c, m) {
const i = [];
function s(n, o, e = -1, t = -1, d) {
var f, E;
const h = c.items[n], g = [];
let l = "", x = "", C = "", a = m[n];
if (!a)
return;
e >= 0 && t >= 0 ? l = h.str.substring(e, t) : e < 0 && t < 0 ? l = h.str : e >= 0 ? l = h.str.substring(e) : t >= 0 && (l = h.str.substring(0, t));
const M = document.createTextNode(l), N = document.createElement("span");
if (N.style.background = d, N.append(M), g.push(N), e > 0)
if (a.childNodes.length === 1 && a.childNodes[0].nodeType === Node.TEXT_NODE) {
x = h.str.substring(0, e);
const p = document.createTextNode(x);
g.unshift(p);
} else {
let p = 0;
const u = [];
for (const T of a.childNodes) {
const v = T.nodeType === Node.TEXT_NODE ? T.nodeValue || "" : ((f = T.firstChild) == null ? void 0 : f.nodeValue) || "", b = v.length;
if (p += b, p <= e)
u.push(T);
else if (e >= p - b && t <= p) {
const w = v.substring(0, e - (p - b));
u.push(document.createTextNode(w));
}
}
g.unshift(...u);
}
if (t > 0)
if (a.childNodes.length === 1 && a.childNodes[0].nodeType === Node.TEXT_NODE) {
C = h.str.substring(t);
const p = document.createTextNode(C);
g.push(p);
} else {
let p = 0;
const u = [];
for (const T of a.childNodes) {
p >= t && u.push(T);
const v = T.nodeType === Node.TEXT_NODE ? T.nodeValue || "" : ((E = T.firstChild) == null ? void 0 : E.nodeValue) || "", b = v.length;
if (p += b, p > t) {
const w = v.substring(t - (p - b));
w && u.push(document.createTextNode(w));
}
}
u.length > 0 && g.push(...u);
}
a.replaceChildren(...g), i.push({ element: N, index: o });
}
for (const [n, o] of r.entries()) {
const { start: e, end: t, color: d } = o;
e.idx === t.idx && s(e.idx, n, e.offset, t.offset, d);
}
return i;
}
export {
_ as findMatches,
H as getHighlightOptionsWithDefaults,
I as highlightMatches,
S as highlightMultipleColorMatches,
W as isMatchEntireWord,
V as resetDivs
};