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.
This commit is contained in:
Jonas Jenwald 2020-09-30 15:02:11 +02:00
parent d49b2f6cc2
commit 89ce326d12

View File

@ -593,10 +593,6 @@ const PDFViewerApplication = {
this.pdfViewer.currentPageNumber = val; this.pdfViewer.currentPageNumber = val;
}, },
get printing() {
return !!this.printService;
},
get supportsPrinting() { get supportsPrinting() {
return PDFPrintServiceFactory.instance.supportsPrinting; return PDFPrintServiceFactory.instance.supportsPrinting;
}, },
@ -1378,12 +1374,9 @@ const PDFViewerApplication = {
} }
} }
if (!this.supportsPrinting) {
return;
}
if (triggerAutoPrint) { if (triggerAutoPrint) {
setTimeout(function () { setTimeout(() => {
window.print(); this.triggerPrinting();
}); });
} }
}, },
@ -1638,7 +1631,7 @@ const PDFViewerApplication = {
}, },
forceRendering() { forceRendering() {
this.pdfRenderingQueue.printing = this.printing; this.pdfRenderingQueue.printing = !!this.printService;
this.pdfRenderingQueue.isThumbnailViewEnabled = this.pdfSidebar.isThumbnailViewVisible; this.pdfRenderingQueue.isThumbnailViewEnabled = this.pdfSidebar.isThumbnailViewVisible;
this.pdfRenderingQueue.renderHighestPriority(); this.pdfRenderingQueue.renderHighestPriority();
}, },
@ -1732,6 +1725,13 @@ const PDFViewerApplication = {
this.pdfPresentationMode.request(); this.pdfPresentationMode.request();
}, },
triggerPrinting() {
if (!this.supportsPrinting) {
return;
}
window.print();
},
bindEvents() { bindEvents() {
const { eventBus, _boundEvents } = this; const { eventBus, _boundEvents } = this;
@ -2242,9 +2242,7 @@ function webViewerNamedAction(evt) {
break; break;
case "Print": case "Print":
if (PDFViewerApplication.supportsPrinting) { PDFViewerApplication.triggerPrinting();
webViewerPrint();
}
break; break;
case "SaveAs": case "SaveAs":
@ -2400,7 +2398,7 @@ function webViewerPresentationMode() {
PDFViewerApplication.requestPresentationMode(); PDFViewerApplication.requestPresentationMode();
} }
function webViewerPrint() { function webViewerPrint() {
window.print(); PDFViewerApplication.triggerPrinting();
} }
function webViewerDownloadOrSave(sourceEventType) { function webViewerDownloadOrSave(sourceEventType) {
if ( if (