Merge pull request #13600 from Snuffleupagus/scrollMatchIntoView-selectedLeft

Take the position of the `selected` element into account when scrolling matches (issue 13596)
This commit is contained in:
Tim van der Meij 2021-06-26 14:04:20 +02:00 committed by GitHub
commit 82660296d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 15 deletions

View File

@ -210,7 +210,12 @@ class PDFFindController {
}); });
} }
scrollMatchIntoView({ element = null, pageIndex = -1, matchIndex = -1 }) { scrollMatchIntoView({
element = null,
selectedLeft = 0,
pageIndex = -1,
matchIndex = -1,
}) {
if (!this._scrollMatches || !element) { if (!this._scrollMatches || !element) {
return; return;
} else if (matchIndex === -1 || matchIndex !== this._selected.matchIdx) { } else if (matchIndex === -1 || matchIndex !== this._selected.matchIdx) {
@ -222,7 +227,7 @@ class PDFFindController {
const spot = { const spot = {
top: MATCH_SCROLL_OFFSET_TOP, top: MATCH_SCROLL_OFFSET_TOP,
left: MATCH_SCROLL_OFFSET_LEFT, left: selectedLeft + MATCH_SCROLL_OFFSET_LEFT,
}; };
scrollIntoView(element, spot, /* scrollMatches = */ true); scrollIntoView(element, spot, /* scrollMatches = */ true);
} }

View File

@ -227,7 +227,7 @@ class TextLayerBuilder {
function beginText(begin, className) { function beginText(begin, className) {
const divIdx = begin.divIdx; const divIdx = begin.divIdx;
textDivs[divIdx].textContent = ""; textDivs[divIdx].textContent = "";
appendTextToDiv(divIdx, 0, begin.offset, className); return appendTextToDiv(divIdx, 0, begin.offset, className);
} }
function appendTextToDiv(divIdx, fromOffset, toOffset, className) { function appendTextToDiv(divIdx, fromOffset, toOffset, className) {
@ -242,9 +242,10 @@ class TextLayerBuilder {
span.className = `${className} appended`; span.className = `${className} appended`;
span.appendChild(node); span.appendChild(node);
div.appendChild(span); div.appendChild(span);
return; return className.includes("selected") ? span.offsetLeft : 0;
} }
div.appendChild(node); div.appendChild(node);
return 0;
} }
let i0 = selectedMatchIdx, let i0 = selectedMatchIdx,
@ -263,15 +264,7 @@ class TextLayerBuilder {
const end = match.end; const end = match.end;
const isSelected = isSelectedPage && i === selectedMatchIdx; const isSelected = isSelectedPage && i === selectedMatchIdx;
const highlightSuffix = isSelected ? " selected" : ""; const highlightSuffix = isSelected ? " selected" : "";
let selectedLeft = 0;
if (isSelected) {
// Attempt to scroll the selected match into view.
findController.scrollMatchIntoView({
element: textDivs[begin.divIdx],
pageIndex: pageIdx,
matchIndex: selectedMatchIdx,
});
}
// Match inside new div. // Match inside new div.
if (!prevEnd || begin.divIdx !== prevEnd.divIdx) { if (!prevEnd || begin.divIdx !== prevEnd.divIdx) {
@ -286,14 +279,14 @@ class TextLayerBuilder {
} }
if (begin.divIdx === end.divIdx) { if (begin.divIdx === end.divIdx) {
appendTextToDiv( selectedLeft = appendTextToDiv(
begin.divIdx, begin.divIdx,
begin.offset, begin.offset,
end.offset, end.offset,
"highlight" + highlightSuffix "highlight" + highlightSuffix
); );
} else { } else {
appendTextToDiv( selectedLeft = appendTextToDiv(
begin.divIdx, begin.divIdx,
begin.offset, begin.offset,
infinity.offset, infinity.offset,
@ -305,6 +298,16 @@ class TextLayerBuilder {
beginText(end, "highlight end" + highlightSuffix); beginText(end, "highlight end" + highlightSuffix);
} }
prevEnd = end; prevEnd = end;
if (isSelected) {
// Attempt to scroll the selected match into view.
findController.scrollMatchIntoView({
element: textDivs[begin.divIdx],
selectedLeft,
pageIndex: pageIdx,
matchIndex: selectedMatchIdx,
});
}
} }
if (prevEnd) { if (prevEnd) {