mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
JS -- Plug PageOpen and PageClose actions
This commit is contained in:
parent
ed3758f84d
commit
6523f8880b
5 changed files with 172 additions and 16 deletions
60
web/app.js
60
web/app.js
|
@ -1542,6 +1542,64 @@ const PDFViewerApplication = {
|
|||
};
|
||||
internalEvents.set("updatefromsandbox", updateFromSandbox);
|
||||
|
||||
const visitedPages = new Map();
|
||||
const pageOpen = ({ pageNumber }) => {
|
||||
visitedPages.set(
|
||||
pageNumber,
|
||||
(async () => {
|
||||
// Avoid sending, and thus serializing, the `actions` data
|
||||
// when the same page is open several times.
|
||||
let actions = null;
|
||||
if (!visitedPages.has(pageNumber)) {
|
||||
// visitedPages doesn't contain pageNumber: first visit.
|
||||
|
||||
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) {
|
||||
return; // The document was closed while the actions resolved.
|
||||
}
|
||||
}
|
||||
|
||||
this._scriptingInstance?.scripting.dispatchEventInSandbox({
|
||||
id: "page",
|
||||
name: "PageOpen",
|
||||
pageNumber,
|
||||
actions,
|
||||
});
|
||||
})()
|
||||
);
|
||||
};
|
||||
|
||||
const pageClose = async ({ pageNumber }) => {
|
||||
const promise = visitedPages.get(pageNumber);
|
||||
if (!promise) {
|
||||
return;
|
||||
}
|
||||
visitedPages.set(pageNumber, null);
|
||||
|
||||
// Wait for PageOpen has been sent.
|
||||
await promise;
|
||||
|
||||
if (pdfDocument !== this.pdfDocument) {
|
||||
return; // The document was closed while the actions resolved.
|
||||
}
|
||||
|
||||
this._scriptingInstance?.scripting.dispatchEventInSandbox({
|
||||
id: "page",
|
||||
name: "PageClose",
|
||||
pageNumber,
|
||||
});
|
||||
};
|
||||
internalEvents.set("pageopen", pageOpen);
|
||||
internalEvents.set("pageclose", pageClose);
|
||||
|
||||
const dispatchEventInSandbox = ({ detail }) => {
|
||||
scripting.dispatchEventInSandbox(detail);
|
||||
};
|
||||
|
@ -1613,6 +1671,8 @@ const PDFViewerApplication = {
|
|||
name: "Open",
|
||||
});
|
||||
|
||||
await pageOpen({ pageNumber: this.pdfViewer.currentPageNumber });
|
||||
|
||||
// Used together with the integration-tests, see the `scriptingReady`
|
||||
// getter, to enable awaiting full initialization of the scripting/sandbox.
|
||||
// (Defer this slightly, to make absolutely sure that everything is done.)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue