From 89ce326d12f6a6101039ea12e66ad01535b1b606 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 30 Sep 2020 15:02:11 +0200 Subject: [PATCH] Re-factor how printing is triggered in the default viewer This adds a new `PDFViewerApplication.triggerPrinting` method, which takes care of checking that printing is actually supported before calling `window.print`, to remove the need to duplicate that code in multiple places. Also, removes the `PDFViewerApplication.printing` getter since it's not really necessary any more. --- web/app.js | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/web/app.js b/web/app.js index 9559e1fef..df777d4f6 100644 --- a/web/app.js +++ b/web/app.js @@ -593,10 +593,6 @@ const PDFViewerApplication = { this.pdfViewer.currentPageNumber = val; }, - get printing() { - return !!this.printService; - }, - get supportsPrinting() { return PDFPrintServiceFactory.instance.supportsPrinting; }, @@ -1378,12 +1374,9 @@ const PDFViewerApplication = { } } - if (!this.supportsPrinting) { - return; - } if (triggerAutoPrint) { - setTimeout(function () { - window.print(); + setTimeout(() => { + this.triggerPrinting(); }); } }, @@ -1638,7 +1631,7 @@ const PDFViewerApplication = { }, forceRendering() { - this.pdfRenderingQueue.printing = this.printing; + this.pdfRenderingQueue.printing = !!this.printService; this.pdfRenderingQueue.isThumbnailViewEnabled = this.pdfSidebar.isThumbnailViewVisible; this.pdfRenderingQueue.renderHighestPriority(); }, @@ -1732,6 +1725,13 @@ const PDFViewerApplication = { this.pdfPresentationMode.request(); }, + triggerPrinting() { + if (!this.supportsPrinting) { + return; + } + window.print(); + }, + bindEvents() { const { eventBus, _boundEvents } = this; @@ -2242,9 +2242,7 @@ function webViewerNamedAction(evt) { break; case "Print": - if (PDFViewerApplication.supportsPrinting) { - webViewerPrint(); - } + PDFViewerApplication.triggerPrinting(); break; case "SaveAs": @@ -2400,7 +2398,7 @@ function webViewerPresentationMode() { PDFViewerApplication.requestPresentationMode(); } function webViewerPrint() { - window.print(); + PDFViewerApplication.triggerPrinting(); } function webViewerDownloadOrSave(sourceEventType) { if (