UNPKG

@pdf-viewer/react

Version:

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

101 lines (100 loc) 3.38 kB
import { useState as E, useRef as T, useCallback as c, useEffect as i } from "react"; import { useDebounce as g } from "./useDebounce.js"; import { useZoomContext as A } from "../../contexts/ZoomContext.js"; import "react/jsx-runtime"; import "../types.js"; import "../../th_TH-d627cd51.js"; import "../appConsole.js"; import "../../contexts/InitialStateContext.js"; import "../../contexts/RPDocumentContext.js"; import "./useLoadPdf.js"; import "pdfjs-dist"; import "../getThumbnailViewport.js"; import "../../contexts/ConfigContext.js"; import "../../contexts/DocumentPasswordContext.js"; import "./usePdfProperties.js"; import "../convertPdfDate.js"; import "../formatFileSize.js"; import "../constants.js"; import "../getZoomLevel.js"; import "../../contexts/LayoutContainerContext.js"; import "../../contexts/ViewModeContext.js"; import "../../contexts/RotationContext.js"; import "../../contexts/GlobalCurrentPage.js"; import "../../contexts/EventCallbackContext.js"; const V = (o) => { const { currentZoom: L, setZoomLevel: w } = A(), [D, m] = E(0), d = g(D, 100), [s, b] = E(!1), r = T(null), u = c((e) => { m(e ? (t) => t + 3 : (t) => t - 3); }, []), a = c( (e) => { if (!e.ctrlKey) return; e.preventDefault(); const t = e.deltaY < 0; u(t); }, [u] ); i(() => { if (d) { const e = Math.round(L * 100 + d); m(0), w(e > 25 ? e : 25); } }, [d]); const p = (e, t) => { const n = e.clientX - t.clientX, l = e.clientY - t.clientY; return Math.sqrt(n * n + l * l); }, f = c( (e) => { if (!s || e.touches.length !== 2) return; const t = p(e.touches[0], e.touches[1]); r.current = t, e.preventDefault(); }, [s] ), h = c(() => { r.current = null; }, []), v = c( (e) => { if (!s || e.touches.length !== 2 || r.current === null) return; const t = p(e.touches[0], e.touches[1]), n = t - r.current, l = Math.abs(n) > 5, S = n > 0; l && (u(S), r.current = t, e.preventDefault()); }, [u, s] ), y = () => { document.activeElement instanceof HTMLElement && document.activeElement.blur(); }; i(() => { if (s) { document.body.style.touchAction = "pan-x pan-y"; return; } document.body.style.touchAction = ""; }, [s]), i(() => { const e = () => { const t = document.activeElement, n = !!(t != null && t.closest('[data-rp="container"]')); b(n); }; return window.addEventListener("focusin", e), window.addEventListener("focusout", e), e(), () => { window.removeEventListener("focusin", e), window.removeEventListener("focusout", e); }; }, []), i(() => { if (!o) return; const e = (t) => { o.contains(t.target) || y(); }; return document.addEventListener("scroll", e), () => { document.removeEventListener("scroll", e); }; }, [o]), i(() => { if (o) return o.addEventListener("wheel", a, { passive: !1 }), o.addEventListener("touchstart", f, { passive: !1 }), o.addEventListener("touchmove", v, { passive: !1 }), o.addEventListener("touchend", h, { passive: !1 }), () => { o.removeEventListener("wheel", a), o.removeEventListener("touchstart", f), o.removeEventListener("touchmove", v), o.removeEventListener("touchend", h); }; }, [o, a, f, v, h]); }; export { V as usePinch };