[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:
Jonas Jenwald 2024-04-24 21:55:50 +02:00
parent fa69d9a3bc
commit f6cd03955b
6 changed files with 42 additions and 74 deletions

View file

@ -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 };