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