From b9add6509932c903b5c14d84556ef3a6551e5414 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 5 Apr 2020 10:59:10 +0200 Subject: [PATCH] Move the initialization of "auto print" out of `PDFViewerApplication.load` Over time, with more and more API-functionality added, the `PDFViewerApplication.load` method has become quite large and complex. In an attempt to improve the current situation somewhat, this patch moves the fetching and initialization of "auto print" out into its own (private) helper method instead. --- web/app.js | 91 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/web/app.js b/web/app.js index f0e64db3c..ab26f567b 100644 --- a/web/app.js +++ b/web/app.js @@ -1185,46 +1185,8 @@ const PDFViewerApplication = { }); }); - pagesPromise.then(async () => { - const [openAction, javaScript] = await Promise.all([ - openActionPromise, - pdfDocument.getJavaScript(), - ]); - let triggerAutoPrint = false; - - if (openAction && openAction.action === "Print") { - triggerAutoPrint = true; - } - if (javaScript) { - javaScript.some(js => { - if (!js) { - // Don't warn/fallback for empty JavaScript actions. - return false; - } - console.warn("Warning: JavaScript is not supported"); - this.fallback(UNSUPPORTED_FEATURES.javaScript); - return true; - }); - - if (!triggerAutoPrint) { - // Hack to support auto printing. - for (const js of javaScript) { - if (js && AutoPrintRegExp.test(js)) { - triggerAutoPrint = true; - break; - } - } - } - } - - if (!this.supportsPrinting) { - return; - } - if (triggerAutoPrint) { - setTimeout(function() { - window.print(); - }); - } + pagesPromise.then(() => { + this._initializeAutoPrint(pdfDocument, openActionPromise); }); onePageRendered.then(() => { @@ -1240,6 +1202,55 @@ const PDFViewerApplication = { this._initializeMetadata(pdfDocument); }, + /** + * @private + */ + async _initializeAutoPrint(pdfDocument, openActionPromise) { + const [openAction, javaScript] = await Promise.all([ + openActionPromise, + pdfDocument.getJavaScript(), + ]); + + if (pdfDocument !== this.pdfDocument) { + return; // The document was closed while the auto print data resolved. + } + let triggerAutoPrint = false; + + if (openAction && openAction.action === "Print") { + triggerAutoPrint = true; + } + if (javaScript) { + javaScript.some(js => { + if (!js) { + // Don't warn/fallback for empty JavaScript actions. + return false; + } + console.warn("Warning: JavaScript is not supported"); + this.fallback(UNSUPPORTED_FEATURES.javaScript); + return true; + }); + + if (!triggerAutoPrint) { + // Hack to support auto printing. + for (const js of javaScript) { + if (js && AutoPrintRegExp.test(js)) { + triggerAutoPrint = true; + break; + } + } + } + } + + if (!this.supportsPrinting) { + return; + } + if (triggerAutoPrint) { + setTimeout(function() { + window.print(); + }); + } + }, + /** * @private */