Skip to content

Commit

Permalink
Merge pull request #12559 from Snuffleupagus/goToPage-labels
Browse files Browse the repository at this point in the history
Also update the browser history when the user *manually* change pages using the pageNumber-input (PR 12493 follow-up)
  • Loading branch information
timvandermeij authored Nov 3, 2020
2 parents 3e52098 + 322b107 commit 4e13559
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
2 changes: 1 addition & 1 deletion web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions web/base_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions web/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 6 additions & 8 deletions web/pdf_history.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -388,16 +388,15 @@ 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;
}
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://")) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -633,8 +632,7 @@ class PDFHistory {
if (
(typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("CHROME") &&
state &&
state.chromecomState &&
state?.chromecomState &&
!this._isValidState(state)) ||
!state
) {
Expand Down
24 changes: 17 additions & 7 deletions web/pdf_link_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -203,19 +206,23 @@ 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) &&
pageNumber > 0 &&
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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 4e13559

Please sign in to comment.