@pdf-viewer/react
Version:
The PDF Viewer component for React and Next.js
48 lines (47 loc) • 1.92 kB
JavaScript
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 h = P({
zoomLevel: 100,
currentZoom: 1,
setZoomLevel: () => {
}
}), T = () => {
const n = L(h);
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: o } = G(), { viewMode: d } = H(), f = c(() => i / 100, [i]), t = c(() => {
let e = s.get(u);
return e || (e = s.get(1)), e == null ? void 0 : e.page.getViewport({ scale: 1, rotation: l });
}, [s, u, l]), m = c(() => {
if (!t)
return 0;
const e = ((t == null ? void 0 : t.width) || 0) + 2 * p, Z = ((t == null ? void 0 : t.height) || 0) + 2 * p, g = (o == null ? void 0 : o.clientWidth) || 0, v = (o == null ? void 0 : o.clientHeight) || 0;
return E(r, g, v, e, Z, d);
}, [t, r, o]);
S(() => {
typeof m == "number" && m && a(m);
}, [m]);
const x = c(() => ({
zoomLevel: i,
currentZoom: f,
setZoomLevel: a
}), [f, i]);
return /* @__PURE__ */ C(h.Provider, { value: x, children: n });
};
export {
h as ZoomContext,
q as ZoomProvider,
T as useZoomContext
};