Merge pull request #12747 from Snuffleupagus/pageopen-pageclose
Add new "pageopen"/"pageclose" events for usage with JavaScript actions
This commit is contained in:
commit
6efd350993
@ -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) {
|
||||||
@ -457,6 +460,8 @@ class BaseViewer {
|
|||||||
*/
|
*/
|
||||||
setDocument(pdfDocument) {
|
setDocument(pdfDocument) {
|
||||||
if (this.pdfDocument) {
|
if (this.pdfDocument) {
|
||||||
|
this.eventBus.dispatch("pagesdestroy", { source: this });
|
||||||
|
|
||||||
this._cancelRendering();
|
this._cancelRendering();
|
||||||
this._resetView();
|
this._resetView();
|
||||||
|
|
||||||
@ -649,6 +654,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 +1506,62 @@ 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);
|
||||||
|
});
|
||||||
|
|
||||||
|
eventBus._on("pagesdestroy", () => {
|
||||||
|
dispatchPageClose(this._currentPageNumber);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { BaseViewer };
|
export { BaseViewer };
|
||||||
|
Loading…
Reference in New Issue
Block a user