Update BaseViewer.scrollPageIntoView
to always validate the pageNumber
parameter
Note that when e.g. presentation mode is active, we fail[1] to ensure that the `pageNumber` parameter is actually an integer before calling `_setCurrentPageNumber` (that method expects the argument be an integer). Also changes the method signature, of `scrollPageIntoView`, to use object destructuring instead. --- [1] Most likely, this is actually *my* oversight :-)
This commit is contained in:
parent
2194aef03e
commit
2e38b7d00b
@ -660,25 +660,23 @@ class BaseViewer {
|
|||||||
* Scrolls page into view.
|
* Scrolls page into view.
|
||||||
* @param {ScrollPageIntoViewParameters} params
|
* @param {ScrollPageIntoViewParameters} params
|
||||||
*/
|
*/
|
||||||
scrollPageIntoView(params) {
|
scrollPageIntoView({ pageNumber, destArray = null,
|
||||||
|
allowNegativeOffset = false, }) {
|
||||||
if (!this.pdfDocument) {
|
if (!this.pdfDocument) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let pageNumber = params.pageNumber || 0;
|
const pageView = (Number.isInteger(pageNumber) &&
|
||||||
let dest = params.destArray || null;
|
this._pages[pageNumber - 1]);
|
||||||
let allowNegativeOffset = params.allowNegativeOffset || false;
|
|
||||||
|
|
||||||
if (this.isInPresentationMode || !dest) {
|
|
||||||
this._setCurrentPageNumber(pageNumber, /* resetCurrentPageView = */ true);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let pageView = this._pages[pageNumber - 1];
|
|
||||||
if (!pageView) {
|
if (!pageView) {
|
||||||
console.error(
|
console.error(
|
||||||
`${this._name}.scrollPageIntoView: Invalid "pageNumber" parameter.`);
|
`${this._name}.scrollPageIntoView: Invalid "pageNumber" parameter.`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.isInPresentationMode || !destArray) {
|
||||||
|
this._setCurrentPageNumber(pageNumber, /* resetCurrentPageView = */ true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
let x = 0, y = 0;
|
let x = 0, y = 0;
|
||||||
let width = 0, height = 0, widthScale, heightScale;
|
let width = 0, height = 0, widthScale, heightScale;
|
||||||
let changeOrientation = (pageView.rotation % 180 === 0 ? false : true);
|
let changeOrientation = (pageView.rotation % 180 === 0 ? false : true);
|
||||||
@ -687,11 +685,11 @@ class BaseViewer {
|
|||||||
let pageHeight = (changeOrientation ? pageView.width : pageView.height) /
|
let pageHeight = (changeOrientation ? pageView.width : pageView.height) /
|
||||||
pageView.scale / CSS_UNITS;
|
pageView.scale / CSS_UNITS;
|
||||||
let scale = 0;
|
let scale = 0;
|
||||||
switch (dest[1].name) {
|
switch (destArray[1].name) {
|
||||||
case 'XYZ':
|
case 'XYZ':
|
||||||
x = dest[2];
|
x = destArray[2];
|
||||||
y = dest[3];
|
y = destArray[3];
|
||||||
scale = dest[4];
|
scale = destArray[4];
|
||||||
// If x and/or y coordinates are not supplied, default to
|
// If x and/or y coordinates are not supplied, default to
|
||||||
// _top_ left of the page (not the obvious bottom left,
|
// _top_ left of the page (not the obvious bottom left,
|
||||||
// since aligning the bottom of the intended page with the
|
// since aligning the bottom of the intended page with the
|
||||||
@ -705,7 +703,7 @@ class BaseViewer {
|
|||||||
break;
|
break;
|
||||||
case 'FitH':
|
case 'FitH':
|
||||||
case 'FitBH':
|
case 'FitBH':
|
||||||
y = dest[2];
|
y = destArray[2];
|
||||||
scale = 'page-width';
|
scale = 'page-width';
|
||||||
// According to the PDF spec, section 12.3.2.2, a `null` value in the
|
// According to the PDF spec, section 12.3.2.2, a `null` value in the
|
||||||
// parameter should maintain the position relative to the new page.
|
// parameter should maintain the position relative to the new page.
|
||||||
@ -716,16 +714,16 @@ class BaseViewer {
|
|||||||
break;
|
break;
|
||||||
case 'FitV':
|
case 'FitV':
|
||||||
case 'FitBV':
|
case 'FitBV':
|
||||||
x = dest[2];
|
x = destArray[2];
|
||||||
width = pageWidth;
|
width = pageWidth;
|
||||||
height = pageHeight;
|
height = pageHeight;
|
||||||
scale = 'page-height';
|
scale = 'page-height';
|
||||||
break;
|
break;
|
||||||
case 'FitR':
|
case 'FitR':
|
||||||
x = dest[2];
|
x = destArray[2];
|
||||||
y = dest[3];
|
y = destArray[3];
|
||||||
width = dest[4] - x;
|
width = destArray[4] - x;
|
||||||
height = dest[5] - y;
|
height = destArray[5] - y;
|
||||||
let hPadding = this.removePageBorders ? 0 : SCROLLBAR_PADDING;
|
let hPadding = this.removePageBorders ? 0 : SCROLLBAR_PADDING;
|
||||||
let vPadding = this.removePageBorders ? 0 : VERTICAL_PADDING;
|
let vPadding = this.removePageBorders ? 0 : VERTICAL_PADDING;
|
||||||
|
|
||||||
@ -736,8 +734,8 @@ class BaseViewer {
|
|||||||
scale = Math.min(Math.abs(widthScale), Math.abs(heightScale));
|
scale = Math.min(Math.abs(widthScale), Math.abs(heightScale));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.error(`${this._name}.scrollPageIntoView: "${dest[1].name}" ` +
|
console.error(`${this._name}.scrollPageIntoView: ` +
|
||||||
'is not a valid destination type.');
|
`"${destArray[1].name}" is not a valid destination type.`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -747,7 +745,7 @@ class BaseViewer {
|
|||||||
this.currentScaleValue = DEFAULT_SCALE_VALUE;
|
this.currentScaleValue = DEFAULT_SCALE_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scale === 'page-fit' && !dest[4]) {
|
if (scale === 'page-fit' && !destArray[4]) {
|
||||||
this._scrollIntoView({
|
this._scrollIntoView({
|
||||||
pageDiv: pageView.div,
|
pageDiv: pageView.div,
|
||||||
pageNumber,
|
pageNumber,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user