UNPKG

@pdf-viewer/react

Version:

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

95 lines (94 loc) 3.33 kB
import { jsx as A } from "react/jsx-runtime"; import { createContext as I, useContext as M, useState as u, useRef as O, useEffect as p, useCallback as Z, useMemo as x } from "react"; import { ZoomLevel as R } from "../utils/types.js"; import { appConsole as J } from "../utils/appConsole.js"; import { useInitialStateContext as _ } from "./InitialStateContext.js"; import { useDocumentContext as j } from "./RPDocumentContext.js"; import { PAGE_PADDING as F } from "../utils/constants.js"; import { getZoomLevel as C } from "../utils/getZoomLevel.js"; import { useLayoutContainer as T } from "./LayoutContainerContext.js"; import { useViewModeContext as k } from "./ViewModeContext.js"; import { useRotationContext as q } from "./RotationContext.js"; import { useGlobalCurrentPage as B } from "./GlobalCurrentPage.js"; import { useDebounce as K } from "../utils/hooks/useDebounce.js"; const D = I({ zoomLevel: 100, currentZoom: 1, setZoomLevel: () => { } }), me = () => { const c = M(D); return typeof c.currentZoom != "number" && J.warn("Please use this hooks inside children component of RPProvider"), c; }, ue = ({ children: c }) => { const { initialScale: W = R.PAGE_FIT } = _(), [n, b] = u(W), { pages: l } = j(), { pagesRef: t } = T(), { viewMode: h } = k(), [H, V] = u({ width: (t == null ? void 0 : t.clientWidth) || 0, height: (t == null ? void 0 : t.clientHeight) || 0 }), a = K(H, 100), { rotate: g } = q(), [i, z] = u(null), d = O(null), { currentPage: v } = B(), [w, y] = u(v); p(() => { 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 m = Z(() => { if (!i) return null; const e = ((i == null ? void 0 : i.width) || 0) + 2 * F, o = (i == null ? void 0 : i.height) || 0, r = (t == null ? void 0 : t.clientWidth) || 0, s = (t == null ? void 0 : t.clientHeight) || 0; return { pageWidth: e, pageHeight: o, clientWidth: r, clientHeight: s }; }, [i, t]), L = x(() => { const e = m(); if (!a.width || !a.height || !e) return 0; if (typeof n == "number") return n / 100; const { clientWidth: o, clientHeight: r, pageWidth: s, pageHeight: f } = e; return C(n, o, r, s, f, h) / 100; }, [n, a, m, h]); p(() => { y(v); }, [n]), p(() => { const e = (r) => { r.forEach((s) => { V({ width: s.contentRect.width, height: s.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 r = m(); if (!r) return; const { clientWidth: s, clientHeight: f, pageWidth: P, pageHeight: E } = r, G = C( n, s, f, P, E, h ); o = e(G); } else o = e; o && b(o); }, [m] ), S = x(() => ({ zoomLevel: n, currentZoom: L, setZoomLevel: N }), [L, n]); return /* @__PURE__ */ A(D.Provider, { value: S, children: c }); }; export { D as ZoomContext, ue as ZoomProvider, me as useZoomContext };