diff --git a/docbeaver/src/Components/PdfView.tsx b/docbeaver/src/Components/PdfView.tsx index ff2da24..58d097e 100644 --- a/docbeaver/src/Components/PdfView.tsx +++ b/docbeaver/src/Components/PdfView.tsx @@ -1,11 +1,11 @@ import { Document, Page, pdfjs } from "react-pdf"; -import { useEffect, useState } from "react"; +import { useEffect, useRef, useState } from "react"; import axios from "../utils/axios"; import useMedia from "../utils/useMedia"; pdfjs.GlobalWorkerOptions.workerSrc = new URL( "pdfjs-dist/build/pdf.worker.min.mjs", - import.meta.url + import.meta.url, ).toString(); interface PdfViewProps { @@ -17,40 +17,58 @@ interface PdfViewProps { export default function PdfView({ viewPdfId, access_token, - setViewPdfId + setViewPdfId, }: PdfViewProps) { const [file, setFile] = useState(null); const [blobStorage, setBlobStorage] = useState(null); const [page, setPage] = useState(1); const [numPages, setNumPages] = useState(null); + const [isFullScreen, setIsFullScreen] = useState(false); const isMobile = useMedia("(max-width: 767px)"); + const [width, setWidth] = useState( + isMobile ? window.innerWidth - 60 : window.innerWidth - 300, + ); + const [scale, setScale] = useState(1); + const pdfContainerRef = useRef(null); + const [isNavOpen, setIsNavOpen] = useState(false); useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { switch (event.key) { case "ArrowLeft": - if (page > 1) { - setPage(page - 1); - } + handlePrevPage(); break; case "ArrowRight": - if (page < numPages!) { - setPage(page + 1); - } + handleNextPage(); break; case "Escape": setViewPdfId(null); break; + case "f": + handleFullscreen(); + break; + case "ArrowUp": + scrollUp(); + break; + case "ArrowDown": + scrollDown(); + break; default: break; } }; + const handleFullscreenChange = () => { + setIsFullScreen(!!document.fullscreenElement); + }; + window.addEventListener("keydown", handleKeyDown); + document.addEventListener("fullscreenchange", handleFullscreenChange); // Clean up the event listener when the component is unmounted return () => { window.removeEventListener("keydown", handleKeyDown); + document.removeEventListener("fullscreenchange", handleFullscreenChange); }; }); @@ -62,17 +80,17 @@ export default function PdfView({ `/documents/file/${viewPdfId}`, { headers: { - Authorization: access_token + Authorization: access_token, }, - responseType: "arraybuffer" - } + responseType: "arraybuffer", + }, ); const blob = new Blob([response.data], { type: "application/pdf" }); const url = URL.createObjectURL(blob); setBlobStorage(blob); setFile(url); - const arrayBuffer = await response.data; + const arrayBuffer = response.data; const typedArray = new Uint8Array(arrayBuffer); const loadingTask = pdfjs.getDocument(typedArray); const pdf = await loadingTask.promise; @@ -87,25 +105,158 @@ export default function PdfView({ getFile(); }, [viewPdfId, access_token, blobStorage]); + const handleFullscreen = () => { + const pdfContainer = document.getElementById("pdfContainer"); + if (document.fullscreenElement) { + document.exitFullscreen(); + setIsFullScreen(false); + } else if (pdfContainer) { + pdfContainer.requestFullscreen(); + setIsFullScreen(true); + } + }; + + const handleZoomIn = () => { + if (scale < 2) { + setScale(scale + 0.2); + } + }; + + const handleZoomOut = () => { + if (scale > 0.4) { + setScale(scale - 0.2); + } + }; + + const handleNextPage = () => { + if (page < numPages!) { + setPage(page + 1); + } + }; + + const handlePrevPage = () => { + if (page > 1) { + setPage(page - 1); + } + }; + + const scrollUp = () => { + if (pdfContainerRef.current) { + pdfContainerRef.current.scrollBy({ top: -200, behavior: "smooth" }); + } + }; + + const scrollDown = () => { + if (pdfContainerRef.current) { + pdfContainerRef.current.scrollBy({ top: 200, behavior: "smooth" }); + } + }; + + useEffect(() => { + if (isFullScreen) { + setWidth(window.innerWidth); + } else if (isMobile) { + setWidth(window.innerWidth - 60); + } else { + setWidth(window.innerWidth - 300); + } + }, [isFullScreen, isMobile]); + useEffect(() => { if (blobStorage) { const url = URL.createObjectURL(blobStorage); setFile(url); return () => URL.revokeObjectURL(url); } - console.log(window.innerHeight, window.innerWidth); }, [blobStorage]); return ( - - - +
+ + +
+
+ + + +
+
+
); }