Merge pull request #12559 from Snuffleupagus/goToPage-labels

Also update the browser history when the user *manually* change pages using the pageNumber-input (PR 12493 follow-up)
This commit is contained in:
Tim van der Meij 2020-11-03 23:06:49 +01:00 committed by GitHub
commit 4e13559cb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 18 deletions

View File

@ -2537,7 +2537,7 @@ function webViewerPageNumberChanged(evt) {
// Note that for `<input type="number">` HTML elements, an empty string will
// be returned for non-number inputs; hence we simply do nothing in that case.
if (evt.value !== "") {
pdfViewer.currentPageLabel = evt.value;
PDFViewerApplication.pdfLinkService.goToPage(evt.value);
}
// Ensure that the page number input displays the correct value, even if the

View File

@ -787,6 +787,22 @@ class BaseViewer {
this._scrollIntoView({ pageDiv: pageView.div });
}
/**
* @param {string} label - The page label.
* @returns {number|null} The page number corresponding to the page label,
* or `null` when no page labels exist and/or the input is invalid.
*/
pageLabelToPageNumber(label) {
if (!this._pageLabels) {
return null;
}
const i = this._pageLabels.indexOf(label);
if (i < 0) {
return null;
}
return i + 1;
}
/**
* @typedef ScrollPageIntoViewParameters
* @property {number} pageNumber - The page number.

View File

@ -59,9 +59,9 @@ class IPDFLinkService {
async goToDestination(dest) {}
/**
* @param {number} pageNumber - The page number.
* @param {number|string} val - The page number, or page label.
*/
goToPage(pageNumber) {}
goToPage(val) {}
/**
* @param dest - The PDF destination object.

View File

@ -290,7 +290,7 @@ class PDFHistory {
return;
}
if (this._destination && this._destination.page === pageNumber) {
if (this._destination?.page === pageNumber) {
// When the new page is identical to the one in `this._destination`, we
// don't want to add a potential duplicate entry in the browser history.
return;
@ -388,8 +388,7 @@ class PDFHistory {
if (
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("CHROME") &&
window.history.state &&
window.history.state.chromecomState
window.history.state?.chromecomState
) {
// history.state.chromecomState is managed by chromecom.js.
newState.chromecomState = window.history.state.chromecomState;
@ -397,7 +396,7 @@ class PDFHistory {
this._updateInternalState(destination, newState.uid);
let newUrl;
if (this._updateUrl && destination && destination.hash) {
if (this._updateUrl && destination?.hash) {
const baseUrl = document.location.href.split("#")[0];
// Prevent errors in Firefox.
if (!baseUrl.startsWith("file://")) {
@ -494,7 +493,7 @@ class PDFHistory {
return false;
}
const [perfEntry] = performance.getEntriesByType("navigation");
if (!perfEntry || perfEntry.type !== "reload") {
if (perfEntry?.type !== "reload") {
return false;
}
} else {
@ -523,7 +522,7 @@ class PDFHistory {
clearTimeout(this._updateViewareaTimeout);
this._updateViewareaTimeout = null;
}
if (removeTemporary && destination && destination.temporary) {
if (removeTemporary && destination?.temporary) {
// When the `destination` comes from the browser history,
// we no longer treat it as a *temporary* position.
delete destination.temporary;
@ -633,8 +632,7 @@ class PDFHistory {
if (
(typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("CHROME") &&
state &&
state.chromecomState &&
state?.chromecomState &&
!this._isValidState(state)) ||
!state
) {

View File

@ -182,6 +182,9 @@ class PDFLinkService {
* @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;
@ -203,9 +206,15 @@ class PDFLinkService {
/**
* This method will, when available, also update the browser history.
*
* @param {number} pageNumber - The page number.
* @param {number|string} val - The page number, or page label.
*/
goToPage(pageNumber) {
goToPage(val) {
if (!this.pdfDocument) {
return;
}
const pageNumber =
(typeof val === "string" && this.pdfViewer.pageLabelToPageNumber(val)) ||
val | 0;
if (
!(
Number.isInteger(pageNumber) &&
@ -213,9 +222,7 @@ class PDFLinkService {
pageNumber <= this.pagesCount
)
) {
console.error(
`PDFLinkService.goToPage: "${pageNumber}" is not a valid page number.`
);
console.error(`PDFLinkService.goToPage: "${val}" is not a valid page.`);
return;
}
@ -261,6 +268,9 @@ class PDFLinkService {
* @param {string} hash
*/
setHash(hash) {
if (!this.pdfDocument) {
return;
}
let pageNumber, dest;
if (hash.includes("=")) {
const params = parseQueryString(hash);
@ -557,9 +567,9 @@ class SimpleLinkService {
async goToDestination(dest) {}
/**
* @param {number} pageNumber - The page number.
* @param {number|string} val - The page number, or page label.
*/
goToPage(pageNumber) {}
goToPage(val) {}
/**
* @param dest - The PDF destination object.