@@ -108,91 +108,127 @@ class PDFLinkService {
108
108
}
109
109
110
110
/**
111
- * @param { string|Array } dest - The named, or explicit, PDF destination.
111
+ * @deprecated
112
112
*/
113
113
navigateTo ( dest ) {
114
- const goToDestination = ( { namedDest, explicitDest } ) => {
115
- // Dest array looks like that: <page-ref> </XYZ|/FitXXX> <args..>
116
- const destRef = explicitDest [ 0 ] ;
117
- let pageNumber ;
118
-
119
- if ( destRef instanceof Object ) {
120
- pageNumber = this . _cachedPageNumber ( destRef ) ;
121
-
122
- if ( pageNumber === null ) {
123
- // Fetch the page reference if it's not yet available. This could
124
- // only occur during loading, before all pages have been resolved.
125
- this . pdfDocument
126
- . getPageIndex ( destRef )
127
- . then ( pageIndex => {
128
- this . cachePageRef ( pageIndex + 1 , destRef ) ;
129
- goToDestination ( { namedDest, explicitDest } ) ;
130
- } )
131
- . catch ( ( ) => {
132
- console . error (
133
- `PDFLinkService.navigateTo: "${ destRef } " is not ` +
134
- `a valid page reference, for dest="${ dest } ".`
135
- ) ;
136
- } ) ;
137
- return ;
138
- }
139
- } else if ( Number . isInteger ( destRef ) ) {
140
- pageNumber = destRef + 1 ;
141
- } else {
142
- console . error (
143
- `PDFLinkService.navigateTo: "${ destRef } " is not ` +
144
- `a valid destination reference, for dest="${ dest } ".`
145
- ) ;
146
- return ;
147
- }
148
- if ( ! pageNumber || pageNumber < 1 || pageNumber > this . pagesCount ) {
149
- console . error (
150
- `PDFLinkService.navigateTo: "${ pageNumber } " is not ` +
151
- `a valid page number, for dest="${ dest } ".`
152
- ) ;
153
- return ;
154
- }
155
-
156
- if ( this . pdfHistory ) {
157
- // Update the browser history before scrolling the new destination into
158
- // view, to be able to accurately capture the current document position.
159
- this . pdfHistory . pushCurrentPosition ( ) ;
160
- this . pdfHistory . push ( { namedDest, explicitDest, pageNumber } ) ;
161
- }
114
+ console . error (
115
+ "Deprecated method: `navigateTo`, use `goToDestination` instead."
116
+ ) ;
117
+ this . goToDestination ( dest ) ;
118
+ }
162
119
163
- this . pdfViewer . scrollPageIntoView ( {
164
- pageNumber,
165
- destArray : explicitDest ,
166
- ignoreDestinationZoom : this . _ignoreDestinationZoom ,
167
- } ) ;
168
- } ;
169
-
170
- new Promise ( ( resolve , reject ) => {
171
- if ( typeof dest === "string" ) {
172
- this . pdfDocument . getDestination ( dest ) . then ( destArray => {
173
- resolve ( {
174
- namedDest : dest ,
175
- explicitDest : destArray ,
120
+ /**
121
+ * @private
122
+ */
123
+ _goToDestinationHelper ( rawDest , namedDest = null , explicitDest ) {
124
+ // Dest array looks like that: <page-ref> </XYZ|/FitXXX> <args..>
125
+ const destRef = explicitDest [ 0 ] ;
126
+ let pageNumber ;
127
+
128
+ if ( destRef instanceof Object ) {
129
+ pageNumber = this . _cachedPageNumber ( destRef ) ;
130
+
131
+ if ( pageNumber === null ) {
132
+ // Fetch the page reference if it's not yet available. This could
133
+ // only occur during loading, before all pages have been resolved.
134
+ this . pdfDocument
135
+ . getPageIndex ( destRef )
136
+ . then ( pageIndex => {
137
+ this . cachePageRef ( pageIndex + 1 , destRef ) ;
138
+ this . _goToDestinationHelper ( rawDest , namedDest , explicitDest ) ;
139
+ } )
140
+ . catch ( ( ) => {
141
+ console . error (
142
+ `PDFLinkService._goToDestinationHelper: "${ destRef } " is not ` +
143
+ `a valid page reference, for dest="${ rawDest } ".`
144
+ ) ;
176
145
} ) ;
177
- } ) ;
178
- return ;
179
- }
180
- resolve ( {
181
- namedDest : "" ,
182
- explicitDest : dest ,
183
- } ) ;
184
- } ) . then ( data => {
185
- if ( ! Array . isArray ( data . explicitDest ) ) {
186
- console . error (
187
- `PDFLinkService.navigateTo: "${ data . explicitDest } " is` +
188
- ` not a valid destination array, for dest="${ dest } ".`
189
- ) ;
190
146
return ;
191
147
}
192
- goToDestination ( data ) ;
148
+ } else if ( Number . isInteger ( destRef ) ) {
149
+ pageNumber = destRef + 1 ;
150
+ } else {
151
+ console . error (
152
+ `PDFLinkService._goToDestinationHelper: "${ destRef } " is not ` +
153
+ `a valid destination reference, for dest="${ rawDest } ".`
154
+ ) ;
155
+ return ;
156
+ }
157
+ if ( ! pageNumber || pageNumber < 1 || pageNumber > this . pagesCount ) {
158
+ console . error (
159
+ `PDFLinkService._goToDestinationHelper: "${ pageNumber } " is not ` +
160
+ `a valid page number, for dest="${ rawDest } ".`
161
+ ) ;
162
+ return ;
163
+ }
164
+
165
+ if ( this . pdfHistory ) {
166
+ // Update the browser history before scrolling the new destination into
167
+ // view, to be able to accurately capture the current document position.
168
+ this . pdfHistory . pushCurrentPosition ( ) ;
169
+ this . pdfHistory . push ( { namedDest, explicitDest, pageNumber } ) ;
170
+ }
171
+
172
+ this . pdfViewer . scrollPageIntoView ( {
173
+ pageNumber,
174
+ destArray : explicitDest ,
175
+ ignoreDestinationZoom : this . _ignoreDestinationZoom ,
193
176
} ) ;
194
177
}
195
178
179
+ /**
180
+ * This method will, when available, also update the browser history.
181
+ *
182
+ * @param {string|Array } dest - The named, or explicit, PDF destination.
183
+ */
184
+ async goToDestination ( dest ) {
185
+ let namedDest , explicitDest ;
186
+ if ( typeof dest === "string" ) {
187
+ namedDest = dest ;
188
+ explicitDest = await this . pdfDocument . getDestination ( dest ) ;
189
+ } else {
190
+ namedDest = null ;
191
+ explicitDest = await dest ;
192
+ }
193
+ if ( ! Array . isArray ( explicitDest ) ) {
194
+ console . error (
195
+ `PDFLinkService.goToDestination: "${ explicitDest } " is not ` +
196
+ `a valid destination array, for dest="${ dest } ".`
197
+ ) ;
198
+ return ;
199
+ }
200
+ this . _goToDestinationHelper ( dest , namedDest , explicitDest ) ;
201
+ }
202
+
203
+ /**
204
+ * This method will, when available, also update the browser history.
205
+ *
206
+ * @param {number } pageNumber - The page number.
207
+ */
208
+ goToPage ( pageNumber ) {
209
+ if (
210
+ ! (
211
+ Number . isInteger ( pageNumber ) &&
212
+ pageNumber > 0 &&
213
+ pageNumber <= this . pagesCount
214
+ )
215
+ ) {
216
+ console . error (
217
+ `PDFLinkService.goToPage: "${ pageNumber } " is not a valid page number.`
218
+ ) ;
219
+ return ;
220
+ }
221
+
222
+ if ( this . pdfHistory ) {
223
+ // Update the browser history before scrolling the new page into view,
224
+ // to be able to accurately capture the current document position.
225
+ this . pdfHistory . pushCurrentPosition ( ) ;
226
+ this . pdfHistory . pushPage ( pageNumber ) ;
227
+ }
228
+
229
+ this . pdfViewer . scrollPageIntoView ( { pageNumber } ) ;
230
+ }
231
+
196
232
/**
197
233
* @param {string|Array } dest - The PDF destination object.
198
234
* @returns {string } The hyperlink to the PDF object.
@@ -307,7 +343,7 @@ class PDFLinkService {
307
343
// Ensure that this parameter is *always* handled last, in order to
308
344
// guarantee that it won't be overridden (e.g. by the "page" parameter).
309
345
if ( "nameddest" in params ) {
310
- this . navigateTo ( params . nameddest ) ;
346
+ this . goToDestination ( params . nameddest ) ;
311
347
}
312
348
} else {
313
349
// Named (or explicit) destination.
@@ -323,7 +359,7 @@ class PDFLinkService {
323
359
} catch ( ex ) { }
324
360
325
361
if ( typeof dest === "string" || isValidExplicitDestination ( dest ) ) {
326
- this . navigateTo ( dest ) ;
362
+ this . goToDestination ( dest ) ;
327
363
return ;
328
364
}
329
365
console . error (
@@ -394,6 +430,9 @@ class PDFLinkService {
394
430
this . _pagesRefCache [ refStr ] = pageNum ;
395
431
}
396
432
433
+ /**
434
+ * @private
435
+ */
397
436
_cachedPageNumber ( pageRef ) {
398
437
const refStr =
399
438
pageRef . gen === 0 ? `${ pageRef . num } R` : `${ pageRef . num } R${ pageRef . gen } ` ;
@@ -510,9 +549,14 @@ class SimpleLinkService {
510
549
set rotation ( value ) { }
511
550
512
551
/**
513
- * @param dest - The PDF destination object.
552
+ * @param {string|Array } dest - The named, or explicit, PDF destination.
553
+ */
554
+ async goToDestination ( dest ) { }
555
+
556
+ /**
557
+ * @param {number } pageNumber - The page number.
514
558
*/
515
- navigateTo ( dest ) { }
559
+ goToPage ( pageNumber ) { }
516
560
517
561
/**
518
562
* @param dest - The PDF destination object.
0 commit comments