Merge pull request #20253 from calixteman/scroll_and_center

Add an option in scrollIntoView and goToXY in order to be able to center vertically or horizontally the point XY in the viewer container
This commit is contained in:
Tim van der Meij 2025-09-11 21:20:55 +02:00 committed by GitHub
commit 557eb2c7e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 68 additions and 2 deletions

View File

@ -17,6 +17,7 @@ import {
awaitPromise, awaitPromise,
closePages, closePages,
createPromise, createPromise,
getRect,
getSpanRectFromText, getSpanRectFromText,
loadAndWait, loadAndWait,
scrollIntoView, scrollIntoView,
@ -1420,4 +1421,49 @@ describe("PDF viewer", () => {
); );
}); });
}); });
describe("Scroll into view", () => {
let pages;
beforeEach(async () => {
pages = await loadAndWait(
"tracemonkey_annotation_on_page_8.pdf",
`.page[data-page-number = "1"] .endOfContent`
);
});
it("Check that the top right corner of the annotation is centered vertically", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
const handle = await page.evaluateHandle(() => [
new Promise(resolve => {
const container = document.getElementById("viewerContainer");
container.addEventListener("scrollend", resolve, {
once: true,
});
window.PDFViewerApplication.pdfLinkService.goToXY(
8,
43.55,
198.36,
{
center: "vertical",
}
);
}),
]);
await awaitPromise(handle);
const annotationSelector =
".page[data-page-number='8'] .stampAnnotation";
await page.waitForSelector(annotationSelector, { visible: true });
const rect = await getRect(page, annotationSelector);
const containerRect = await getRect(page, "#viewerContainer");
expect(
Math.abs(2 * (rect.y - containerRect.y) - containerRect.height)
)
.withContext(`In ${browserName}`)
.toBeLessThan(1);
})
);
});
});
}); });

View File

@ -742,3 +742,4 @@
!tracemonkey_with_annotations.pdf !tracemonkey_with_annotations.pdf
!tracemonkey_with_editable_annotations.pdf !tracemonkey_with_editable_annotations.pdf
!bug1980958.pdf !bug1980958.pdf
!tracemonkey_annotation_on_page_8.pdf

Binary file not shown.

View File

@ -244,12 +244,14 @@ class PDFLinkService {
* @param {number} pageNumber - The page number to scroll to. * @param {number} pageNumber - The page number to scroll to.
* @param {number} x - The x-coordinate to scroll to in page coordinates. * @param {number} x - The x-coordinate to scroll to in page coordinates.
* @param {number} y - The y-coordinate to scroll to in page coordinates. * @param {number} y - The y-coordinate to scroll to in page coordinates.
* @param {Object} [options]
*/ */
goToXY(pageNumber, x, y) { goToXY(pageNumber, x, y, options = {}) {
this.pdfViewer.scrollPageIntoView({ this.pdfViewer.scrollPageIntoView({
pageNumber, pageNumber,
destArray: [null, { name: "XYZ" }, x, y], destArray: [null, { name: "XYZ" }, x, y],
ignoreDestinationZoom: true, ignoreDestinationZoom: true,
...options,
}); });
} }

View File

@ -1554,6 +1554,9 @@ class PDFViewer {
* The default value is `false`. * The default value is `false`.
* @property {boolean} [ignoreDestinationZoom] - Ignore the zoom argument in * @property {boolean} [ignoreDestinationZoom] - Ignore the zoom argument in
* the destination array. The default value is `false`. * the destination array. The default value is `false`.
* @property {string} [center] - Center the view on the specified coordinates.
* The default value is `null`. Possible values are: `null` (don't center),
* `horizontal`, `vertical` and `both`.
*/ */
/** /**
@ -1565,6 +1568,7 @@ class PDFViewer {
destArray = null, destArray = null,
allowNegativeOffset = false, allowNegativeOffset = false,
ignoreDestinationZoom = false, ignoreDestinationZoom = false,
center = null,
}) { }) {
if (!this.pdfDocument) { if (!this.pdfDocument) {
return; return;
@ -1687,7 +1691,20 @@ class PDFViewer {
let left = Math.min(boundingRect[0][0], boundingRect[1][0]); let left = Math.min(boundingRect[0][0], boundingRect[1][0]);
let top = Math.min(boundingRect[0][1], boundingRect[1][1]); let top = Math.min(boundingRect[0][1], boundingRect[1][1]);
if (!allowNegativeOffset) { if (center) {
if (center === "both" || center === "vertical") {
top -=
(this.container.clientHeight -
Math.abs(boundingRect[1][1] - boundingRect[0][1])) /
2;
}
if (center === "both" || center === "horizontal") {
left -=
(this.container.clientWidth -
Math.abs(boundingRect[1][0] - boundingRect[0][0])) /
2;
}
} else if (!allowNegativeOffset) {
// Some bad PDF generators will create destinations with e.g. top values // Some bad PDF generators will create destinations with e.g. top values
// that exceeds the page height. Ensure that offsets are not negative, // that exceeds the page height. Ensure that offsets are not negative,
// to prevent a previous page from becoming visible (fixes bug 874482). // to prevent a previous page from becoming visible (fixes bug 874482).