Ensure that the *correct* PDF document is still active after *every* asynchronous API-call in PDFViewerApplication._initializeJavaScript
This patch also changes the method to skip *all* data fetching when "enableScripting" isn't active. Finally, simplifies some event-data accesses in the "updateFromSandbox" listener.
This commit is contained in:
parent
1f2f8c907b
commit
f4d8a427f0
47
web/app.js
47
web/app.js
@ -1409,16 +1409,20 @@ const PDFViewerApplication = {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
async _initializeJavaScript(pdfDocument) {
|
async _initializeJavaScript(pdfDocument) {
|
||||||
const objects = await pdfDocument.getFieldObjects();
|
if (!AppOptions.get("enableScripting")) {
|
||||||
|
|
||||||
if (pdfDocument !== this.pdfDocument) {
|
|
||||||
return; // The document was closed while the JavaScript data resolved.
|
|
||||||
}
|
|
||||||
if (!objects || !AppOptions.get("enableScripting")) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const calculationOrder = await pdfDocument.getCalculationOrderIds();
|
const [objects, calculationOrder] = await Promise.all([
|
||||||
const scripting = this.externalServices.scripting;
|
pdfDocument.getFieldObjects(),
|
||||||
|
pdfDocument.getCalculationOrderIds(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!objects || pdfDocument !== this.pdfDocument) {
|
||||||
|
// No FieldObjects were found in the document,
|
||||||
|
// or the document was closed while the data resolved.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const { scripting } = this.externalServices;
|
||||||
|
|
||||||
if (!this.documentInfo) {
|
if (!this.documentInfo) {
|
||||||
// It should be *extremely* rare for metadata to not have been resolved
|
// It should be *extremely* rare for metadata to not have been resolved
|
||||||
@ -1436,37 +1440,37 @@ const PDFViewerApplication = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener("updateFromSandbox", event => {
|
window.addEventListener("updateFromSandbox", event => {
|
||||||
const detail = event.detail;
|
const { detail } = event;
|
||||||
const id = detail.id;
|
const { id, command, value } = detail;
|
||||||
if (!id) {
|
if (!id) {
|
||||||
switch (detail.command) {
|
switch (command) {
|
||||||
case "alert":
|
case "alert":
|
||||||
// eslint-disable-next-line no-alert
|
// eslint-disable-next-line no-alert
|
||||||
window.alert(detail.value);
|
window.alert(value);
|
||||||
break;
|
break;
|
||||||
case "clear":
|
case "clear":
|
||||||
console.clear();
|
console.clear();
|
||||||
break;
|
break;
|
||||||
case "error":
|
case "error":
|
||||||
console.error(detail.value);
|
console.error(value);
|
||||||
break;
|
break;
|
||||||
case "layout":
|
case "layout":
|
||||||
this.pdfViewer.spreadMode = apiPageLayoutToSpreadMode(detail.value);
|
this.pdfViewer.spreadMode = apiPageLayoutToSpreadMode(value);
|
||||||
return;
|
return;
|
||||||
case "page-num":
|
case "page-num":
|
||||||
this.pdfViewer.currentPageNumber = detail.value + 1;
|
this.pdfViewer.currentPageNumber = value + 1;
|
||||||
return;
|
return;
|
||||||
case "print":
|
case "print":
|
||||||
this.triggerPrinting();
|
this.triggerPrinting();
|
||||||
return;
|
return;
|
||||||
case "println":
|
case "println":
|
||||||
console.log(detail.value);
|
console.log(value);
|
||||||
break;
|
break;
|
||||||
case "zoom":
|
case "zoom":
|
||||||
if (typeof detail.value === "string") {
|
if (typeof value === "string") {
|
||||||
this.pdfViewer.currentScaleValue = detail.value;
|
this.pdfViewer.currentScaleValue = value;
|
||||||
} else {
|
} else {
|
||||||
this.pdfViewer.currentScale = detail.value;
|
this.pdfViewer.currentScale = value;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -1477,10 +1481,9 @@ const PDFViewerApplication = {
|
|||||||
if (element) {
|
if (element) {
|
||||||
element.dispatchEvent(new CustomEvent("updateFromSandbox", { detail }));
|
element.dispatchEvent(new CustomEvent("updateFromSandbox", { detail }));
|
||||||
} else {
|
} else {
|
||||||
const value = detail.value;
|
|
||||||
if (value !== undefined && value !== null) {
|
if (value !== undefined && value !== null) {
|
||||||
// the element hasn't been rendered yet so use annotation storage
|
// The element hasn't been rendered yet, use the AnnotationStorage.
|
||||||
pdfDocument.annotationStorage.setValue(id, detail.value);
|
pdfDocument.annotationStorage.setValue(id, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user