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 // 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. // be returned for non-number inputs; hence we simply do nothing in that case.
if (evt.value !== "") { 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 // 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 }); 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 * @typedef ScrollPageIntoViewParameters
* @property {number} pageNumber - The page number. * @property {number} pageNumber - The page number.

View File

@ -59,9 +59,9 @@ class IPDFLinkService {
async goToDestination(dest) {} 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. * @param dest - The PDF destination object.

View File

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

View File

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