286286<script setup lang="ts">
287287import type { DropdownMenuItem } from ' @nuxt/ui'
288288import type { RouteLocationRaw } from ' vue-router'
289- import type { PDFDocumentLoadingTask , PDFDocumentProxy , PDFPageProxy , PageViewport } from ' pdfjs-dist'
289+ import type { PDFDocumentProxy , PDFPageProxy , PageViewport } from ' pdfjs-dist'
290290import type { TextItem , TextMarkedContent } from ' pdfjs-dist/types/src/display/api'
291291import type { ReaderLeftSidebarTab } from ' ./ReaderLeftSidebar.props'
292292import { ANNOTATION_TEXT_MAX_LENGTH } from ' ~~/shared/constants/annotations'
@@ -387,9 +387,6 @@ const scaleMenuItems = computed<DropdownMenuItem[]>(() => {
387387})
388388
389389const pdfDocument = shallowRef <PDFDocumentProxy >()
390- // Tracked separately from pdfDocument so an in-flight getDocument() can be
391- // canceled on reload/unmount; destroying the task also destroys the document.
392- const pdfLoadingTask = shallowRef <PDFDocumentLoadingTask >()
393390const textContentCache = new Map <number , Awaited <ReturnType <PDFPageProxy [' getTextContent' ]>>>()
394391const pageSearchTextCache = new Map <number , { raw: string , lower: string }>()
395392const renderQueue = ref <(() => Promise <void >)[]>([])
@@ -610,8 +607,7 @@ onBeforeUnmount(() => {
610607 renderQueue .value = []
611608 textContentCache .clear ()
612609 pageSearchTextCache .clear ()
613- pdfLoadingTask .value ?.destroy ().catch (() => {})
614- pdfLoadingTask .value = undefined
610+ pdfDocument .value ?.destroy ()
615611 pdfDocument .value = undefined
616612})
617613
@@ -676,24 +672,20 @@ async function loadPDF() {
676672 return
677673 }
678674
679- pdfLoadingTask .value ?.destroy ().catch (() => {})
680- pdfDocument .value = undefined
675+ if (pdfDocument .value ) {
676+ pdfDocument .value .destroy ()
677+ pdfDocument .value = undefined
678+ }
681679
682- let loadingTask: PDFDocumentLoadingTask | undefined
683680 try {
684- loadingTask = pdfjsLib .value .getDocument ({
681+ const loadingTask = pdfjsLib .value .getDocument ({
685682 data: props .pdfBuffer ,
686683 wasmUrl: ` https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjsLib .value .version }/wasm/ ` ,
687684 cMapUrl: ` https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjsLib .value .version }/cmaps/ ` ,
688685 cMapPacked: true ,
689686 })
690- pdfLoadingTask .value = loadingTask
691-
692- const doc = await loadingTask .promise
693- // Superseded by a newer load or unmount while awaiting.
694- if (pdfLoadingTask .value !== loadingTask ) return
695687
696- pdfDocument .value = doc
688+ pdfDocument .value = await loadingTask . promise
697689 totalPages .value = pdfDocument .value .numPages
698690
699691 outlineItems .value = []
@@ -706,8 +698,6 @@ async function loadPDF() {
706698 renderPages ()
707699 }
708700 catch (error ) {
709- // Destroying a superseded task rejects its promise; not a real error.
710- if (loadingTask && pdfLoadingTask .value !== loadingTask ) return
711701 emit (' error' , error as Error )
712702 }
713703}
@@ -922,13 +912,19 @@ async function renderPageToCanvas(
922912 const page = await pdfDocument .value .getPage (pageNum )
923913 const viewport = page .getViewport ({ scale: scale .value })
924914
915+ const context = canvas .getContext (' 2d' )
916+ if (! context ) {
917+ page .cleanup ()
918+ return
919+ }
920+
925921 canvas .height = viewport .height * pixelRatio .value
926922 canvas .width = viewport .width * pixelRatio .value
927923 canvas .style .width = ` ${viewport .width }px `
928924 canvas .style .height = ` ${viewport .height }px `
929925
930926 await page .render ({
931- canvas ,
927+ canvasContext: context ,
932928 transform: pixelRatio .value !== 1
933929 ? [pixelRatio .value , 0 , 0 , pixelRatio .value , 0 , 0 ]
934930 : undefined ,
0 commit comments