UNPKG

@pdf-viewer/react

Version:

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

48 lines (47 loc) 1.94 kB
import { jsx as C } from "react/jsx-runtime"; import { createContext as P, useContext as L, useState as y, useMemo as c, useEffect as S } from "react"; import { ZoomLevel as V } from "../utils/types.js"; import { appConsole as b } from "../utils/appConsole.js"; import { useInitialStateContext as A } from "./InitialStateContext.js"; import { useDocumentContext as D } from "./RPDocumentContext.js"; import { PAGE_PADDING as p } from "../utils/constants.js"; import { getZoomLevel as E } from "../utils/getZoomLevel.js"; import { useLayoutContainer as G } from "./LayoutContainerContext.js"; import { useViewModeContext as H } from "./ViewModeContext.js"; const d = P({ zoomLevel: 100, currentZoom: 1, setZoomLevel: () => { } }), T = () => { const n = L(d); return typeof n.currentZoom != "number" && b.warn("Please use this hooks inside children component of RPProvider"), n; }, q = ({ children: n }) => { const { initialScale: r = V.PAGE_FIT, initialPage: u = 1, initialRotation: l = 0 } = A(), [i, a] = y(typeof r == "number" ? r : 100), { pages: s } = D(), { pagesRef: e } = G(), { viewMode: h } = H(), f = c(() => i / 100, [i]), t = c(() => { let o = s.get(u); return o || (o = s.get(1)), o == null ? void 0 : o.page.getViewport({ scale: 1, rotation: l + o.defaultRotation }); }, [s, u, l]), m = c(() => { if (!t) return 0; const o = ((t == null ? void 0 : t.width) || 0) + 2 * p, Z = ((t == null ? void 0 : t.height) || 0) + 2 * p, g = (e == null ? void 0 : e.clientWidth) || 0, v = (e == null ? void 0 : e.clientHeight) || 0; return E(r, g, v, o, Z, h); }, [t, r, e]); S(() => { typeof m == "number" && m && a(m); }, [m]); const x = c(() => ({ zoomLevel: i, currentZoom: f, setZoomLevel: a }), [f, i]); return /* @__PURE__ */ C(d.Provider, { value: x, children: n }); }; export { d as ZoomContext, q as ZoomProvider, T as useZoomContext };