Skip to content

Commit 36de2d9

Browse files
authored
Merge pull request #20476 from calixteman/thumbnail_scroll
Use HTMLElement.scrollIntoView when showing a thumbnail
2 parents 8a42b82 + f9b8574 commit 36de2d9

3 files changed

Lines changed: 56 additions & 4 deletions

File tree

test/integration/thumbnail_view_spec.mjs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { closePages, loadAndWait } from "./test_utils.mjs";
1+
import { awaitPromise, closePages, loadAndWait } from "./test_utils.mjs";
22

33
describe("PDF Thumbnail View", () => {
44
describe("Works without errors", () => {
@@ -32,4 +32,52 @@ describe("PDF Thumbnail View", () => {
3232
);
3333
});
3434
});
35+
36+
describe("The view is scrolled correctly", () => {
37+
let pages;
38+
39+
beforeEach(async () => {
40+
pages = await loadAndWait("tracemonkey.pdf", "#sidebarToggleButton");
41+
});
42+
43+
afterEach(async () => {
44+
await closePages(pages);
45+
});
46+
47+
async function goToPage(page, number) {
48+
const handle = await page.evaluateHandle(
49+
num => [
50+
new Promise(resolve => {
51+
const container = document.getElementById("thumbnailView");
52+
container.addEventListener("scrollend", resolve, { once: true });
53+
// eslint-disable-next-line no-undef
54+
PDFViewerApplication.pdfLinkService.goToPage(num);
55+
}),
56+
],
57+
number
58+
);
59+
return awaitPromise(handle);
60+
}
61+
62+
it("should scroll the view", async () => {
63+
await Promise.all(
64+
pages.map(async ([browserName, page]) => {
65+
await page.click("#sidebarToggleButton");
66+
67+
for (const pageNum of [14, 1, 13, 2]) {
68+
await goToPage(page, pageNum);
69+
const thumbSelector = `.thumbnailImage[data-l10n-args='{"page":${pageNum}}']`;
70+
await page.waitForSelector(
71+
`.thumbnail:has(${thumbSelector}).selected`,
72+
{ visible: true }
73+
);
74+
const src = await page.$eval(thumbSelector, el => el.src);
75+
expect(src)
76+
.withContext(`In ${browserName}`)
77+
.toMatch(/^blob:http:/);
78+
}
79+
})
80+
);
81+
});
82+
});
3583
});

web/pdf_thumbnail_viewer.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ import {
2424
getVisibleElements,
2525
isValidRotation,
2626
RenderingStates,
27-
scrollIntoView,
2827
watchScroll,
2928
} from "./ui_utils.js";
3029
import { PDFThumbnailView } from "./pdf_thumbnail_view.js";
3130

32-
const THUMBNAIL_SCROLL_MARGIN = -19;
3331
const THUMBNAIL_SELECTED_CLASS = "selected";
32+
const SCROLL_OPTIONS = {
33+
behavior: "instant",
34+
container: "nearest",
35+
block: "nearest",
36+
};
3437

3538
/**
3639
* @typedef {Object} PDFThumbnailViewerOptions
@@ -139,7 +142,7 @@ class PDFThumbnailViewer {
139142
}
140143
}
141144
if (shouldScroll) {
142-
scrollIntoView(thumbnailView.div, { top: THUMBNAIL_SCROLL_MARGIN });
145+
thumbnailView.div.scrollIntoView(SCROLL_OPTIONS);
143146
}
144147
}
145148

web/viewer.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,7 @@ body {
729729
height: auto;
730730

731731
> .thumbnail {
732+
scroll-margin-block: 19px;
732733
width: var(--thumbnail-width);
733734
margin: 0 10px 5px;
734735
padding: 1px;

0 commit comments

Comments
 (0)