Move handling of the PageOpen/PageClose events into the PDFScriptingManager (PR 13042 follow-up)

By moving this code from the `BaseViewer` and into `PDFScriptingManager`, all of the scripting initialization/handling code is now limited to just one file/class which help overall readability (in my opinion). Also, this patch is a *net reduction* in number of lines of code which can never hurt.

As part of these changes, the intermediary "pageopen"/"pageclose" events are now removed in favor of using the "regular" viewer events directly in `PDFScriptingManager`. Hence this removes some (strictly unnecessary) indirection in the current code, when handling PageOpen/PageClose events, which leads to overall fewer function calls in this part of the code.
This commit is contained in:
Jonas Jenwald 2021-03-04 14:49:02 +01:00
parent fb87704b38
commit 87dd93b7fc
2 changed files with 77 additions and 110 deletions

View file

@ -470,7 +470,7 @@ class BaseViewer {
this.findController.setDocument(null);
}
if (this._scriptingManager) {
// Defer this slightly, to allow the "pageclose" event to be handled.
// Defer this slightly, to allow the "PageClose" event to be handled.
Promise.resolve().then(() => {
this._scriptingManager.setDocument(null);
});
@ -670,8 +670,6 @@ class BaseViewer {
this.eventBus._off("pagerendered", this._onAfterDraw);
this._onAfterDraw = null;
}
this._resetScriptingEvents();
// Remove the pages from the DOM...
this.viewer.textContent = "";
// ... and reset the Scroll mode CSS class(es) afterwards.
@ -1648,83 +1646,6 @@ class BaseViewer {
this.currentPageNumber = Math.max(currentPageNumber - advance, 1);
return true;
}
async initializeScriptingEvents() {
if (!this.enableScripting || this._pageOpenPendingSet) {
return;
}
const eventBus = this.eventBus,
pageOpenPendingSet = (this._pageOpenPendingSet = new Set()),
scriptingEvents = (this._scriptingEvents = Object.create(null));
const dispatchPageClose = pageNumber => {
if (pageOpenPendingSet.has(pageNumber)) {
return; // No "pageopen" event was dispatched for the previous page.
}
eventBus.dispatch("pageclose", { source: this, pageNumber });
};
const dispatchPageOpen = pageNumber => {
const pageView = this._pages[pageNumber - 1];
if (pageView?.renderingState === RenderingStates.FINISHED) {
pageOpenPendingSet.delete(pageNumber);
eventBus.dispatch("pageopen", {
source: this,
pageNumber,
actionsPromise: pageView.pdfPage?.getJSActions(),
});
} else {
pageOpenPendingSet.add(pageNumber);
}
};
scriptingEvents.onPageChanging = ({ pageNumber, previous }) => {
if (pageNumber === previous) {
return; // The active page didn't change.
}
dispatchPageClose(previous);
dispatchPageOpen(pageNumber);
};
eventBus._on("pagechanging", scriptingEvents.onPageChanging);
scriptingEvents.onPageRendered = ({ pageNumber }) => {
if (!pageOpenPendingSet.has(pageNumber)) {
return; // No pending "pageopen" event for the newly rendered page.
}
if (pageNumber !== this._currentPageNumber) {
return; // The newly rendered page is no longer the current one.
}
dispatchPageOpen(pageNumber);
};
eventBus._on("pagerendered", scriptingEvents.onPageRendered);
scriptingEvents.onPagesDestroy = () => {
dispatchPageClose(this._currentPageNumber);
};
eventBus._on("pagesdestroy", scriptingEvents.onPagesDestroy);
// Ensure that a "pageopen" event is dispatched for the initial page.
dispatchPageOpen(this._currentPageNumber);
}
/**
* @private
*/
_resetScriptingEvents() {
if (!this.enableScripting || !this._pageOpenPendingSet) {
return;
}
const eventBus = this.eventBus,
scriptingEvents = this._scriptingEvents;
// Remove the event listeners.
eventBus._off("pagechanging", scriptingEvents.onPageChanging);
eventBus._off("pagerendered", scriptingEvents.onPageRendered);
eventBus._off("pagesdestroy", scriptingEvents.onPagesDestroy);
this._pageOpenPendingSet = null;
this._scriptingEvents = null;
}
}
export { BaseViewer };