[resumes][fix] fix zoomed resume left side cut off

pull/385/head
Wu Peirong 2 years ago
parent d38f997701
commit 966cf2e8d6

@ -1,4 +1,5 @@
import { useState } from 'react'; import clsx from 'clsx';
import { useEffect, useState } from 'react';
import { Document, Page, pdfjs } from 'react-pdf'; import { Document, Page, pdfjs } from 'react-pdf';
import type { PDFDocumentProxy } from 'react-pdf/node_modules/pdfjs-dist'; import type { PDFDocumentProxy } from 'react-pdf/node_modules/pdfjs-dist';
import { import {
@ -18,14 +19,30 @@ type Props = Readonly<{
export default function ResumePdf({ url }: Props) { export default function ResumePdf({ url }: Props) {
const [numPages, setNumPages] = useState(0); const [numPages, setNumPages] = useState(0);
const [pageNumber, setPageNumber] = useState(1); const [pageNumber, setPageNumber] = useState(1);
const [scale, setScale] = useState(1); const [pageWidth, setPageWidth] = useState(750);
const [componentWidth, setComponentWidth] = useState(780);
const onPdfLoadSuccess = (pdf: PDFDocumentProxy) => { const onPdfLoadSuccess = (pdf: PDFDocumentProxy) => {
setNumPages(pdf.numPages); setNumPages(pdf.numPages);
}; };
useEffect(() => {
const onPageResize = () => {
setComponentWidth(
document.querySelector('#pdfView')?.getBoundingClientRect().width ??
780,
);
};
window.addEventListener('resize', onPageResize);
return () => {
window.removeEventListener('resize', onPageResize);
};
}, []);
return ( return (
<div> <div id="pdfView">
<div className="group relative"> <div className="group relative">
<Document <Document
className="flex h-[calc(100vh-17rem)] flex-row justify-center overflow-auto" className="flex h-[calc(100vh-17rem)] flex-row justify-center overflow-auto"
@ -33,25 +50,34 @@ export default function ResumePdf({ url }: Props) {
loading={<Spinner display="block" size="lg" />} loading={<Spinner display="block" size="lg" />}
noData="" noData=""
onLoadSuccess={onPdfLoadSuccess}> onLoadSuccess={onPdfLoadSuccess}>
<Page pageNumber={pageNumber} scale={scale} width={750} /> <div
style={{
paddingLeft: clsx(
pageWidth > componentWidth
? `${pageWidth - componentWidth}px`
: '',
),
}}>
<Page pageNumber={pageNumber} width={pageWidth} />
</div>
<div className="absolute top-2 right-5 hidden hover:block group-hover:block"> <div className="absolute top-2 right-5 hidden hover:block group-hover:block">
<Button <Button
className="rounded-r-none focus:ring-0 focus:ring-offset-0" className="rounded-r-none focus:ring-0 focus:ring-offset-0"
disabled={scale === 0.5} disabled={pageWidth === 450}
icon={MagnifyingGlassMinusIcon} icon={MagnifyingGlassMinusIcon}
isLabelHidden={true} isLabelHidden={true}
label="Zoom Out" label="Zoom Out"
variant="tertiary" variant="tertiary"
onClick={() => setScale(scale - 0.25)} onClick={() => setPageWidth(pageWidth - 150)}
/> />
<Button <Button
className="rounded-l-none focus:ring-0 focus:ring-offset-0" className="rounded-l-none focus:ring-0 focus:ring-offset-0"
disabled={scale === 1.5} disabled={pageWidth === 1050}
icon={MagnifyingGlassPlusIcon} icon={MagnifyingGlassPlusIcon}
isLabelHidden={true} isLabelHidden={true}
label="Zoom In" label="Zoom In"
variant="tertiary" variant="tertiary"
onClick={() => setScale(scale + 0.25)} onClick={() => setPageWidth(pageWidth + 150)}
/> />
</div> </div>
</Document> </Document>

Loading…
Cancel
Save