Skip to content

Commit

Permalink
Also update the browser history when the user *manually* change pages…
Browse files Browse the repository at this point in the history
… using the pageNumber-input (PR 12493 follow-up)

This patch addresses a review comment, which pointed out that we should *also* handle the pageNumber-input, from PR 12493.

Given that a user *manually* changing pages using the pageNumber-input, on the toolbar, could be regarded as a pretty strong indication of user-intent w.r.t. navigation in the document, hence I suppose that updating the browser history in this case as well probably won't hurt.
  • Loading branch information
Snuffleupagus committed Nov 1, 2020
1 parent 4eaa058 commit 911948c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 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
15 changes: 8 additions & 7 deletions web/pdf_link_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +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 @@ -566,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 911948c

Please sign in to comment.