@@ -46,19 +46,13 @@ const PDFViewerApplication = {
4646 * @returns {Promise } - Returns the promise, which is resolved when document
4747 * is opened.
4848 */
49- open ( params ) {
49+ async open ( params ) {
5050 if ( this . pdfLoadingTask ) {
51- // We need to destroy already opened document
52- return this . close ( ) . then (
53- function ( ) {
54- // ... and repeat the open() call.
55- return this . open ( params ) ;
56- } . bind ( this )
57- ) ;
51+ // We need to destroy already opened document.
52+ await this . close ( ) ;
5853 }
5954
60- const url = params . url ;
61- const self = this ;
55+ const { url } = params ;
6256 this . setTitleUsingUrl ( url ) ;
6357
6458 // Loading document.
@@ -70,24 +64,22 @@ const PDFViewerApplication = {
7064 } ) ;
7165 this . pdfLoadingTask = loadingTask ;
7266
73- loadingTask . onProgress = function ( progressData ) {
74- self . progress ( progressData . loaded / progressData . total ) ;
75- } ;
67+ loadingTask . onProgress = evt => this . progress ( evt . percent ) ;
7668
7769 return loadingTask . promise . then (
78- function ( pdfDocument ) {
70+ pdfDocument => {
7971 // Document loaded, specifying document for the viewer.
80- self . pdfDocument = pdfDocument ;
81- self . pdfViewer . setDocument ( pdfDocument ) ;
82- self . pdfLinkService . setDocument ( pdfDocument ) ;
83- self . pdfHistory . initialize ( {
72+ this . pdfDocument = pdfDocument ;
73+ this . pdfViewer . setDocument ( pdfDocument ) ;
74+ this . pdfLinkService . setDocument ( pdfDocument ) ;
75+ this . pdfHistory . initialize ( {
8476 fingerprint : pdfDocument . fingerprints [ 0 ] ,
8577 } ) ;
8678
87- self . loadingBar . hide ( ) ;
88- self . setTitleUsingMetadata ( pdfDocument ) ;
79+ this . loadingBar . hide ( ) ;
80+ this . setTitleUsingMetadata ( pdfDocument ) ;
8981 } ,
90- function ( reason ) {
82+ reason => {
9183 let key = "pdfjs-loading-error" ;
9284 if ( reason instanceof pdfjsLib . InvalidPDFException ) {
9385 key = "pdfjs-invalid-file-error" ;
@@ -96,10 +88,10 @@ const PDFViewerApplication = {
9688 ? "pdfjs-missing-file-error"
9789 : "pdfjs-unexpected-response-error" ;
9890 }
99- self . l10n . get ( key ) . then ( msg => {
100- self . error ( msg , { message : reason ? .message } ) ;
91+ this . l10n . get ( key ) . then ( msg => {
92+ this . error ( msg , { message : reason . message } ) ;
10193 } ) ;
102- self . loadingBar . hide ( ) ;
94+ this . loadingBar . hide ( ) ;
10395 }
10496 ) ;
10597 } ,
@@ -109,9 +101,9 @@ const PDFViewerApplication = {
109101 * @returns {Promise } - Returns the promise, which is resolved when all
110102 * destruction is completed.
111103 */
112- close ( ) {
104+ async close ( ) {
113105 if ( ! this . pdfLoadingTask ) {
114- return Promise . resolve ( ) ;
106+ return ;
115107 }
116108
117109 const promise = this . pdfLoadingTask . destroy ( ) ;
@@ -128,7 +120,7 @@ const PDFViewerApplication = {
128120 }
129121 }
130122
131- return promise ;
123+ await promise ;
132124 } ,
133125
134126 get loadingBar ( ) {
@@ -152,48 +144,36 @@ const PDFViewerApplication = {
152144 this . setTitle ( title ) ;
153145 } ,
154146
155- setTitleUsingMetadata ( pdfDocument ) {
156- const self = this ;
157- pdfDocument . getMetadata ( ) . then ( function ( data ) {
158- const info = data . info ,
159- metadata = data . metadata ;
160- self . documentInfo = info ;
161- self . metadata = metadata ;
162-
163- // Provides some basic debug information
164- console . log (
165- "PDF " +
166- pdfDocument . fingerprints [ 0 ] +
167- " [" +
168- info . PDFFormatVersion +
169- " " +
170- ( info . Producer || "-" ) . trim ( ) +
171- " / " +
172- ( info . Creator || "-" ) . trim ( ) +
173- "]" +
174- " (PDF.js: " +
175- ( pdfjsLib . version || "-" ) +
176- ")"
177- ) ;
178-
179- let pdfTitle ;
180- if ( metadata && metadata . has ( "dc:title" ) ) {
181- const title = metadata . get ( "dc:title" ) ;
182- // Ghostscript sometimes returns 'Untitled', so prevent setting the
183- // title to 'Untitled.
184- if ( title !== "Untitled" ) {
185- pdfTitle = title ;
186- }
187- }
147+ async setTitleUsingMetadata ( pdfDocument ) {
148+ const { info, metadata } = await pdfDocument . getMetadata ( ) ;
149+ this . documentInfo = info ;
150+ this . metadata = metadata ;
151+
152+ // Provides some basic debug information
153+ console . log (
154+ `PDF ${ pdfDocument . fingerprints [ 0 ] } [${ info . PDFFormatVersion } ` +
155+ `${ ( metadata ?. get ( "pdf:producer" ) || info . Producer || "-" ) . trim ( ) } / ` +
156+ `${ ( metadata ?. get ( "xmp:creatortool" ) || info . Creator || "-" ) . trim ( ) } ` +
157+ `] (PDF.js: ${ pdfjsLib . version || "?" } [${ pdfjsLib . build || "?" } ])`
158+ ) ;
188159
189- if ( ! pdfTitle && info && info . Title ) {
190- pdfTitle = info . Title ;
160+ let pdfTitle ;
161+ if ( metadata && metadata . has ( "dc:title" ) ) {
162+ const title = metadata . get ( "dc:title" ) ;
163+ // Ghostscript sometimes returns 'Untitled', so prevent setting the
164+ // title to 'Untitled.
165+ if ( title !== "Untitled" ) {
166+ pdfTitle = title ;
191167 }
168+ }
192169
193- if ( pdfTitle ) {
194- self . setTitle ( pdfTitle + " - " + document . title ) ;
195- }
196- } ) ;
170+ if ( ! pdfTitle && info && info . Title ) {
171+ pdfTitle = info . Title ;
172+ }
173+
174+ if ( pdfTitle ) {
175+ this . setTitle ( pdfTitle + " - " + document . title ) ;
176+ }
197177 } ,
198178
199179 setTitle : function pdfViewSetTitle ( title ) {
@@ -223,8 +203,7 @@ const PDFViewerApplication = {
223203 console . error ( `${ message } \n\n${ moreInfoText . join ( "\n" ) } ` ) ;
224204 } ,
225205
226- progress : function pdfViewProgress ( level ) {
227- const percent = Math . round ( level * 100 ) ;
206+ progress ( percent ) {
228207 // Updating the bar if value increases.
229208 if ( percent > this . loadingBar . percent || isNaN ( percent ) ) {
230209 this . loadingBar . percent = percent ;
0 commit comments