Inlude the JS actions for the page when dispatching the "pageopen"-event in the BaseViewer

Note first of all how the `PDFDocumentProxy.getJSActions` method in the API caches the result, which makes repeated lookups cheap enough to not really be an issue.
Secondly, with the previous patch, we're now only dispatching "pageopen"/"pageclose"-events when there's actually a sandbox that listens for them.

All-in-all, with these changes we can thus simplify the default-viewer "pageopen"-event handler a fair bit.
This commit is contained in:
Jonas Jenwald 2021-01-12 13:59:53 +01:00
parent a882a85446
commit 13742eb82d
3 changed files with 16 additions and 23 deletions

View File

@ -1140,12 +1140,9 @@ class PDFPageProxy {
* {Object} with JS actions. * {Object} with JS actions.
*/ */
getJSActions() { getJSActions() {
if (!this._jsActionsPromise) { return (this._jsActionsPromise ||= this._transport.getPageJSActions(
this._jsActionsPromise = this._transport.getPageJSActions( this._pageIndex
this._pageIndex ));
);
}
return this._jsActionsPromise;
} }
/** /**

View File

@ -1564,24 +1564,15 @@ const PDFViewerApplication = {
internalEvents.set("updatefromsandbox", updateFromSandbox); internalEvents.set("updatefromsandbox", updateFromSandbox);
const visitedPages = new Map(); const visitedPages = new Map();
const pageOpen = ({ pageNumber }) => { const pageOpen = ({ pageNumber, actionsPromise }) => {
visitedPages.set( visitedPages.set(
pageNumber, pageNumber,
(async () => { (async () => {
// Avoid sending, and thus serializing, the `actions` data // Avoid sending, and thus serializing, the `actions` data
// when the same page is open several times. // when the same page is opened several times.
let actions = null; let actions = null;
if (!visitedPages.has(pageNumber)) { if (!visitedPages.has(pageNumber)) {
// visitedPages doesn't contain pageNumber: first visit. actions = await actionsPromise;
const pageView = this.pdfViewer.getPageView(
/* index = */ pageNumber - 1
);
if (pageView?.pdfPage) {
actions = await pageView.pdfPage.getJSActions();
} else {
actions = await pdfDocument.getPage(pageNumber).getJSActions();
}
if (pdfDocument !== this.pdfDocument) { if (pdfDocument !== this.pdfDocument) {
return; // The document was closed while the actions resolved. return; // The document was closed while the actions resolved.
@ -1599,14 +1590,15 @@ const PDFViewerApplication = {
}; };
const pageClose = async ({ pageNumber }) => { const pageClose = async ({ pageNumber }) => {
const promise = visitedPages.get(pageNumber); const actionsPromise = visitedPages.get(pageNumber);
if (!promise) { if (!actionsPromise) {
// Ensure that the "pageclose" event was preceded by a "pageopen" event.
return; return;
} }
visitedPages.set(pageNumber, null); visitedPages.set(pageNumber, null);
// Wait for PageOpen has been sent. // Ensure that the "pageopen" event is handled first.
await promise; await actionsPromise;
if (pdfDocument !== this.pdfDocument) { if (pdfDocument !== this.pdfDocument) {
return; // The document was closed while the actions resolved. return; // The document was closed while the actions resolved.

View File

@ -1526,7 +1526,11 @@ class BaseViewer {
if (pageView?.renderingState === RenderingStates.FINISHED) { if (pageView?.renderingState === RenderingStates.FINISHED) {
pageOpenPendingSet.delete(pageNumber); pageOpenPendingSet.delete(pageNumber);
eventBus.dispatch("pageopen", { source: this, pageNumber }); eventBus.dispatch("pageopen", {
source: this,
pageNumber,
actionsPromise: pageView.pdfPage?.getJSActions(),
});
} else { } else {
pageOpenPendingSet.add(pageNumber); pageOpenPendingSet.add(pageNumber);
} }