@@ -49,8 +49,6 @@ const LinkTarget = {
4949class PDFLinkService {
5050 externalLinkEnabled = true ;
5151
52- #pagesRefCache = new Map ( ) ;
53-
5452 /**
5553 * @param {PDFLinkServiceOptions } options
5654 */
@@ -74,7 +72,6 @@ class PDFLinkService {
7472 setDocument ( pdfDocument , baseUrl = null ) {
7573 this . baseUrl = baseUrl ;
7674 this . pdfDocument = pdfDocument ;
77- this . #pagesRefCache. clear ( ) ;
7875 }
7976
8077 setViewer ( pdfViewer ) {
@@ -131,44 +128,53 @@ class PDFLinkService {
131128 return this . pdfDocument ? this . pdfViewer . isInPresentationMode : false ;
132129 }
133130
134- #goToDestinationHelper( rawDest , namedDest = null , explicitDest ) {
131+ /**
132+ * This method will, when available, also update the browser history.
133+ *
134+ * @param {string|Array } dest - The named, or explicit, PDF destination.
135+ */
136+ async goToDestination ( dest ) {
137+ if ( ! this . pdfDocument ) {
138+ return ;
139+ }
140+ let namedDest , explicitDest , pageNumber ;
141+ if ( typeof dest === "string" ) {
142+ namedDest = dest ;
143+ explicitDest = await this . pdfDocument . getDestination ( dest ) ;
144+ } else {
145+ namedDest = null ;
146+ explicitDest = await dest ;
147+ }
148+ if ( ! Array . isArray ( explicitDest ) ) {
149+ console . error (
150+ `goToDestination: "${ explicitDest } " is not a valid destination array, for dest="${ dest } ".`
151+ ) ;
152+ return ;
153+ }
135154 // Dest array looks like that: <page-ref> </XYZ|/FitXXX> <args..>
136- const destRef = explicitDest [ 0 ] ;
137- let pageNumber ;
155+ const [ destRef ] = explicitDest ;
138156
139- if ( typeof destRef === "object" && destRef !== null ) {
140- pageNumber = this . _cachedPageNumber ( destRef ) ;
157+ if ( destRef && typeof destRef === "object" ) {
158+ pageNumber = this . pdfDocument . cachedPageNumber ( destRef ) ;
141159
142160 if ( ! pageNumber ) {
143161 // Fetch the page reference if it's not yet available. This could
144162 // only occur during loading, before all pages have been resolved.
145- this . pdfDocument
146- . getPageIndex ( destRef )
147- . then ( pageIndex => {
148- this . cachePageRef ( pageIndex + 1 , destRef ) ;
149- this . #goToDestinationHelper( rawDest , namedDest , explicitDest ) ;
150- } )
151- . catch ( ( ) => {
152- console . error (
153- `PDFLinkService.#goToDestinationHelper: "${ destRef } " is not ` +
154- `a valid page reference, for dest="${ rawDest } ".`
155- ) ;
156- } ) ;
157- return ;
163+ try {
164+ pageNumber = ( await this . pdfDocument . getPageIndex ( destRef ) ) + 1 ;
165+ } catch {
166+ console . error (
167+ `goToDestination: "${ destRef } " is not a valid page reference, for dest="${ dest } ".`
168+ ) ;
169+ return ;
170+ }
158171 }
159172 } else if ( Number . isInteger ( destRef ) ) {
160173 pageNumber = destRef + 1 ;
161- } else {
162- console . error (
163- `PDFLinkService.#goToDestinationHelper: "${ destRef } " is not ` +
164- `a valid destination reference, for dest="${ rawDest } ".`
165- ) ;
166- return ;
167174 }
168175 if ( ! pageNumber || pageNumber < 1 || pageNumber > this . pagesCount ) {
169176 console . error (
170- `PDFLinkService.#goToDestinationHelper: "${ pageNumber } " is not ` +
171- `a valid page number, for dest="${ rawDest } ".`
177+ `goToDestination: "${ pageNumber } " is not a valid page number, for dest="${ dest } ".`
172178 ) ;
173179 return ;
174180 }
@@ -187,33 +193,6 @@ class PDFLinkService {
187193 } ) ;
188194 }
189195
190- /**
191- * This method will, when available, also update the browser history.
192- *
193- * @param {string|Array } dest - The named, or explicit, PDF destination.
194- */
195- async goToDestination ( dest ) {
196- if ( ! this . pdfDocument ) {
197- return ;
198- }
199- let namedDest , explicitDest ;
200- if ( typeof dest === "string" ) {
201- namedDest = dest ;
202- explicitDest = await this . pdfDocument . getDestination ( dest ) ;
203- } else {
204- namedDest = null ;
205- explicitDest = await dest ;
206- }
207- if ( ! Array . isArray ( explicitDest ) ) {
208- console . error (
209- `PDFLinkService.goToDestination: "${ explicitDest } " is not ` +
210- `a valid destination array, for dest="${ dest } ".`
211- ) ;
212- return ;
213- }
214- this . #goToDestinationHelper( dest , namedDest , explicitDest ) ;
215- }
216-
217196 /**
218197 * This method will, when available, also update the browser history.
219198 *
@@ -508,31 +487,6 @@ class PDFLinkService {
508487 ) ;
509488 }
510489
511- /**
512- * @param {number } pageNum - page number.
513- * @param {Object } pageRef - reference to the page.
514- */
515- cachePageRef ( pageNum , pageRef ) {
516- if ( ! pageRef ) {
517- return ;
518- }
519- const refStr =
520- pageRef . gen === 0 ? `${ pageRef . num } R` : `${ pageRef . num } R${ pageRef . gen } ` ;
521- this . #pagesRefCache. set ( refStr , pageNum ) ;
522- }
523-
524- /**
525- * @ignore
526- */
527- _cachedPageNumber ( pageRef ) {
528- if ( ! pageRef ) {
529- return null ;
530- }
531- const refStr =
532- pageRef . gen === 0 ? `${ pageRef . num } R` : `${ pageRef . num } R${ pageRef . gen } ` ;
533- return this . #pagesRefCache. get ( refStr ) || null ;
534- }
535-
536490 static #isValidExplicitDest( dest ) {
537491 if ( ! Array . isArray ( dest ) || dest . length < 2 ) {
538492 return false ;
@@ -592,12 +546,6 @@ class PDFLinkService {
592546 */
593547class SimpleLinkService extends PDFLinkService {
594548 setDocument ( pdfDocument , baseUrl = null ) { }
595-
596- /**
597- * @param {number } pageNum - page number.
598- * @param {Object } pageRef - reference to the page.
599- */
600- cachePageRef ( pageNum , pageRef ) { }
601549}
602550
603551export { LinkTarget , PDFLinkService , SimpleLinkService } ;
0 commit comments