Skip to content

Commit

Permalink
Don't dispatch a "doc/Open" event in the sandbox when creating it failed
Browse files Browse the repository at this point in the history
There's really no point, as far as I can tell, to attempt to dispatch an event in a non-existent sandbox. Generally speaking, even trying to do this *could* possibly even lead to errors in some cases.

Furthermore, utilize optional chaining to simplify some `dispatchEventInSandbox` calls througout the viewer.

Finally, replace superfluous `return` statements with `break` in the switch-statement in the `updateFromSandbox` event-handler.
  • Loading branch information
Snuffleupagus committed Dec 18, 2020
1 parent c78f153 commit 66e9d4a
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1036,13 +1036,10 @@ const PDFViewerApplication = {
this.download({ sourceEventType });
return;
}

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

this._saveInProgress = true;
this.pdfDocument
Expand All @@ -1051,12 +1048,10 @@ const PDFViewerApplication = {
const blob = new Blob([data], { type: "application/pdf" });
downloadManager.download(blob, url, filename, sourceEventType);

if (this._scriptingInstance) {
this._scriptingInstance.scripting.dispatchEventInSandbox({
id: "doc",
name: "DidSave",
});
}
this._scriptingInstance?.scripting.dispatchEventInSandbox({
id: "doc",
name: "DidSave",
});
})
.catch(() => {
this.download({ sourceEventType });
Expand Down Expand Up @@ -1514,15 +1509,15 @@ const PDFViewerApplication = {
break;
case "layout":
this.pdfViewer.spreadMode = apiPageLayoutToSpreadMode(value);
return;
break;
case "page-num":
this.pdfViewer.currentPageNumber = value + 1;
return;
break;
case "print":
this.pdfViewer.pagesPromise.then(() => {
this.triggerPrinting();
});
return;
break;
case "println":
console.log(value);
break;
Expand Down Expand Up @@ -1609,6 +1604,7 @@ const PDFViewerApplication = {
} catch (error) {
console.error(`_initializeJavaScript: "${error?.message}".`);
this._destroyScriptingInstance();
return;
}

scripting.dispatchEventInSandbox({
Expand Down Expand Up @@ -2033,21 +2029,17 @@ const PDFViewerApplication = {
if (!this.supportsPrinting) {
return;
}
if (this._scriptingInstance) {
this._scriptingInstance.scripting.dispatchEventInSandbox({
id: "doc",
name: "WillPrint",
});
}
this._scriptingInstance?.scripting.dispatchEventInSandbox({
id: "doc",
name: "WillPrint",
});

window.print();

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

bindEvents() {
Expand Down

0 comments on commit 66e9d4a

Please sign in to comment.