Add new "pageopen"/"pageclose" events for usage with JavaScript actions
Having looked at the Acrobat JavaScript specification, see https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/AcrobatDC_js_api_reference.pdf#G5.1963437, I suppose that introducing these two new events is probably the easiest solution overall. However there's a number of things that, as far as I'm concerned, will help the overall implementation: - Only dispatch these new events when `enableScripting = true` is set. - Handle them *separately* from the existing "pagechanging" event dispatching, to avoid too much clutter. - Don't dispatch either of the events if the page didn't actually change. - When waiting for pages to render, don't dispatch "pageopen" if the page is no longer active when rendering finishes. - Ensure that we only use *one* "pagerendered" event listener. - Ensure that "pageopen" is actually dispatched when the document loads. I suppose that we *could* avoid adding the "pageclose" event, and use the existing "pagechanging" event instead, however having a separate event might allow more flexibility in the future. (E.g. I don't know if we'll possibly want to dispatch "pageclose" on document close, as mentioned briefly in the specification.)
This commit is contained in:
parent
3447f7c703
commit
2a83c965e8
@ -214,6 +214,7 @@ class BaseViewer {
|
|||||||
if (this.removePageBorders) {
|
if (this.removePageBorders) {
|
||||||
this.viewer.classList.add("removePageBorders");
|
this.viewer.classList.add("removePageBorders");
|
||||||
}
|
}
|
||||||
|
this._initializeScriptingEvents();
|
||||||
// Defer the dispatching of this event, to give other viewer components
|
// Defer the dispatching of this event, to give other viewer components
|
||||||
// time to initialize *and* register 'baseviewerinit' event listeners.
|
// time to initialize *and* register 'baseviewerinit' event listeners.
|
||||||
Promise.resolve().then(() => {
|
Promise.resolve().then(() => {
|
||||||
@ -283,12 +284,14 @@ class BaseViewer {
|
|||||||
if (!(0 < val && val <= this.pagesCount)) {
|
if (!(0 < val && val <= this.pagesCount)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
const previous = this._currentPageNumber;
|
||||||
this._currentPageNumber = val;
|
this._currentPageNumber = val;
|
||||||
|
|
||||||
this.eventBus.dispatch("pagechanging", {
|
this.eventBus.dispatch("pagechanging", {
|
||||||
source: this,
|
source: this,
|
||||||
pageNumber: val,
|
pageNumber: val,
|
||||||
pageLabel: this._pageLabels && this._pageLabels[val - 1],
|
pageLabel: this._pageLabels && this._pageLabels[val - 1],
|
||||||
|
previous,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (resetCurrentPageView) {
|
if (resetCurrentPageView) {
|
||||||
@ -649,6 +652,7 @@ class BaseViewer {
|
|||||||
this._pagesCapability = createPromiseCapability();
|
this._pagesCapability = createPromiseCapability();
|
||||||
this._scrollMode = ScrollMode.VERTICAL;
|
this._scrollMode = ScrollMode.VERTICAL;
|
||||||
this._spreadMode = SpreadMode.NONE;
|
this._spreadMode = SpreadMode.NONE;
|
||||||
|
this._pageOpenPendingSet = null;
|
||||||
|
|
||||||
if (this._onBeforeDraw) {
|
if (this._onBeforeDraw) {
|
||||||
this.eventBus._off("pagerender", this._onBeforeDraw);
|
this.eventBus._off("pagerender", this._onBeforeDraw);
|
||||||
@ -1500,6 +1504,58 @@ class BaseViewer {
|
|||||||
this._setCurrentPageNumber(pageNumber, /* resetCurrentPageView = */ true);
|
this._setCurrentPageNumber(pageNumber, /* resetCurrentPageView = */ true);
|
||||||
this.update();
|
this.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_initializeScriptingEvents() {
|
||||||
|
if (!this.enableScripting) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const { eventBus } = this;
|
||||||
|
|
||||||
|
const dispatchPageClose = pageNumber => {
|
||||||
|
eventBus.dispatch("pageclose", { source: this, pageNumber });
|
||||||
|
};
|
||||||
|
const dispatchPageOpen = (pageNumber, force = false) => {
|
||||||
|
const pageView = this._pages[pageNumber - 1];
|
||||||
|
if (force || pageView?.renderingState === RenderingStates.FINISHED) {
|
||||||
|
this._pageOpenPendingSet?.delete(pageNumber);
|
||||||
|
|
||||||
|
eventBus.dispatch("pageopen", { source: this, pageNumber });
|
||||||
|
} else {
|
||||||
|
if (!this._pageOpenPendingSet) {
|
||||||
|
this._pageOpenPendingSet = new Set();
|
||||||
|
}
|
||||||
|
this._pageOpenPendingSet.add(pageNumber);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
eventBus._on("pagechanging", ({ pageNumber, previous }) => {
|
||||||
|
if (pageNumber === previous) {
|
||||||
|
return; // The active page didn't change.
|
||||||
|
}
|
||||||
|
dispatchPageClose(previous);
|
||||||
|
dispatchPageOpen(pageNumber);
|
||||||
|
});
|
||||||
|
|
||||||
|
eventBus._on("pagerendered", ({ pageNumber }) => {
|
||||||
|
if (!this._pageOpenPendingSet) {
|
||||||
|
return; // No pending "pageopen" events.
|
||||||
|
}
|
||||||
|
if (!this._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, /* force = */ true);
|
||||||
|
});
|
||||||
|
|
||||||
|
eventBus._on("pagesinit", () => {
|
||||||
|
dispatchPageOpen(this._currentPageNumber);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { BaseViewer };
|
export { BaseViewer };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user