Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Scripting] Try to ensure that the WillPrint/DidPrint respectively DidSave events are always dispatched #12771

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ const PDFViewerApplication = {
.catch(downloadByUrl); // Error occurred, try downloading with the URL.
},

save({ sourceEventType = "download" } = {}) {
async save({ sourceEventType = "download" } = {}) {
if (this._saveInProgress) {
return;
}
Expand All @@ -1036,27 +1036,28 @@ const PDFViewerApplication = {
this.download({ sourceEventType });
return;
}
this._scriptingInstance?.scripting.dispatchEventInSandbox({
this._saveInProgress = true;

await this._scriptingInstance?.scripting.dispatchEventInSandbox({
id: "doc",
name: "WillSave",
});

this._saveInProgress = true;
this.pdfDocument
.saveDocument(this.pdfDocument.annotationStorage)
.then(data => {
const blob = new Blob([data], { type: "application/pdf" });
downloadManager.download(blob, url, filename, sourceEventType);

this._scriptingInstance?.scripting.dispatchEventInSandbox({
id: "doc",
name: "DidSave",
});
})
.catch(() => {
this.download({ sourceEventType });
})
.finally(() => {
.finally(async () => {
await this._scriptingInstance?.scripting.dispatchEventInSandbox({
id: "doc",
name: "DidSave",
});

this._saveInProgress = false;
});
},
Expand Down Expand Up @@ -1614,7 +1615,7 @@ const PDFViewerApplication = {
return;
}

scripting.dispatchEventInSandbox({
await scripting.dispatchEventInSandbox({
id: "doc",
name: "Open",
});
Expand Down Expand Up @@ -1967,6 +1968,13 @@ const PDFViewerApplication = {
},

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) {
// There is no way to suppress beforePrint/afterPrint events,
// but PDFPrintService may generate double events -- this will ignore
Expand Down Expand Up @@ -2028,6 +2036,13 @@ const PDFViewerApplication = {
},

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) {
this.printService.destroy();
this.printService = null;
Expand Down Expand Up @@ -2060,17 +2075,7 @@ const PDFViewerApplication = {
if (!this.supportsPrinting) {
return;
}
this._scriptingInstance?.scripting.dispatchEventInSandbox({
id: "doc",
name: "WillPrint",
});

window.print();

this._scriptingInstance?.scripting.dispatchEventInSandbox({
id: "doc",
name: "DidPrint",
});
},

bindEvents() {
Expand Down