mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 09:20:06 +02:00
Inline the helper method in PDFLinkService.goToDestination
We no longer need the helper method to *potentially* call itself once data is available, and can instead take full advantage of async/await by inlining the code.
This commit is contained in:
parent
3052e99f65
commit
fa69d9a3bc
1 changed files with 35 additions and 47 deletions
|
@ -131,10 +131,31 @@ class PDFLinkService {
|
||||||
return this.pdfDocument ? this.pdfViewer.isInPresentationMode : false;
|
return this.pdfDocument ? this.pdfViewer.isInPresentationMode : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#goToDestinationHelper(rawDest, namedDest = null, explicitDest) {
|
/**
|
||||||
|
* This method will, when available, also update the browser history.
|
||||||
|
*
|
||||||
|
* @param {string|Array} dest - The named, or explicit, PDF destination.
|
||||||
|
*/
|
||||||
|
async goToDestination(dest) {
|
||||||
|
if (!this.pdfDocument) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let namedDest, explicitDest, pageNumber;
|
||||||
|
if (typeof dest === "string") {
|
||||||
|
namedDest = dest;
|
||||||
|
explicitDest = await this.pdfDocument.getDestination(dest);
|
||||||
|
} else {
|
||||||
|
namedDest = null;
|
||||||
|
explicitDest = await dest;
|
||||||
|
}
|
||||||
|
if (!Array.isArray(explicitDest)) {
|
||||||
|
console.error(
|
||||||
|
`goToDestination: "${explicitDest}" is not a valid destination array, for dest="${dest}".`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Dest array looks like that: <page-ref> </XYZ|/FitXXX> <args..>
|
// Dest array looks like that: <page-ref> </XYZ|/FitXXX> <args..>
|
||||||
const destRef = explicitDest[0];
|
const [destRef] = explicitDest;
|
||||||
let pageNumber;
|
|
||||||
|
|
||||||
if (typeof destRef === "object" && destRef !== null) {
|
if (typeof destRef === "object" && destRef !== null) {
|
||||||
pageNumber = this._cachedPageNumber(destRef);
|
pageNumber = this._cachedPageNumber(destRef);
|
||||||
|
@ -142,33 +163,27 @@ class PDFLinkService {
|
||||||
if (!pageNumber) {
|
if (!pageNumber) {
|
||||||
// Fetch the page reference if it's not yet available. This could
|
// Fetch the page reference if it's not yet available. This could
|
||||||
// only occur during loading, before all pages have been resolved.
|
// only occur during loading, before all pages have been resolved.
|
||||||
this.pdfDocument
|
try {
|
||||||
.getPageIndex(destRef)
|
pageNumber = (await this.pdfDocument.getPageIndex(destRef)) + 1;
|
||||||
.then(pageIndex => {
|
this.cachePageRef(pageNumber, destRef);
|
||||||
this.cachePageRef(pageIndex + 1, destRef);
|
} catch {
|
||||||
this.#goToDestinationHelper(rawDest, namedDest, explicitDest);
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
console.error(
|
console.error(
|
||||||
`PDFLinkService.#goToDestinationHelper: "${destRef}" is not ` +
|
`goToDestination: "${destRef}" is not a valid page reference, for dest="${dest}".`
|
||||||
`a valid page reference, for dest="${rawDest}".`
|
|
||||||
);
|
);
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else if (Number.isInteger(destRef)) {
|
} else if (Number.isInteger(destRef)) {
|
||||||
pageNumber = destRef + 1;
|
pageNumber = destRef + 1;
|
||||||
} else {
|
} else {
|
||||||
console.error(
|
console.error(
|
||||||
`PDFLinkService.#goToDestinationHelper: "${destRef}" is not ` +
|
`goToDestination: "${destRef}" is not a valid destination reference, for dest="${dest}".`
|
||||||
`a valid destination reference, for dest="${rawDest}".`
|
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!pageNumber || pageNumber < 1 || pageNumber > this.pagesCount) {
|
if (!pageNumber || pageNumber < 1 || pageNumber > this.pagesCount) {
|
||||||
console.error(
|
console.error(
|
||||||
`PDFLinkService.#goToDestinationHelper: "${pageNumber}" is not ` +
|
`goToDestination: "${pageNumber}" is not a valid page number, for dest="${dest}".`
|
||||||
`a valid page number, for dest="${rawDest}".`
|
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -187,33 +202,6 @@ class PDFLinkService {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This method will, when available, also update the browser history.
|
|
||||||
*
|
|
||||||
* @param {string|Array} dest - The named, or explicit, PDF destination.
|
|
||||||
*/
|
|
||||||
async goToDestination(dest) {
|
|
||||||
if (!this.pdfDocument) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let namedDest, explicitDest;
|
|
||||||
if (typeof dest === "string") {
|
|
||||||
namedDest = dest;
|
|
||||||
explicitDest = await this.pdfDocument.getDestination(dest);
|
|
||||||
} else {
|
|
||||||
namedDest = null;
|
|
||||||
explicitDest = await dest;
|
|
||||||
}
|
|
||||||
if (!Array.isArray(explicitDest)) {
|
|
||||||
console.error(
|
|
||||||
`PDFLinkService.goToDestination: "${explicitDest}" is not ` +
|
|
||||||
`a valid destination array, for dest="${dest}".`
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.#goToDestinationHelper(dest, namedDest, explicitDest);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method will, when available, also update the browser history.
|
* This method will, when available, also update the browser history.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue