Skip to content

Commit

Permalink
Fix issues with PDF Viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
Podginator committed Jan 25, 2025
1 parent 6457aa4 commit 2aa59fd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default function PdfArticleContainer(props: PdfArticleContainerProps) {
// Uses the existing mechanism to hide the reader toolbar from pspdfkit
const updateReaderSettings = () => {
const show = localStorage.getItem('reader-show-pdf-tool-bar')
const showBar = show ? JSON.parse(show) == true : false
const showBar = show ? JSON.parse(show) == true : true
setShowToolbar(showBar)

const latestPage = localStorage.getItem('reader-remember-latest-page')
Expand Down
15 changes: 12 additions & 3 deletions packages/web/components/templates/article/pdf.js/PdfViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ export default function PdfViewer(props: PdfArticleContainerProps) {
[]
)

const removeAllHighlightsNotAtScale = (scaleId: string) => {
const highlights = document.querySelectorAll(`.canvasWrapper .highlight:not(.scale${scaleId})`)
highlights.forEach((highlight) => highlight.remove())
}

const addHighlightToPage = useCallback(
(page: PDFPageView, highlight: Highlight) => {
(page: PDFPageView, highlight: Highlight,) => {
const scale = page.viewport.scale
const element = document.createElement('div')
element.id = highlight.id
Expand All @@ -68,7 +73,7 @@ export default function PdfViewer(props: PdfArticleContainerProps) {

boundingRects.forEach((rect: number[]) => {
const svgElement = document.createElement('svg')
svgElement.className = 'highlight'
svgElement.className = `highlight scale${scale.toString().replace('.','')}`
svgElement.setAttribute('viewBox', '0 0 1 1')
svgElement.style.borderRadius = '0px'
svgElement.style.background = colorMap[color]
Expand Down Expand Up @@ -352,7 +357,8 @@ export default function PdfViewer(props: PdfArticleContainerProps) {
useEffect(() => {
const render = (data: { source: PDFPageView }) => {
const page = data.source
if (page) {
if (page && highlights) {
removeAllHighlightsNotAtScale(page.viewport.scale.toString().replace('.',''))
highlights
?.filter((it) => it.highlightPositionAnchorIndex == page.id)
.forEach((it) => addHighlightToPage(page as PDFPageView, it))
Expand All @@ -361,6 +367,8 @@ export default function PdfViewer(props: PdfArticleContainerProps) {
const setPage = (e: { pageNumber: number }) =>
setCurrentPageNum(e.pageNumber)



if (props.eventBus && props.pdfViewer) {
props.eventBus.on('textlayerrendered', render)
props.eventBus.on('pagechanging', setPage)
Expand All @@ -370,6 +378,7 @@ export default function PdfViewer(props: PdfArticleContainerProps) {
if (props.eventBus) {
props.eventBus.off('textlayerrendered', render)
props.eventBus.off('pagechanging', setPage)

}
}
}, [props.eventBus, props.pdfViewer, highlights, addHighlightToPage])
Expand Down
2 changes: 1 addition & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"next": "^13.5.6",
"node-html-markdown": "^1.3.0",
"papaparse": "^5.4.1",
"pdfjs-dist": "^4.9.155",
"pdfjs-dist": "4.10.38",
"pspdfkit": "^2023.4.6",
"re-resizable": "^6.9.11",
"react": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/web/public/pdfjs-dist/pdf.worker.min.mjs

Large diffs are not rendered by default.

0 comments on commit 2aa59fd

Please sign in to comment.