2024-07-29 08:29:59 -04:00
|
|
|
import {
|
|
|
|
memo,
|
|
|
|
MouseEventHandler,
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from 'react'
|
2024-01-08 07:08:00 -05:00
|
|
|
import { debounce, throttle } from 'lodash'
|
2024-05-29 10:17:37 -04:00
|
|
|
import PdfViewerControlsToolbar from './pdf-viewer-controls-toolbar'
|
2021-09-30 07:29:25 -04:00
|
|
|
import { useProjectContext } from '../../../shared/context/project-context'
|
|
|
|
import usePersistedState from '../../../shared/hooks/use-persisted-state'
|
|
|
|
import { buildHighlightElement } from '../util/highlights'
|
|
|
|
import PDFJSWrapper from '../util/pdf-js-wrapper'
|
2021-10-06 04:33:24 -04:00
|
|
|
import withErrorBoundary from '../../../infrastructure/error-boundary'
|
2022-08-09 09:48:59 -04:00
|
|
|
import PdfPreviewErrorBoundaryFallback from './pdf-preview-error-boundary-fallback'
|
2022-03-31 07:22:36 -04:00
|
|
|
import { useDetachCompileContext as useCompileContext } from '../../../shared/context/detach-compile-context'
|
2022-05-23 07:57:30 -04:00
|
|
|
import { captureException } from '../../../infrastructure/error-reporter'
|
2023-08-22 09:33:19 -04:00
|
|
|
import * as eventTracking from '../../../infrastructure/event-tracking'
|
2022-07-20 04:15:41 -04:00
|
|
|
import { getPdfCachingMetrics } from '../util/metrics'
|
2023-09-27 05:45:49 -04:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2023-09-28 05:56:45 -04:00
|
|
|
import { usePdfPreviewContext } from '@/features/pdf-preview/components/pdf-preview-provider'
|
2024-06-14 08:14:23 -04:00
|
|
|
import usePresentationMode from '../hooks/use-presentation-mode'
|
2024-07-04 06:01:53 -04:00
|
|
|
import useMouseWheelZoom from '../hooks/use-mouse-wheel-zoom'
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2024-06-03 05:22:27 -04:00
|
|
|
type PdfJsViewerProps = {
|
|
|
|
url: string
|
|
|
|
pdfFile: Record<string, any>
|
|
|
|
}
|
|
|
|
|
|
|
|
function PdfJsViewer({ url, pdfFile }: PdfJsViewerProps) {
|
2021-09-30 07:29:25 -04:00
|
|
|
const { _id: projectId } = useProjectContext()
|
|
|
|
|
2022-11-28 06:57:54 -05:00
|
|
|
const { setError, firstRenderDone, highlights, position, setPosition } =
|
|
|
|
useCompileContext()
|
2021-10-15 05:52:07 -04:00
|
|
|
|
2023-09-28 05:56:45 -04:00
|
|
|
const { setLoadingError } = usePdfPreviewContext()
|
|
|
|
|
2021-09-30 07:29:25 -04:00
|
|
|
// state values persisted in localStorage to restore on load
|
|
|
|
const [scale, setScale] = usePersistedState(
|
|
|
|
`pdf-viewer-scale:${projectId}`,
|
|
|
|
'page-width'
|
|
|
|
)
|
|
|
|
|
2024-05-29 10:17:37 -04:00
|
|
|
// rawScale is different from scale as it is always a number.
|
|
|
|
// This is relevant when scale is e.g. 'page-width'.
|
2024-06-07 04:35:09 -04:00
|
|
|
const [rawScale, setRawScale] = useState<number | null>(null)
|
|
|
|
const [page, setPage] = useState<number | null>(null)
|
|
|
|
const [totalPages, setTotalPages] = useState<number | null>(null)
|
2024-05-29 10:17:37 -04:00
|
|
|
|
2021-09-30 07:29:25 -04:00
|
|
|
// local state values
|
2024-06-03 05:22:27 -04:00
|
|
|
const [pdfJsWrapper, setPdfJsWrapper] = useState<PDFJSWrapper | null>()
|
2021-09-30 07:29:25 -04:00
|
|
|
const [initialised, setInitialised] = useState(false)
|
|
|
|
|
2024-05-29 10:17:37 -04:00
|
|
|
const handlePageChange = useCallback(
|
2024-06-14 08:14:23 -04:00
|
|
|
(newPage: number) => {
|
|
|
|
if (!totalPages || newPage < 1 || newPage > totalPages) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-29 10:17:37 -04:00
|
|
|
setPage(newPage)
|
|
|
|
if (pdfJsWrapper?.viewer) {
|
|
|
|
pdfJsWrapper.viewer.currentPageNumber = newPage
|
|
|
|
}
|
|
|
|
},
|
2024-06-14 08:14:23 -04:00
|
|
|
[pdfJsWrapper, setPage, totalPages]
|
2024-05-29 10:17:37 -04:00
|
|
|
)
|
|
|
|
|
2021-09-30 07:29:25 -04:00
|
|
|
// create the viewer when the container is mounted
|
2022-05-23 07:57:30 -04:00
|
|
|
const handleContainer = useCallback(
|
|
|
|
parent => {
|
|
|
|
if (parent) {
|
|
|
|
const wrapper = new PDFJSWrapper(parent.firstChild)
|
|
|
|
wrapper
|
|
|
|
.init()
|
|
|
|
.then(() => {
|
|
|
|
setPdfJsWrapper(wrapper)
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2023-09-28 05:56:45 -04:00
|
|
|
setLoadingError(true)
|
2022-05-23 07:57:30 -04:00
|
|
|
captureException(error)
|
|
|
|
})
|
2022-03-18 06:27:29 -04:00
|
|
|
|
2022-05-23 07:57:30 -04:00
|
|
|
return () => {
|
|
|
|
setPdfJsWrapper(null)
|
|
|
|
wrapper.destroy()
|
|
|
|
}
|
2022-03-18 06:27:29 -04:00
|
|
|
}
|
2022-05-23 07:57:30 -04:00
|
|
|
},
|
2023-09-28 05:56:45 -04:00
|
|
|
[setLoadingError]
|
2022-05-23 07:57:30 -04:00
|
|
|
)
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-07-18 06:24:31 -04:00
|
|
|
const [startFetch, setStartFetch] = useState(0)
|
2022-07-08 04:15:05 -04:00
|
|
|
|
2022-07-18 06:24:31 -04:00
|
|
|
// listen for events and trigger rendering.
|
|
|
|
// Do everything in one effect to mitigate de-sync between events.
|
2021-09-30 07:29:25 -04:00
|
|
|
useEffect(() => {
|
2022-07-18 06:24:31 -04:00
|
|
|
if (!pdfJsWrapper || !firstRenderDone) return
|
|
|
|
|
2024-06-03 05:22:27 -04:00
|
|
|
let timePDFFetched: number
|
|
|
|
let timePDFRendered: number
|
2022-07-18 06:24:31 -04:00
|
|
|
const submitLatencies = () => {
|
|
|
|
if (!timePDFFetched) {
|
|
|
|
// The pagerendered event was attached after pagesinit fired. :/
|
|
|
|
return
|
2021-10-20 04:45:10 -04:00
|
|
|
}
|
2022-07-18 06:24:31 -04:00
|
|
|
|
|
|
|
const latencyFetch = Math.ceil(timePDFFetched - startFetch)
|
|
|
|
let latencyRender
|
|
|
|
if (timePDFRendered) {
|
|
|
|
// The renderer does not yield in case the browser tab is hidden.
|
|
|
|
// It will yield when the browser tab is visible again.
|
|
|
|
// This will skew our performance metrics for rendering!
|
|
|
|
// We are omitting the render time in case we detect this state.
|
|
|
|
latencyRender = Math.ceil(timePDFRendered - timePDFFetched)
|
|
|
|
}
|
2022-07-20 04:15:41 -04:00
|
|
|
firstRenderDone({
|
|
|
|
latencyFetch,
|
|
|
|
latencyRender,
|
|
|
|
// Let the pdfCachingMetrics round trip to account for pdf-detach.
|
|
|
|
pdfCachingMetrics: getPdfCachingMetrics(),
|
|
|
|
})
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
2022-07-08 04:15:05 -04:00
|
|
|
|
2022-07-18 06:24:31 -04:00
|
|
|
const handlePagesinit = () => {
|
|
|
|
setInitialised(true)
|
|
|
|
timePDFFetched = performance.now()
|
|
|
|
if (document.hidden) {
|
|
|
|
// Rendering does not start in case we are hidden. See comment above.
|
|
|
|
submitLatencies()
|
2022-07-08 04:15:05 -04:00
|
|
|
}
|
|
|
|
}
|
2022-07-18 06:24:31 -04:00
|
|
|
|
|
|
|
const handleRendered = () => {
|
|
|
|
if (!document.hidden) {
|
|
|
|
// The render time is not accurate in case we are hidden. See above.
|
|
|
|
timePDFRendered = performance.now()
|
|
|
|
}
|
|
|
|
submitLatencies()
|
|
|
|
|
|
|
|
// Only get the times for the first page.
|
|
|
|
pdfJsWrapper.eventBus.off('pagerendered', handleRendered)
|
|
|
|
}
|
|
|
|
|
2024-05-29 10:17:37 -04:00
|
|
|
const handleRenderedInitialPageNumber = () => {
|
|
|
|
setPage(pdfJsWrapper.viewer.currentPageNumber)
|
2024-06-03 05:20:57 -04:00
|
|
|
|
|
|
|
// Only need to set the initial page number once.
|
|
|
|
pdfJsWrapper.eventBus.off('pagerendered', handleRenderedInitialPageNumber)
|
2024-05-29 10:17:37 -04:00
|
|
|
}
|
|
|
|
|
2024-06-07 04:35:09 -04:00
|
|
|
const handleScaleChanged = (scale: { scale: number }) => {
|
|
|
|
setRawScale(scale.scale)
|
2024-05-29 10:17:37 -04:00
|
|
|
}
|
|
|
|
|
2022-07-18 06:24:31 -04:00
|
|
|
// `pagesinit` fires when the data for rendering the first page is ready.
|
|
|
|
pdfJsWrapper.eventBus.on('pagesinit', handlePagesinit)
|
|
|
|
// `pagerendered` fires when a page was actually rendered.
|
|
|
|
pdfJsWrapper.eventBus.on('pagerendered', handleRendered)
|
2024-05-29 10:17:37 -04:00
|
|
|
// Once a page has been rendered we can set the initial current page number.
|
|
|
|
pdfJsWrapper.eventBus.on('pagerendered', handleRenderedInitialPageNumber)
|
2024-06-07 04:35:09 -04:00
|
|
|
pdfJsWrapper.eventBus.on('scalechanging', handleScaleChanged)
|
|
|
|
|
2022-07-18 06:24:31 -04:00
|
|
|
return () => {
|
|
|
|
pdfJsWrapper.eventBus.off('pagesinit', handlePagesinit)
|
|
|
|
pdfJsWrapper.eventBus.off('pagerendered', handleRendered)
|
2024-05-29 10:17:37 -04:00
|
|
|
pdfJsWrapper.eventBus.off('pagerendered', handleRenderedInitialPageNumber)
|
2024-06-07 04:35:09 -04:00
|
|
|
pdfJsWrapper.eventBus.off('scalechanging', handleScaleChanged)
|
2022-07-18 06:24:31 -04:00
|
|
|
}
|
|
|
|
}, [pdfJsWrapper, firstRenderDone, startFetch])
|
2021-09-30 07:29:25 -04:00
|
|
|
|
|
|
|
// load the PDF document from the URL
|
|
|
|
useEffect(() => {
|
|
|
|
if (pdfJsWrapper && url) {
|
|
|
|
setInitialised(false)
|
|
|
|
setError(undefined)
|
2022-07-18 06:24:31 -04:00
|
|
|
setStartFetch(performance.now())
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-08-01 07:16:29 -04:00
|
|
|
const abortController = new AbortController()
|
2024-06-03 05:22:27 -04:00
|
|
|
const handleFetchError = (err: Error) => {
|
2022-08-01 07:16:29 -04:00
|
|
|
if (abortController.signal.aborted) return
|
|
|
|
// The error is already logged at the call-site with additional context.
|
2022-11-07 09:23:53 -05:00
|
|
|
if (err instanceof pdfJsWrapper.PDFJS.MissingPDFException) {
|
2023-06-27 09:37:53 -04:00
|
|
|
setError('rendering-error-expected')
|
2022-11-07 09:23:53 -05:00
|
|
|
} else {
|
|
|
|
setError('rendering-error')
|
|
|
|
}
|
2022-08-01 07:16:29 -04:00
|
|
|
}
|
|
|
|
pdfJsWrapper
|
|
|
|
.loadDocument({ url, pdfFile, abortController, handleFetchError })
|
2024-05-29 10:17:37 -04:00
|
|
|
.then(doc => {
|
|
|
|
setTotalPages(doc.numPages)
|
|
|
|
})
|
2022-08-01 07:16:29 -04:00
|
|
|
.catch(error => {
|
|
|
|
if (abortController.signal.aborted) return
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.error(error)
|
2023-06-27 09:37:53 -04:00
|
|
|
setError('rendering-error')
|
2022-08-01 07:16:29 -04:00
|
|
|
})
|
|
|
|
return () => {
|
|
|
|
abortController.abort()
|
|
|
|
pdfJsWrapper.abortDocumentLoading()
|
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
2022-07-18 06:24:31 -04:00
|
|
|
}, [pdfJsWrapper, url, pdfFile, setError, setStartFetch])
|
2021-09-30 07:29:25 -04:00
|
|
|
|
|
|
|
// listen for scroll events
|
|
|
|
useEffect(() => {
|
2024-06-03 05:22:27 -04:00
|
|
|
let storePositionTimer: number
|
2021-10-15 05:39:56 -04:00
|
|
|
|
2021-10-06 04:33:11 -04:00
|
|
|
if (initialised && pdfJsWrapper) {
|
2022-01-10 08:58:31 -05:00
|
|
|
if (!pdfJsWrapper.isVisible()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-30 07:29:25 -04:00
|
|
|
// store the scroll position in localStorage, for the synctex button
|
|
|
|
const storePosition = debounce(pdfViewer => {
|
|
|
|
// set position for "sync to code" button
|
|
|
|
try {
|
|
|
|
setPosition(pdfViewer.currentPosition)
|
|
|
|
} catch (error) {
|
2023-09-27 05:45:49 -04:00
|
|
|
// debugConsole.error(error)
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
}, 500)
|
|
|
|
|
2021-10-15 05:39:56 -04:00
|
|
|
storePositionTimer = window.setTimeout(() => {
|
|
|
|
storePosition(pdfJsWrapper)
|
|
|
|
}, 100)
|
2021-09-30 07:29:25 -04:00
|
|
|
|
|
|
|
const scrollListener = () => {
|
|
|
|
storePosition(pdfJsWrapper)
|
2024-05-29 10:17:37 -04:00
|
|
|
setPage(pdfJsWrapper.viewer.currentPageNumber)
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pdfJsWrapper.container.addEventListener('scroll', scrollListener)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
pdfJsWrapper.container.removeEventListener('scroll', scrollListener)
|
2021-10-15 05:39:56 -04:00
|
|
|
if (storePositionTimer) {
|
|
|
|
window.clearTimeout(storePositionTimer)
|
|
|
|
}
|
|
|
|
storePosition.cancel()
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
}
|
2021-10-15 05:39:56 -04:00
|
|
|
}, [setPosition, pdfJsWrapper, initialised])
|
2021-09-30 07:29:25 -04:00
|
|
|
|
|
|
|
// listen for double-click events
|
|
|
|
useEffect(() => {
|
|
|
|
if (pdfJsWrapper) {
|
2024-06-03 05:22:27 -04:00
|
|
|
const handleTextlayerrendered = (textLayer: any) => {
|
2023-05-17 08:57:36 -04:00
|
|
|
// handle both versions for backwards-compatibility
|
|
|
|
const textLayerDiv =
|
|
|
|
textLayer.source.textLayerDiv ?? textLayer.source.textLayer.div
|
|
|
|
|
2024-07-29 08:29:59 -04:00
|
|
|
if (!textLayerDiv.dataset.listeningForDoubleClick) {
|
|
|
|
textLayerDiv.dataset.listeningForDoubleClick = true
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2024-07-29 08:29:59 -04:00
|
|
|
const doubleClickListener: MouseEventHandler = event => {
|
2022-11-07 05:51:07 -05:00
|
|
|
const clickPosition = pdfJsWrapper.clickPosition(
|
|
|
|
event,
|
2024-07-29 08:29:59 -04:00
|
|
|
textLayerDiv.closest('.page').querySelector('canvas'),
|
|
|
|
textLayer.pageNumber - 1
|
2022-08-04 05:39:31 -04:00
|
|
|
)
|
2022-11-07 05:51:07 -05:00
|
|
|
|
|
|
|
if (clickPosition) {
|
2023-08-22 09:33:19 -04:00
|
|
|
eventTracking.sendMB('jump-to-location', {
|
|
|
|
direction: 'pdf-location-in-code',
|
|
|
|
method: 'double-click',
|
|
|
|
})
|
|
|
|
|
2022-11-07 05:51:07 -05:00
|
|
|
window.dispatchEvent(
|
|
|
|
new CustomEvent('synctex:sync-to-position', {
|
|
|
|
detail: clickPosition,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2022-08-04 05:39:31 -04:00
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2024-07-29 08:29:59 -04:00
|
|
|
textLayerDiv.addEventListener('dblclick', doubleClickListener)
|
2022-11-07 05:51:07 -05:00
|
|
|
}
|
2021-10-06 04:33:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pdfJsWrapper.eventBus.on('textlayerrendered', handleTextlayerrendered)
|
|
|
|
return () =>
|
|
|
|
pdfJsWrapper.eventBus.off('textlayerrendered', handleTextlayerrendered)
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
}, [pdfJsWrapper])
|
|
|
|
|
2022-01-07 11:32:46 -05:00
|
|
|
const positionRef = useRef(position)
|
|
|
|
useEffect(() => {
|
|
|
|
positionRef.current = position
|
|
|
|
}, [position])
|
|
|
|
|
|
|
|
const scaleRef = useRef(scale)
|
|
|
|
useEffect(() => {
|
|
|
|
scaleRef.current = scale
|
|
|
|
}, [scale])
|
|
|
|
|
2024-05-29 10:17:37 -04:00
|
|
|
// restore the saved scale and scroll position
|
2021-09-30 07:29:25 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (initialised && pdfJsWrapper) {
|
2022-01-10 08:58:31 -05:00
|
|
|
if (!pdfJsWrapper.isVisible()) {
|
|
|
|
return
|
|
|
|
}
|
2022-01-07 11:32:46 -05:00
|
|
|
if (positionRef.current) {
|
2024-06-03 05:22:27 -04:00
|
|
|
// Typescript is incorrectly inferring the type of the scale argument to
|
|
|
|
// scrollToPosition from its default value. We can remove this ignore once
|
|
|
|
// pdfJsWrapper is converted to using tyepscript.
|
|
|
|
// @ts-ignore
|
2022-01-07 11:32:46 -05:00
|
|
|
pdfJsWrapper.scrollToPosition(positionRef.current, scaleRef.current)
|
|
|
|
} else {
|
|
|
|
pdfJsWrapper.viewer.currentScaleValue = scaleRef.current
|
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
2022-01-07 11:32:46 -05:00
|
|
|
}, [initialised, pdfJsWrapper, scaleRef, positionRef])
|
2021-09-30 07:29:25 -04:00
|
|
|
|
|
|
|
// transmit scale value to the viewer when it changes
|
|
|
|
useEffect(() => {
|
|
|
|
if (pdfJsWrapper) {
|
|
|
|
pdfJsWrapper.viewer.currentScaleValue = scale
|
|
|
|
}
|
|
|
|
}, [scale, pdfJsWrapper])
|
|
|
|
|
|
|
|
// when highlights are created, build the highlight elements
|
|
|
|
useEffect(() => {
|
2024-06-03 05:22:27 -04:00
|
|
|
const timers: number[] = []
|
|
|
|
let intersectionObserver: IntersectionObserver
|
2022-04-27 06:32:11 -04:00
|
|
|
|
2021-09-30 07:29:25 -04:00
|
|
|
if (pdfJsWrapper && highlights?.length) {
|
2022-04-27 06:32:11 -04:00
|
|
|
// watch for the highlight elements to scroll into view
|
|
|
|
intersectionObserver = new IntersectionObserver(
|
|
|
|
entries => {
|
|
|
|
for (const entry of entries) {
|
|
|
|
if (entry.isIntersecting) {
|
|
|
|
intersectionObserver.unobserve(entry.target)
|
|
|
|
|
2024-06-03 05:22:27 -04:00
|
|
|
const element = entry.target as HTMLElement
|
|
|
|
|
2022-04-27 06:32:11 -04:00
|
|
|
// fade the element in and out
|
2024-06-03 05:22:27 -04:00
|
|
|
element.style.opacity = '0.5'
|
2022-04-27 06:32:11 -04:00
|
|
|
|
|
|
|
timers.push(
|
|
|
|
window.setTimeout(() => {
|
2024-06-03 05:22:27 -04:00
|
|
|
element.style.opacity = '0'
|
2024-08-06 05:31:47 -04:00
|
|
|
}, 1100)
|
2022-04-27 06:32:11 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
threshold: 1.0, // the whole element must be visible
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-06-03 05:22:27 -04:00
|
|
|
const elements: HTMLDivElement[] = []
|
2021-10-15 05:47:45 -04:00
|
|
|
|
|
|
|
for (const highlight of highlights) {
|
|
|
|
try {
|
2022-03-18 06:27:29 -04:00
|
|
|
const element = buildHighlightElement(highlight, pdfJsWrapper)
|
2021-10-15 05:47:45 -04:00
|
|
|
elements.push(element)
|
2022-04-27 06:32:11 -04:00
|
|
|
intersectionObserver.observe(element)
|
2021-10-15 05:47:45 -04:00
|
|
|
} catch (error) {
|
|
|
|
// ignore invalid highlights
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
|
2022-04-27 06:32:11 -04:00
|
|
|
const [firstElement] = elements
|
|
|
|
|
|
|
|
if (firstElement) {
|
|
|
|
// scroll to the first highlighted element
|
2024-08-06 05:31:47 -04:00
|
|
|
// Briefly delay the scrolling after adding the element to the DOM.
|
|
|
|
timers.push(
|
|
|
|
window.setTimeout(() => {
|
|
|
|
firstElement.scrollIntoView({
|
|
|
|
block: 'center',
|
|
|
|
inline: 'start',
|
|
|
|
behavior: 'smooth',
|
|
|
|
})
|
|
|
|
}, 100)
|
|
|
|
)
|
2022-04-27 06:32:11 -04:00
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
|
|
|
|
return () => {
|
2022-04-27 06:32:11 -04:00
|
|
|
for (const timer of timers) {
|
|
|
|
window.clearTimeout(timer)
|
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
for (const element of elements) {
|
|
|
|
element.remove()
|
|
|
|
}
|
2022-04-27 06:32:11 -04:00
|
|
|
intersectionObserver?.disconnect()
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [highlights, pdfJsWrapper])
|
|
|
|
|
|
|
|
// set the scale in response to zoom option changes
|
|
|
|
const setZoom = useCallback(
|
|
|
|
zoom => {
|
|
|
|
switch (zoom) {
|
|
|
|
case 'zoom-in':
|
2022-08-04 05:40:06 -04:00
|
|
|
if (pdfJsWrapper) {
|
2024-06-07 04:35:09 -04:00
|
|
|
setScale(
|
|
|
|
`${Math.min(pdfJsWrapper.viewer.currentScale * 1.25, 9.99)}`
|
|
|
|
)
|
2022-08-04 05:40:06 -04:00
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
break
|
|
|
|
|
|
|
|
case 'zoom-out':
|
2022-08-04 05:40:06 -04:00
|
|
|
if (pdfJsWrapper) {
|
2024-06-07 04:35:09 -04:00
|
|
|
setScale(
|
|
|
|
`${Math.max(pdfJsWrapper.viewer.currentScale / 1.25, 0.1)}`
|
|
|
|
)
|
2022-08-04 05:40:06 -04:00
|
|
|
}
|
2021-09-30 07:29:25 -04:00
|
|
|
break
|
2024-05-29 10:17:37 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
setScale(zoom)
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
[pdfJsWrapper, setScale]
|
|
|
|
)
|
|
|
|
|
|
|
|
// adjust the scale when the container is resized
|
|
|
|
useEffect(() => {
|
2021-11-29 06:59:34 -05:00
|
|
|
if (pdfJsWrapper && 'ResizeObserver' in window) {
|
2024-01-08 07:08:00 -05:00
|
|
|
const resizeListener = throttle(() => {
|
2021-09-30 07:29:25 -04:00
|
|
|
pdfJsWrapper.updateOnResize()
|
2024-01-08 07:08:00 -05:00
|
|
|
}, 250)
|
2021-09-30 07:29:25 -04:00
|
|
|
|
|
|
|
const resizeObserver = new ResizeObserver(resizeListener)
|
|
|
|
resizeObserver.observe(pdfJsWrapper.container)
|
|
|
|
|
|
|
|
window.addEventListener('resize', resizeListener)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
resizeObserver.disconnect()
|
|
|
|
window.removeEventListener('resize', resizeListener)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [pdfJsWrapper])
|
|
|
|
|
2021-10-08 05:25:43 -04:00
|
|
|
const handleKeyDown = useCallback(
|
|
|
|
event => {
|
2024-08-14 11:52:37 -04:00
|
|
|
if (!initialised || !pdfJsWrapper) {
|
2021-10-08 05:25:43 -04:00
|
|
|
return
|
|
|
|
}
|
2022-11-28 06:57:54 -05:00
|
|
|
if (event.metaKey || event.ctrlKey) {
|
|
|
|
switch (event.key) {
|
2024-07-02 08:10:49 -04:00
|
|
|
case '+':
|
2022-11-28 06:57:54 -05:00
|
|
|
case '=':
|
|
|
|
event.preventDefault()
|
|
|
|
setZoom('zoom-in')
|
2024-08-14 11:52:37 -04:00
|
|
|
pdfJsWrapper.container.focus()
|
2022-11-28 06:57:54 -05:00
|
|
|
break
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
event.preventDefault()
|
|
|
|
setZoom('zoom-out')
|
2024-08-14 11:52:37 -04:00
|
|
|
pdfJsWrapper.container.focus()
|
2022-11-28 06:57:54 -05:00
|
|
|
break
|
|
|
|
|
|
|
|
case '0':
|
|
|
|
event.preventDefault()
|
2024-05-29 10:17:37 -04:00
|
|
|
setZoom('page-width')
|
2024-08-14 11:52:37 -04:00
|
|
|
pdfJsWrapper.container.focus()
|
2022-11-28 06:57:54 -05:00
|
|
|
break
|
2024-07-04 08:34:32 -04:00
|
|
|
|
|
|
|
case '9':
|
|
|
|
event.preventDefault()
|
|
|
|
setZoom('page-height')
|
2024-08-14 11:52:37 -04:00
|
|
|
pdfJsWrapper.container.focus()
|
2024-07-04 08:34:32 -04:00
|
|
|
break
|
2022-11-28 06:57:54 -05:00
|
|
|
}
|
2021-10-08 05:25:43 -04:00
|
|
|
}
|
|
|
|
},
|
2024-08-14 11:52:37 -04:00
|
|
|
[initialised, setZoom, pdfJsWrapper]
|
2021-10-08 05:25:43 -04:00
|
|
|
)
|
|
|
|
|
2024-07-04 06:01:53 -04:00
|
|
|
useMouseWheelZoom(pdfJsWrapper, setScale)
|
|
|
|
|
2024-06-14 08:14:23 -04:00
|
|
|
const requestPresentationMode = usePresentationMode(
|
|
|
|
pdfJsWrapper,
|
|
|
|
page,
|
|
|
|
handlePageChange,
|
|
|
|
scale,
|
|
|
|
setScale
|
|
|
|
)
|
|
|
|
|
2024-05-29 10:17:37 -04:00
|
|
|
// Don't render the toolbar until we have the necessary information
|
|
|
|
const toolbarInfoLoaded =
|
|
|
|
rawScale !== null && page !== null && totalPages !== null
|
|
|
|
|
2021-09-30 07:29:25 -04:00
|
|
|
/* eslint-disable jsx-a11y/no-noninteractive-tabindex */
|
2021-10-08 05:25:43 -04:00
|
|
|
/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */
|
2021-09-30 07:29:25 -04:00
|
|
|
return (
|
2024-05-30 08:37:29 -04:00
|
|
|
/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */
|
2024-05-29 10:17:37 -04:00
|
|
|
<div
|
|
|
|
className="pdfjs-viewer pdfjs-viewer-outer"
|
|
|
|
ref={handleContainer}
|
|
|
|
onKeyDown={handleKeyDown}
|
2024-05-30 08:37:29 -04:00
|
|
|
tabIndex={-1}
|
2024-05-29 10:17:37 -04:00
|
|
|
>
|
2024-08-14 11:52:37 -04:00
|
|
|
<div className="pdfjs-viewer-inner" tabIndex={0} role="tabpanel">
|
2021-09-30 07:29:25 -04:00
|
|
|
<div className="pdfViewer" />
|
|
|
|
</div>
|
2024-07-19 05:46:55 -04:00
|
|
|
{toolbarInfoLoaded && (
|
|
|
|
<PdfViewerControlsToolbar
|
|
|
|
requestPresentationMode={requestPresentationMode}
|
|
|
|
setZoom={setZoom}
|
|
|
|
rawScale={rawScale}
|
|
|
|
setPage={handlePageChange}
|
|
|
|
page={page}
|
|
|
|
totalPages={totalPages}
|
|
|
|
/>
|
|
|
|
)}
|
2021-09-30 07:29:25 -04:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-10-06 04:33:24 -04:00
|
|
|
export default withErrorBoundary(memo(PdfJsViewer), () => (
|
2022-08-09 09:48:59 -04:00
|
|
|
<PdfPreviewErrorBoundaryFallback type="pdf" />
|
2021-10-06 04:33:24 -04:00
|
|
|
))
|