-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/f 35 pdf preview implementation (#14)
* feat: add basic pdf viewer * feat: track current page * feat: add download button * feat: add tooltip * fix: yarn lock * feat: add dropdown * feat: remove comments * feat: change colors to grey, refactor function, center topbar, add shadow * fix: one color for both themes and arrow functions
- Loading branch information
1 parent
c6af467
commit 0a13259
Showing
10 changed files
with
1,975 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
'use client'; | ||
|
||
import 'react-pdf/dist/esm/Page/TextLayer.css'; | ||
import 'react-pdf/dist/esm/Page/AnnotationLayer.css'; | ||
|
||
import { Button } from '@nextui-org/button'; | ||
import { Chip } from '@nextui-org/chip'; | ||
import { Dropdown, DropdownItem, DropdownMenu, DropdownTrigger } from '@nextui-org/react'; | ||
import { Tooltip } from '@nextui-org/tooltip'; | ||
import { useEffect, useState } from 'react'; | ||
import { Document, Page, pdfjs } from 'react-pdf'; | ||
|
||
import PdfViewerProps from '@/domain/props/PdfViewerProps'; | ||
import PdfViewerOperations from '@/operations/pdf/PdfViewerOperations'; | ||
|
||
pdfjs.GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url).toString(); | ||
|
||
export function PdfViewer({ onTooltipClick, file, onDownloadPdf, onDownloadJson, onDownloadCsv }: PdfViewerProps) { | ||
const [totalPages, setTotalPages] = useState<number>(); | ||
const [pageNumber, setPageNumber] = useState(1); | ||
const [selectionText, setSelectionText] = useState<string | null>(null); | ||
const [tooltipPosition, setTooltipPosition] = useState<{ top: number; left: number } | null>(null); | ||
|
||
const handleDocumentScroll = (): void => { | ||
PdfViewerOperations.handleDocumentScroll(document, setPageNumber, pageNumber); | ||
}; | ||
|
||
const onDocumentLoadSuccess = ({ numPages }: { numPages: number }): void => { | ||
setTotalPages(numPages); | ||
window.addEventListener('scroll', handleDocumentScroll); | ||
}; | ||
|
||
const onSelectStart = (): void => { | ||
setSelectionText(null); | ||
}; | ||
|
||
const onSelectEnd = (): void => { | ||
const activeSelection = document.getSelection(); | ||
const text = activeSelection?.toString(); | ||
|
||
if (!activeSelection || !text) { | ||
setSelectionText(null); | ||
return; | ||
} | ||
|
||
setSelectionText(text); | ||
|
||
const rect = activeSelection.getRangeAt(0).getBoundingClientRect(); | ||
|
||
setTooltipPosition({ | ||
top: rect.top + window.scrollY - 10, | ||
left: rect.left + rect.width / 2, | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
document.addEventListener('selectstart', onSelectStart); | ||
document.addEventListener('mouseup', onSelectEnd); | ||
|
||
return () => { | ||
document.removeEventListener('selectstart', onSelectStart); | ||
document.removeEventListener('mouseup', onSelectEnd); | ||
window.removeEventListener('scroll', handleDocumentScroll); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div className="min-h-screen flex flex-col items-center justify-start relative"> | ||
{/* Top Bar */} | ||
<div className="bg-surfaceGrey shadow-md w-full py-4 flex justify-between items-center px-6 sticky top-0 z-10"> | ||
<h1 className="text-lg font-semibold">Preview</h1> | ||
<Chip className="absolute left-1/2 transform -translate-x-1/2" color="secondary" size="md"> | ||
<p className="text-sm text-black"> | ||
{pageNumber} / {totalPages} | ||
</p> | ||
</Chip> | ||
<Dropdown> | ||
<DropdownTrigger> | ||
<Button className="text-black" color="secondary"> | ||
Download As | ||
</Button> | ||
</DropdownTrigger> | ||
<DropdownMenu color="secondary"> | ||
<DropdownItem onClick={onDownloadPdf}>Pdf</DropdownItem> | ||
<DropdownItem onClick={onDownloadJson}>Json</DropdownItem> | ||
<DropdownItem onClick={onDownloadCsv}>Csv</DropdownItem> | ||
</DropdownMenu> | ||
</Dropdown> | ||
</div> | ||
|
||
{/* PDF Viewer */} | ||
<div className="w-full h-full bg-surfaceGrey flex-grow flex justify-center items-center pb-10 z-0"> | ||
<div className="max-w-3xl"> | ||
<Document file={file} onLoadSuccess={onDocumentLoadSuccess} className="flex-col items-center mx-auto"> | ||
{Array.from(new Array(totalPages), (_, index) => ( | ||
<Page canvasBackground="transparent" key={`page_${index + 1}`} pageNumber={index + 1} /> | ||
))} | ||
</Document> | ||
</div> | ||
</div> | ||
|
||
{/* Tooltip */} | ||
{tooltipPosition && selectionText && ( | ||
<div className="absolute z-20 p-2 rounded-lg" style={{ top: tooltipPosition.top, left: tooltipPosition.left }}> | ||
<Tooltip | ||
color="primary" | ||
showArrow | ||
isOpen | ||
content={ | ||
<Button size="sm" color="primary" onClick={() => onTooltipClick(selectionText)}> | ||
Ask AI | ||
</Button> | ||
} | ||
> | ||
<div /> | ||
</Tooltip> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default interface PdfViewerProps { | ||
onTooltipClick: (selectionText: string) => void; | ||
onDownloadPdf: () => void; | ||
onDownloadJson: () => void; | ||
onDownloadCsv: () => void; | ||
file: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export default class PdfViewerOperations { | ||
static handleDocumentScroll( | ||
document: Document, | ||
updatePageNumber: (pageNumber: number) => void, | ||
currentPageNumber: number | ||
): void { | ||
const pages = document.querySelectorAll('.react-pdf__Page'); | ||
let currentPage = currentPageNumber; | ||
|
||
pages.forEach((page, index) => { | ||
const rect = page.getBoundingClientRect(); | ||
const pageTop = rect.top; | ||
const pageBottom = rect.bottom; | ||
const viewportMidpoint = window.innerHeight / 2; | ||
|
||
if (pageTop < viewportMidpoint && pageBottom > viewportMidpoint) { | ||
currentPage = index + 1; | ||
} | ||
}); | ||
|
||
updatePageNumber(currentPage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
|
||
/* PdfViewer.css */ | ||
canvas.react-pdf__Page__canvas { | ||
margin-bottom: 50px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.