Skip to content

Commit 9babc14

Browse files
committed
Make horizontal padding relative to device width
The fixed -400px horizontal offset used by scrollIntoView led to horizontal scroll only moving part-way right on narrow screens. The highlights near the right-edge remained party or completely off screen. This centres the highlighted match on any viewport width while clamping the left margin to 20-400px. On very narrow screens the scrollbar now moves all the way to the right instead of stopping midway.
1 parent e5922f2 commit 9babc14

4 files changed

Lines changed: 93 additions & 4 deletions

File tree

test/integration/find_spec.mjs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,32 @@ describe("find bar", () => {
149149
);
150150
});
151151
});
152+
153+
describe("scrolls to the search result text for smaller viewports", () => {
154+
let pages;
155+
156+
beforeEach(async () => {
157+
pages = await loadAndWait("tracemonkey.pdf", ".textLayer", 100);
158+
});
159+
160+
afterEach(async () => {
161+
await closePages(pages);
162+
});
163+
164+
it("must scroll to the search result text", async () => {
165+
await Promise.all(
166+
pages.map(async ([browserName, page]) => {
167+
// Set a smaller viewport to simulate a mobile device
168+
await page.setViewport({ width: 350, height: 600 });
169+
await page.click("#viewFindButton");
170+
await page.waitForSelector("#findInput", { visible: true });
171+
await page.type("#findInput", "productivity");
172+
173+
const highlight = await page.waitForSelector(".textLayer .highlight");
174+
175+
expect(await highlight.isIntersectingViewport()).toBeTrue();
176+
})
177+
);
178+
});
179+
});
152180
});

test/integration/viewer_spec.mjs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,4 +1355,57 @@ describe("PDF viewer", () => {
13551355
);
13561356
});
13571357
});
1358+
1359+
describe("Pinch-zoom", () => {
1360+
let pages;
1361+
beforeEach(async () => {
1362+
pages = await loadAndWait(
1363+
"tracemonkey.pdf",
1364+
`.page[data-page-number = "1"] .endOfContent`
1365+
);
1366+
});
1367+
it("keeps the content under the pinch centre fixed on the screen", async () => {
1368+
await Promise.all(
1369+
pages.map(async ([browserName, page]) => {
1370+
if (browserName === "firefox") {
1371+
// Firefox does not support touch events on devices
1372+
// with no touch screen.
1373+
return;
1374+
}
1375+
1376+
const rect = await getSpanRectFromText(page, 1, "type-stable");
1377+
const originX = rect.x + rect.width / 2;
1378+
const originY = rect.y + rect.height / 2;
1379+
const rendered = await createPromise(page, resolve => {
1380+
const cb = e => {
1381+
if (e.pageNumber === 1) {
1382+
window.PDFViewerApplication.eventBus.off(
1383+
"textlayerrendered",
1384+
cb
1385+
);
1386+
resolve();
1387+
}
1388+
};
1389+
window.PDFViewerApplication.eventBus.on("textlayerrendered", cb);
1390+
});
1391+
const client = await page.target().createCDPSession();
1392+
await client.send("Input.synthesizePinchGesture", {
1393+
x: originX,
1394+
y: originY,
1395+
scaleFactor: 3,
1396+
gestureSourceType: "touch",
1397+
});
1398+
await awaitPromise(rendered);
1399+
const spanHandle = await page.evaluateHandle(() =>
1400+
Array.from(
1401+
document.querySelectorAll(
1402+
'.page[data-page-number="1"] .textLayer span'
1403+
)
1404+
).find(span => span.textContent.includes("type-stable"))
1405+
);
1406+
expect(await spanHandle.isIntersectingViewport()).toBeTrue();
1407+
})
1408+
);
1409+
});
1410+
});
13581411
});

web/pdf_find_controller.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const FindState = {
2929

3030
const FIND_TIMEOUT = 250; // ms
3131
const MATCH_SCROLL_OFFSET_TOP = -50; // px
32-
const MATCH_SCROLL_OFFSET_LEFT = -400; // px
3332

3433
const CHARACTERS_TO_NORMALIZE = {
3534
"\u2010": "-", // Hyphen
@@ -573,10 +572,9 @@ class PDFFindController {
573572
return;
574573
}
575574
this._scrollMatches = false; // Ensure that scrolling only happens once.
576-
577575
const spot = {
578576
top: MATCH_SCROLL_OFFSET_TOP,
579-
left: selectedLeft + MATCH_SCROLL_OFFSET_LEFT,
577+
left: selectedLeft,
580578
};
581579
scrollIntoView(element, spot, /* scrollMatches = */ true);
582580
}

web/ui_utils.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,17 @@ function scrollIntoView(element, spot, scrollMatches = false) {
120120
offsetY += spot.top;
121121
}
122122
if (spot.left !== undefined) {
123-
offsetX += spot.left;
123+
if (scrollMatches) {
124+
const elementWidth = element.getBoundingClientRect().width;
125+
const padding = MathClamp(
126+
(parent.clientWidth - elementWidth) / 2,
127+
20,
128+
400
129+
);
130+
offsetX += spot.left - padding;
131+
} else {
132+
offsetX += spot.left;
133+
}
124134
parent.scrollLeft = offsetX;
125135
}
126136
}

0 commit comments

Comments
 (0)