UNPKG

@pdf-viewer/react

Version:

A react-pdf-viewer component for React and Next.js. Suitable for react-pdf document.

222 lines (221 loc) 6.94 kB
import { getCharacterType as D } from "./charators.js"; function V(r, c, u, i) { const o = []; for (const n of r) { const s = y(c, n, i); o.push(...I(s, c, u)); } return o; } const X = (r, c) => { const u = ["g"]; c.matchCase || u.push("i"); let o = r.replace(/[.^$*+?()[{|\\]/g, (n) => `\\${n}`).trim(); return new RegExp(o, u.join("")); }; function y(r, c, u) { 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 = X(c, u); const s = []; let e; for (; (e = n.exec(o)) !== null; ) u.wholeWords && !L(o, e.index, e[0].length) || s.push([e.index, e[0].length, e[0]]); return s; } function I(r, c, u) { 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 h = { 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: h, end: m, str: r[l][2], oIndex: r[l][0], pageIndex: u, rect: { left: 0, bottom: 0, width: 0, height: 0 } }); } for (const l of t) { const d = s[l.start.idx], h = s[l.end.idx], m = d.transform[4] + l.start.offset * (d.width / d.str.length), f = d.transform[5], E = h.transform[4] + l.end.offset * (h.width / h.str.length), a = h.transform[5], M = { left: Math.min(m, E), bottom: Math.min(f, a), width: Math.abs(E - m), height: Math.max(d.height, h.height) }; l.rect = M; } return t; } function L(r, c, u) { 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 (D(o) === D(n)) return !1; } if (i = r.slice(c + u).match(/^\p{M}*([^\p{M}])/u), i) { const o = r.charCodeAt(c + u - 1), n = i[1].charCodeAt(0); if (D(o) === D(n)) return !1; } return !0; } function H(r, c, u) { const i = []; function o(n, s, e = -1, t = -1, l = "") { const d = c.items[n], h = []; let m = "", f = "", E = "", a = u[n]; if (!a) return; if (a.nodeType === Node.TEXT_NODE) { const p = document.createElement("span"); a.before(p), p.append(a), u[n] = p, a = p; } 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 M = document.createTextNode(m), N = document.createElement("span"); if (N.className = "highlight appended " + l, N.setAttribute("data-match-index", `${s}`), N.append(M), h.push(N), i.push({ element: N, index: s }), e > 0) if (a.childNodes.length === 1 && a.childNodes[0].nodeType === Node.TEXT_NODE) { f = d.str.substring(0, e); const p = document.createTextNode(f); h.unshift(p); } else { let p = 0; const b = []; for (const g of a.childNodes) { const x = g.nodeType === Node.TEXT_NODE ? g.nodeValue : g.firstChild.nodeValue; p += x.length, p <= e ? b.push(g) : e >= p - x.length && t <= p && b.push( document.createTextNode( x.substring(0, e - (p - x.length)) ) ); } h.unshift(...b); } if (t > 0) { E = d.str.substring(t); const p = document.createTextNode(E); h.push(p); } a.replaceChildren(...h); } 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 u = 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(u[i]); o.replaceChildren(n); } } } function W(r = {}) { return { matchCase: !1, wholeWords: !1, ...r }; } function A(r, c, u) { const i = []; function o(n, s, e = -1, t = -1, l) { var p, b; const d = c.items[n], h = []; let m = "", f = "", E = "", a = u[n]; if (!a) 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 M = document.createTextNode(m), N = document.createElement("span"); if (N.style.background = l, N.append(M), h.push(N), e > 0) if (a.childNodes.length === 1 && a.childNodes[0].nodeType === Node.TEXT_NODE) { f = d.str.substring(0, e); const g = document.createTextNode(f); h.unshift(g); } else { let g = 0; const x = []; for (const T of a.childNodes) { const v = T.nodeType === Node.TEXT_NODE ? T.nodeValue || "" : ((p = T.firstChild) == null ? void 0 : p.nodeValue) || "", C = v.length; if (g += C, g <= e) x.push(T); else if (e >= g - C && t <= g) { const w = v.substring(0, e - (g - C)); x.push(document.createTextNode(w)); } } h.unshift(...x); } if (t > 0) if (a.childNodes.length === 1 && a.childNodes[0].nodeType === Node.TEXT_NODE) { E = d.str.substring(t); const g = document.createTextNode(E); h.push(g); } else { let g = 0; const x = []; for (const T of a.childNodes) { g >= t && x.push(T); const v = T.nodeType === Node.TEXT_NODE ? T.nodeValue || "" : ((b = T.firstChild) == null ? void 0 : b.nodeValue) || "", C = v.length; if (g += C, g > t) { const w = v.substring(t - (g - C)); w && x.push(document.createTextNode(w)); } } x.length > 0 && h.push(...x); } a.replaceChildren(...h), 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 };