Skip to content

Commit e1bdc34

Browse files
committed
Add an option in scrollIntoView and goToXY in order to be able to center vertically or horizontally the point XY in the viewer container
1 parent 5b7f9ca commit e1bdc34

5 files changed

Lines changed: 68 additions & 2 deletions

File tree

test/integration/viewer_spec.mjs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
awaitPromise,
1818
closePages,
1919
createPromise,
20+
getRect,
2021
getSpanRectFromText,
2122
loadAndWait,
2223
scrollIntoView,
@@ -1416,4 +1417,49 @@ describe("PDF viewer", () => {
14161417
);
14171418
});
14181419
});
1420+
1421+
describe("Scroll into view", () => {
1422+
let pages;
1423+
1424+
beforeEach(async () => {
1425+
pages = await loadAndWait(
1426+
"tracemonkey_annotation_on_page_8.pdf",
1427+
`.page[data-page-number = "1"] .endOfContent`
1428+
);
1429+
});
1430+
1431+
it("Check that the top right corner of the annotation is centered vertically", async () => {
1432+
await Promise.all(
1433+
pages.map(async ([browserName, page]) => {
1434+
const handle = await page.evaluateHandle(() => [
1435+
new Promise(resolve => {
1436+
const container = document.getElementById("viewerContainer");
1437+
container.addEventListener("scrollend", resolve, {
1438+
once: true,
1439+
});
1440+
window.PDFViewerApplication.pdfLinkService.goToXY(
1441+
8,
1442+
43.55,
1443+
198.36,
1444+
{
1445+
center: "vertical",
1446+
}
1447+
);
1448+
}),
1449+
]);
1450+
await awaitPromise(handle);
1451+
const annotationSelector =
1452+
".page[data-page-number='8'] .stampAnnotation";
1453+
await page.waitForSelector(annotationSelector, { visible: true });
1454+
const rect = await getRect(page, annotationSelector);
1455+
const containerRect = await getRect(page, "#viewerContainer");
1456+
expect(
1457+
Math.abs(2 * (rect.y - containerRect.y) - containerRect.height)
1458+
)
1459+
.withContext(`In ${browserName}`)
1460+
.toBeLessThan(1);
1461+
})
1462+
);
1463+
});
1464+
});
14191465
});

test/pdfs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,3 +742,4 @@
742742
!tracemonkey_with_annotations.pdf
743743
!tracemonkey_with_editable_annotations.pdf
744744
!bug1980958.pdf
745+
!tracemonkey_annotation_on_page_8.pdf
1 MB
Binary file not shown.

web/pdf_link_service.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,14 @@ class PDFLinkService {
244244
* @param {number} pageNumber - The page number to scroll to.
245245
* @param {number} x - The x-coordinate to scroll to in page coordinates.
246246
* @param {number} y - The y-coordinate to scroll to in page coordinates.
247+
* @param {Object} [options]
247248
*/
248-
goToXY(pageNumber, x, y) {
249+
goToXY(pageNumber, x, y, options = {}) {
249250
this.pdfViewer.scrollPageIntoView({
250251
pageNumber,
251252
destArray: [null, { name: "XYZ" }, x, y],
252253
ignoreDestinationZoom: true,
254+
...options,
253255
});
254256
}
255257

web/pdf_viewer.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,9 @@ class PDFViewer {
15541554
* The default value is `false`.
15551555
* @property {boolean} [ignoreDestinationZoom] - Ignore the zoom argument in
15561556
* the destination array. The default value is `false`.
1557+
* @property {string} [center] - Center the view on the specified coordinates.
1558+
* The default value is `null`. Possible values are: `null` (don't center),
1559+
* `horizontal`, `vertical` and `both`.
15571560
*/
15581561

15591562
/**
@@ -1565,6 +1568,7 @@ class PDFViewer {
15651568
destArray = null,
15661569
allowNegativeOffset = false,
15671570
ignoreDestinationZoom = false,
1571+
center = null,
15681572
}) {
15691573
if (!this.pdfDocument) {
15701574
return;
@@ -1687,7 +1691,20 @@ class PDFViewer {
16871691
let left = Math.min(boundingRect[0][0], boundingRect[1][0]);
16881692
let top = Math.min(boundingRect[0][1], boundingRect[1][1]);
16891693

1690-
if (!allowNegativeOffset) {
1694+
if (center) {
1695+
if (center === "both" || center === "vertical") {
1696+
top -=
1697+
(this.container.clientHeight -
1698+
Math.abs(boundingRect[1][1] - boundingRect[0][1])) /
1699+
2;
1700+
}
1701+
if (center === "both" || center === "horizontal") {
1702+
left -=
1703+
(this.container.clientWidth -
1704+
Math.abs(boundingRect[1][0] - boundingRect[0][0])) /
1705+
2;
1706+
}
1707+
} else if (!allowNegativeOffset) {
16911708
// Some bad PDF generators will create destinations with e.g. top values
16921709
// that exceeds the page height. Ensure that offsets are not negative,
16931710
// to prevent a previous page from becoming visible (fixes bug 874482).

0 commit comments

Comments
 (0)