mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
[api-minor] Move the page reference/number caching into the API
Rather than having to handle this *manually* throughout the viewer, this functionality can instead be moved into the API which simplifies the code slightly.
This commit is contained in:
parent
fa69d9a3bc
commit
f6cd03955b
6 changed files with 42 additions and 74 deletions
|
@ -106,12 +106,6 @@ class IPDFLinkService {
|
|||
* @param {Object} action
|
||||
*/
|
||||
executeSetOCGState(action) {}
|
||||
|
||||
/**
|
||||
* @param {number} pageNum - page number.
|
||||
* @param {Object} pageRef - reference to the page.
|
||||
*/
|
||||
cachePageRef(pageNum, pageRef) {}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -49,8 +49,6 @@ const LinkTarget = {
|
|||
class PDFLinkService {
|
||||
externalLinkEnabled = true;
|
||||
|
||||
#pagesRefCache = new Map();
|
||||
|
||||
/**
|
||||
* @param {PDFLinkServiceOptions} options
|
||||
*/
|
||||
|
@ -74,7 +72,6 @@ class PDFLinkService {
|
|||
setDocument(pdfDocument, baseUrl = null) {
|
||||
this.baseUrl = baseUrl;
|
||||
this.pdfDocument = pdfDocument;
|
||||
this.#pagesRefCache.clear();
|
||||
}
|
||||
|
||||
setViewer(pdfViewer) {
|
||||
|
@ -157,15 +154,14 @@ class PDFLinkService {
|
|||
// Dest array looks like that: <page-ref> </XYZ|/FitXXX> <args..>
|
||||
const [destRef] = explicitDest;
|
||||
|
||||
if (typeof destRef === "object" && destRef !== null) {
|
||||
pageNumber = this._cachedPageNumber(destRef);
|
||||
if (destRef && typeof destRef === "object") {
|
||||
pageNumber = this.pdfDocument.cachedPageNumber(destRef);
|
||||
|
||||
if (!pageNumber) {
|
||||
// Fetch the page reference if it's not yet available. This could
|
||||
// only occur during loading, before all pages have been resolved.
|
||||
try {
|
||||
pageNumber = (await this.pdfDocument.getPageIndex(destRef)) + 1;
|
||||
this.cachePageRef(pageNumber, destRef);
|
||||
} catch {
|
||||
console.error(
|
||||
`goToDestination: "${destRef}" is not a valid page reference, for dest="${dest}".`
|
||||
|
@ -496,31 +492,6 @@ class PDFLinkService {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} pageNum - page number.
|
||||
* @param {Object} pageRef - reference to the page.
|
||||
*/
|
||||
cachePageRef(pageNum, pageRef) {
|
||||
if (!pageRef) {
|
||||
return;
|
||||
}
|
||||
const refStr =
|
||||
pageRef.gen === 0 ? `${pageRef.num}R` : `${pageRef.num}R${pageRef.gen}`;
|
||||
this.#pagesRefCache.set(refStr, pageNum);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
_cachedPageNumber(pageRef) {
|
||||
if (!pageRef) {
|
||||
return null;
|
||||
}
|
||||
const refStr =
|
||||
pageRef.gen === 0 ? `${pageRef.num}R` : `${pageRef.num}R${pageRef.gen}`;
|
||||
return this.#pagesRefCache.get(refStr) || null;
|
||||
}
|
||||
|
||||
static #isValidExplicitDest(dest) {
|
||||
if (!Array.isArray(dest) || dest.length < 2) {
|
||||
return false;
|
||||
|
@ -580,12 +551,6 @@ class PDFLinkService {
|
|||
*/
|
||||
class SimpleLinkService extends PDFLinkService {
|
||||
setDocument(pdfDocument, baseUrl = null) {}
|
||||
|
||||
/**
|
||||
* @param {number} pageNum - page number.
|
||||
* @param {Object} pageRef - reference to the page.
|
||||
*/
|
||||
cachePageRef(pageNum, pageRef) {}
|
||||
}
|
||||
|
||||
export { LinkTarget, PDFLinkService, SimpleLinkService };
|
||||
|
|
|
@ -325,21 +325,10 @@ class PDFOutlineViewer extends BaseTreeViewer {
|
|||
if (Array.isArray(explicitDest)) {
|
||||
const [destRef] = explicitDest;
|
||||
|
||||
if (typeof destRef === "object" && destRef !== null) {
|
||||
pageNumber = this.linkService._cachedPageNumber(destRef);
|
||||
|
||||
if (!pageNumber) {
|
||||
try {
|
||||
pageNumber = (await pdfDocument.getPageIndex(destRef)) + 1;
|
||||
|
||||
if (pdfDocument !== this._pdfDocument) {
|
||||
return null; // The document was closed while the data resolved.
|
||||
}
|
||||
this.linkService.cachePageRef(pageNumber, destRef);
|
||||
} catch {
|
||||
// Invalid page reference, ignore it and continue parsing.
|
||||
}
|
||||
}
|
||||
if (destRef && typeof destRef === "object") {
|
||||
// The page reference must be available, since the current method
|
||||
// won't be invoked until all pages have been loaded.
|
||||
pageNumber = pdfDocument.cachedPageNumber(destRef);
|
||||
} else if (Number.isInteger(destRef)) {
|
||||
pageNumber = destRef + 1;
|
||||
}
|
||||
|
|
|
@ -935,11 +935,7 @@ class PDFViewer {
|
|||
// Set the first `pdfPage` immediately, since it's already loaded,
|
||||
// rather than having to repeat the `PDFDocumentProxy.getPage` call in
|
||||
// the `this.#ensurePdfPageLoaded` method before rendering can start.
|
||||
const firstPageView = this._pages[0];
|
||||
if (firstPageView) {
|
||||
firstPageView.setPdfPage(firstPdfPage);
|
||||
this.linkService.cachePageRef(1, firstPdfPage.ref);
|
||||
}
|
||||
this._pages[0]?.setPdfPage(firstPdfPage);
|
||||
|
||||
if (this._scrollMode === ScrollMode.PAGE) {
|
||||
// Ensure that the current page becomes visible on document load.
|
||||
|
@ -994,7 +990,6 @@ class PDFViewer {
|
|||
if (!pageView.pdfPage) {
|
||||
pageView.setPdfPage(pdfPage);
|
||||
}
|
||||
this.linkService.cachePageRef(pageNum, pdfPage.ref);
|
||||
if (--getPagesLeft === 0) {
|
||||
this._pagesCapability.resolve();
|
||||
}
|
||||
|
@ -1718,9 +1713,6 @@ class PDFViewer {
|
|||
if (!pageView.pdfPage) {
|
||||
pageView.setPdfPage(pdfPage);
|
||||
}
|
||||
if (!this.linkService._cachedPageNumber?.(pdfPage.ref)) {
|
||||
this.linkService.cachePageRef(pageView.id, pdfPage.ref);
|
||||
}
|
||||
return pdfPage;
|
||||
} catch (reason) {
|
||||
console.error("Unable to get page for page view", reason);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue