UNPKG

@pdf-viewer/react

Version:

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

328 lines (327 loc) 9.85 kB
import { sanitizeExternalUrl as L } from "./sanitizeExternalUrl.js"; import { AnnotationType as l } from "./types.js"; import { dateFormatter as w } from "./dateFormatter.js"; import "../de_DE-a553b162.js"; const x = ["click", "dblclick", "input", "change", "mouseover"], p = /* @__PURE__ */ new Map(), v = (t, n) => `${t.loadingTask.docId}___${n.num}R${n.gen === 0 ? "" : n.gen}`; function _(t, n) { const e = v(t, n); return p.has(e) ? p.get(e) ?? null : null; } const B = (t, n, e) => { p.set(v(t, n), e); }, O = (t, n) => { switch (n[1].name) { case "XYZ": return { bottomOffset: (e, a) => n[3] === null ? a : n[3], leftOffset: (e, a) => n[2] === null ? 0 : n[2], pageIndex: t, scaleTo: n[4] }; case "Fit": case "FitB": return { bottomOffset: 0, leftOffset: 0, pageIndex: t, scaleTo: 1 }; case "FitH": case "FitBH": return { bottomOffset: n[2], leftOffset: 0, pageIndex: t, scaleTo: 1 }; default: return { bottomOffset: 0, leftOffset: 0, pageIndex: t, scaleTo: 1 }; } }; async function E(t, n) { let e; if (typeof n == "string" ? e = await t.getDestination(n) : e = n, e && typeof e[0] == "object" && e[0] !== null) { const a = e[0], s = _(t, a); if (s === null) { const o = await t.getPageIndex(a); return B(t, a, o), await E(t, e); } else return O(s, e); } else return O(e[0], e); } const H = (t) => { const e = t.target.parentNode; if (!e) return; const a = new MutationObserver((s) => { s.forEach(() => { e.querySelectorAll( '[data-l10n-id="pdfjs-annotation-date-time-string"]' ).forEach((i) => { const c = i.getAttribute("data-l10n-args"); if (c) { try { const { dateObj: r } = JSON.parse(c); r && (i.textContent = w(r)); } catch { } a.disconnect(); } }); }); }); return a.observe(e, { childList: !0, // Watch for changes to child elements subtree: !0, // Watch all descendants, not just direct children attributes: !0 // Watch for changes to attributes }), () => a.disconnect(); }; function P(t, n) { for (const e of x) t.addEventListener(e, n); } function W(t, n) { for (const e of x) t.removeEventListener(e, n); } function j(t) { const { id: n, url: e, unsafeUrl: a } = t; new MutationObserver((o, i) => { const c = document.querySelector(`[data-annotation-id="${n}"]`); if (c) { const r = c.querySelector("a"), g = L((e || a) ?? "", ""); r && (r == null || r.setAttribute("target", "_blank"), r == null || r.setAttribute("href", g), r == null || r.setAttribute("rel", "noopener noreferrer")), i.disconnect(); } }).observe(document.body, { childList: !0, subtree: !0 }); } function V(t, n, e) { const { rotation: a, scale: s } = n, o = a % 180 === 0; if (t.fieldType === "Btn" && t.pushButton) { const i = t.rect[2] - t.rect[0], c = t.rect[3] - t.rect[1], r = document.createElement("canvas"); r.setAttribute("width", ((o ? i : c) * s).toString()), r.setAttribute("height", ((o ? c : i) * s).toString()), e.set(t.id, r); } } function $(t) { t.querySelectorAll("[tabindex]").forEach((e) => { const a = e.getAttribute("tabindex"); a && parseInt(a, 10) > 0 && (e.tagName === "A" || e.tagName === "BUTTON" || e.getAttribute("role") === "button" || e.getAttribute("role") === "link" || e.classList.contains("linkAnnotation") ? e.setAttribute("tabindex", "0") : e.removeAttribute("tabindex")); }); } function D(t) { t.querySelectorAll("a").forEach((e) => { const a = e.textContent && e.textContent.trim().length > 0, s = e.getAttribute("aria-label"); if (!a && !s) { const o = e.getAttribute("href") || ""; let i; if (!o || o.startsWith("#") || o.startsWith("javascript:")) i = "Link to page in document"; else try { i = `External link to ${new URL(o, window.location.href).hostname.replace("www.", "")}`; } catch { i = "External link"; } e.setAttribute("aria-label", i); } }); } function S(t) { var o; let n = t; for (; n.tagName !== "SECTION"; ) n = n.parentElement; const e = n.getAttribute("aria-haspopup") === "dialog", a = n.classList.contains("popupAnnotation"); if (!e && !a) return; let s = n.id || n.getAttribute("aria-controls"); if (s || (s = (o = n.firstChild) == null ? void 0 : o.id), s && n.parentElement) { const i = n.parentElement.querySelector(`[aria-controls="${s}"]`); i && new MutationObserver((r, g) => { const m = document.querySelectorAll(".popupDate"); m.length > 0 && (m.forEach((b) => { if (b.innerHTML) return !1; const h = b.getAttribute("data-l10n-args"); if (h) { const { date: N, time: A, dateObj: y } = JSON.parse(h); if (N && A) b.textContent = `${N}, ${A}`; else if (y) { const I = new Date(y); b.textContent = I.toLocaleString(); } } }), g.disconnect()); }).observe(i, { childList: !0, subtree: !0 }); } } function d(t, n, e) { const a = []; if (e) for (const s of e) s[t] === n && a.push(s); return a; } function u(t, n) { return { type: t, data: n }; } async function k(t, n) { if (t.dest) if (typeof t.dest == "string") { const e = await E(n, t.dest); return u(l.INTERNAL_LINK, { referencedPage: e.pageIndex, offset: null }); } else { const e = await n.getPageIndex(t.dest[0]); return u(l.INTERNAL_LINK, { referencedPage: e, offset: { left: t.dest[2], bottom: t.dest[3] } }); } else if (t.url || t.unsafeUrl) return u(l.LINK, { url: t.url ?? t.unsafeUrl, unsafeUrl: t.unsafeUrl }); } function T(t) { for (const n of t.getElementsByTagName("span")) { let e = n.textContent; const a = JSON.parse(n.dataset.l10nArgs ?? "{}"); if (e) for (const s in a) e = e.replace(`{{${s}}}`, a[s]); n.textContent = e; } } function C(t) { return u(l.FILE_ATTACHMENT, t.file); } function f(t, n) { switch (t.type) { case "textarea": case "text": return u(l.FORM_TEXT, { fieldName: t.name, value: t.value }); case "select-one": case "select-multiple": const e = []; for (const s of t.options) e.push({ value: s.value, label: s.label }); const a = []; for (const s of t.selectedOptions) a.push({ value: s.value, label: s.label }); return u(l.FORM_SELECT, { fieldName: t.name, value: a, options: e }); case "checkbox": return u(l.FORM_CHECKBOX, { fieldName: t.name, checked: t.checked }); case "radio": return u(l.FORM_RADIO, { fieldName: t.name, ...n }); case "button": return u(l.FORM_BUTTON, { fieldName: t.name, ...n }); } } async function q(t, n, e) { var s; let a = t.target.parentNode; if (a.tagName === "DIV" && (a = a.firstChild), S(a), typeof a.className != "object") if (a.className === "linkAnnotation" && t.type === "click") { const o = (s = a.dataset) == null ? void 0 : s.annotationId; if (o) { const i = d("id", o, e); if (i.length) return await k(i[0], n); } } else if ( /* annotation.className.includes('popupAnnotation') || */ a.className.includes("textAnnotation") ) T(a); else if (a.className.includes("fileAttachmentAnnotation")) { T(a); const o = a.dataset.annotationId; if (o && t.type === "dblclick") return C(d("id", o, e)[0]); } else if (a.className.includes("textWidgetAnnotation") && t.type === "input") { let o = a.getElementsByTagName("input")[0]; return o || (o = a.getElementsByTagName("textarea")[0]), f(o); } else { if (a.className.includes("choiceWidgetAnnotation") && t.type === "input") return f(a.getElementsByTagName("select")[0]); if (a.className.includes("buttonWidgetAnnotation checkBox") && t.type === "change") return f(a.getElementsByTagName("input")[0]); if (a.className.includes("buttonWidgetAnnotation radioButton") && t.type === "change") { const o = a.dataset.annotationId; if (o) { const i = d("id", o, e)[0], c = []; for (const r of d( "fieldName", i.fieldName, e )) r.buttonValue && c.push(r.buttonValue); return f(a.getElementsByTagName("input")[0], { value: i.buttonValue, defaultValue: i.fieldValue, options: c }); } } else if (a.className.includes("buttonWidgetAnnotation pushButton") && t.type === "click") { const o = a.dataset.annotationId; if (o) { const i = d("id", o, e)[0], { action: c } = i; return c && ["Print", "SaveAs"].includes(c) ? { type: l.BUTTON, data: i } : i.resetForm ? f( { name: i.fieldName, type: "button" }, { actions: i.actions, reset: !0 } ) : f( { name: i.fieldName, type: "button" }, { actions: i.actions, reset: !1 } ); } } } } export { q as annotationEventsHandler, H as annotationsEvents, P as bindLayerEvents, D as enhanceLinkAccessibility, $ as fixTabIndexAccessibility, j as handleAnnotationLink, V as handleAnnotationWidget, W as unbindLayerEvents };