-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/f 35 pdf preview implementation #14
Merged
marinovl7
merged 10 commits into
main
from
feature/f-35-pdf-preview-library-research-and-implementation
Nov 8, 2024
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
758aa21
feat: add basic pdf viewer
bohdangarchu 8e2cfc7
feat: track current page
bohdangarchu 3d935c5
feat: add download button
bohdangarchu ba4e947
feat: add tooltip
bohdangarchu fd429ee
Merge branch 'main' into feature/f-35-pdf-preview-library-research-an…
bohdangarchu 835e6a9
fix: yarn lock
bohdangarchu b502875
feat: add dropdown
bohdangarchu 42a2c4b
feat: remove comments
bohdangarchu 2d65c24
feat: change colors to grey, refactor function, center topbar, add sh…
bohdangarchu 5fd1c14
fix: one color for both themes and arrow functions
bohdangarchu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||
|
||
function handleDocumentScroll(): void { | ||
PdfViewerOperations.handleDocumentScroll(document, setPageNumber, pageNumber); | ||
} | ||
|
||
function onDocumentLoadSuccess({ numPages }: { numPages: number }): void { | ||
setTotalPages(numPages); | ||
window.addEventListener('scroll', handleDocumentScroll); | ||
} | ||
|
||
function onSelectStart(): void { | ||
setSelectionText(null); | ||
} | ||
|
||
function 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-lightGrey dark:bg-darkGrey 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-lightGrey dark:bg-darkGrey 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ module.exports = { | |
colors: { | ||
brand: '#157DBC', | ||
brandHover: '#0F6396', | ||
lightGrey: '#B0B0B0', | ||
darkGrey: '#444444', | ||
}, | ||
}, | ||
}, | ||
|
@@ -26,7 +28,7 @@ module.exports = { | |
light: { | ||
colors: { | ||
primary: { DEFAULT: '#157DBC', foreground: '#F5F5F5' }, | ||
secondary: '#EEEEEE', | ||
secondary: '#666666', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this corresponds to our secondary color used in figma designs |
||
foreground: '#333333', | ||
background: '#F5F5F5', | ||
divider: '#157DBC', | ||
|
@@ -42,7 +44,7 @@ module.exports = { | |
dark: { | ||
colors: { | ||
primary: '#157DBC', | ||
secondary: '#424242', | ||
secondary: '#B0B0B0', | ||
foreground: '#E0E0E0', | ||
background: '#121212', | ||
divider: '#157DBC', | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to use css here to add padding between pages