UNPKG

@pdf-viewer/react

Version:

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

108 lines (107 loc) 3.75 kB
import { jsx as I } from "react/jsx-runtime"; import { createContext as M, useContext as O, useState as u, useRef as R, useEffect as f, useCallback as Z, useMemo as x } from "react"; import { ZoomLevel as J } from "../utils/types.js"; import { appConsole as _ } from "../utils/appConsole.js"; import { useInitialStateContext as j } from "./InitialStateContext.js"; import { useDocumentContext as F } from "./RPDocumentContext.js"; import { PAGE_PADDING as T } from "../utils/constants.js"; import { getZoomLevel as C } from "../utils/getZoomLevel.js"; import { useLayoutContainer as k } from "./LayoutContainerContext.js"; import { useViewModeContext as q } from "./ViewModeContext.js"; import { useRotationContext as B } from "./RotationContext.js"; import { useGlobalCurrentPage as K } from "./GlobalCurrentPage.js"; import { useDebounce as Q } from "../utils/hooks/useDebounce.js"; import "../th_TH-d627cd51.js"; import "../utils/hooks/useLoadPdf.js"; import "pdfjs-dist"; import "../utils/getThumbnailViewport.js"; import "./ConfigContext.js"; import "./DocumentPasswordContext.js"; import "../utils/hooks/usePdfProperties.js"; import "../utils/convertPdfDate.js"; import "../utils/formatFileSize.js"; import "./EventCallbackContext.js"; const D = M({ zoomLevel: 100, currentZoom: 1, setZoomLevel: () => { } }), Le = () => { const m = O(D); return typeof m.currentZoom != "number" && _.warn("Please use this hooks inside children component of RPProvider"), m; }, Pe = ({ children: m }) => { const { initialScale: W = J.PAGE_FIT } = j(), [s, b] = u(W), { pages: l } = F(), { pagesRef: t } = k(), { viewMode: p } = q(), [H, V] = u({ width: (t == null ? void 0 : t.clientWidth) || 0, height: (t == null ? void 0 : t.clientHeight) || 0 }), h = Q(H, 100), { rotate: g } = B(), [n, z] = u(null), d = R(null), { currentPage: v } = K(), [w, y] = u(v); f(() => { let e = l.get(w); e || (e = l.get(1)); const o = e == null ? void 0 : e.page.getViewport({ scale: 1, rotation: e.defaultRotation + g }); o && JSON.stringify(o) !== JSON.stringify(d.current) && (d.current = o, z(o)); }, [l, g, w]); const c = Z(() => { if (!n) return null; const e = ((n == null ? void 0 : n.width) || 0) + 2 * T, o = (n == null ? void 0 : n.height) || 0, i = (t == null ? void 0 : t.clientWidth) || 0, r = (t == null ? void 0 : t.clientHeight) || 0; return { pageWidth: e, pageHeight: o, clientWidth: i, clientHeight: r }; }, [n, t]), L = x(() => { const e = c(); if (!h.width || !h.height || !e) return 0; if (typeof s == "number") return s / 100; const { clientWidth: o, clientHeight: i, pageWidth: r, pageHeight: a } = e; return C(s, o, i, r, a, p) / 100; }, [s, h, c, p]); f(() => { y(v); }, [s]), f(() => { const e = (i) => { i.forEach((r) => { V({ width: r.contentRect.width, height: r.contentRect.height }); }); }, o = new ResizeObserver(e); return t && o.observe(t), () => { o.disconnect(); }; }, [t]); const N = Z( (e) => { let o = 0; if (typeof e == "function") { const i = c(); if (!i) return; const { clientWidth: r, clientHeight: a, pageWidth: P, pageHeight: E } = i; o = (G) => { const A = C( G, r, a, P, E, p ); return e(A); }; } else o = e; o && b(o); }, [c] ), S = x(() => ({ zoomLevel: s, currentZoom: L, setZoomLevel: N }), [L, s]); return /* @__PURE__ */ I(D.Provider, { value: S, children: m }); }; export { D as ZoomContext, Pe as ZoomProvider, Le as useZoomContext };