Merge pull request #12771 from Snuffleupagus/viewer-dispatchEventInSandbox-fixes

[Scripting] Try to ensure that the `WillPrint`/`DidPrint` respectively `DidSave` events are always dispatched
This commit is contained in:
calixteman 2020-12-24 02:47:18 -08:00 committed by GitHub
commit 0a7d5940d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1013,7 +1013,7 @@ const PDFViewerApplication = {
.catch(downloadByUrl); // Error occurred, try downloading with the URL. .catch(downloadByUrl); // Error occurred, try downloading with the URL.
}, },
save({ sourceEventType = "download" } = {}) { async save({ sourceEventType = "download" } = {}) {
if (this._saveInProgress) { if (this._saveInProgress) {
return; return;
} }
@ -1036,27 +1036,28 @@ const PDFViewerApplication = {
this.download({ sourceEventType }); this.download({ sourceEventType });
return; return;
} }
this._scriptingInstance?.scripting.dispatchEventInSandbox({ this._saveInProgress = true;
await this._scriptingInstance?.scripting.dispatchEventInSandbox({
id: "doc", id: "doc",
name: "WillSave", name: "WillSave",
}); });
this._saveInProgress = true;
this.pdfDocument this.pdfDocument
.saveDocument(this.pdfDocument.annotationStorage) .saveDocument(this.pdfDocument.annotationStorage)
.then(data => { .then(data => {
const blob = new Blob([data], { type: "application/pdf" }); const blob = new Blob([data], { type: "application/pdf" });
downloadManager.download(blob, url, filename, sourceEventType); downloadManager.download(blob, url, filename, sourceEventType);
this._scriptingInstance?.scripting.dispatchEventInSandbox({
id: "doc",
name: "DidSave",
});
}) })
.catch(() => { .catch(() => {
this.download({ sourceEventType }); this.download({ sourceEventType });
}) })
.finally(() => { .finally(async () => {
await this._scriptingInstance?.scripting.dispatchEventInSandbox({
id: "doc",
name: "DidSave",
});
this._saveInProgress = false; this._saveInProgress = false;
}); });
}, },
@ -1614,7 +1615,7 @@ const PDFViewerApplication = {
return; return;
} }
scripting.dispatchEventInSandbox({ await scripting.dispatchEventInSandbox({
id: "doc", id: "doc",
name: "Open", name: "Open",
}); });
@ -1967,6 +1968,13 @@ const PDFViewerApplication = {
}, },
beforePrint() { beforePrint() {
// Given that the "beforeprint" browser event is synchronous, we
// unfortunately cannot await the scripting event dispatching here.
this._scriptingInstance?.scripting.dispatchEventInSandbox({
id: "doc",
name: "WillPrint",
});
if (this.printService) { if (this.printService) {
// There is no way to suppress beforePrint/afterPrint events, // There is no way to suppress beforePrint/afterPrint events,
// but PDFPrintService may generate double events -- this will ignore // but PDFPrintService may generate double events -- this will ignore
@ -2028,6 +2036,13 @@ const PDFViewerApplication = {
}, },
afterPrint() { afterPrint() {
// Given that the "afterprint" browser event is synchronous, we
// unfortunately cannot await the scripting event dispatching here.
this._scriptingInstance?.scripting.dispatchEventInSandbox({
id: "doc",
name: "DidPrint",
});
if (this.printService) { if (this.printService) {
this.printService.destroy(); this.printService.destroy();
this.printService = null; this.printService = null;
@ -2060,17 +2075,7 @@ const PDFViewerApplication = {
if (!this.supportsPrinting) { if (!this.supportsPrinting) {
return; return;
} }
this._scriptingInstance?.scripting.dispatchEventInSandbox({
id: "doc",
name: "WillPrint",
});
window.print(); window.print();
this._scriptingInstance?.scripting.dispatchEventInSandbox({
id: "doc",
name: "DidPrint",
});
}, },
bindEvents() { bindEvents() {