Move scrolling the selected match into view from the find controller to the text layer builder

The find controller should only coordinate finding a string in the
document and should not be responsible for presenting the matches to the
user. The text layer builder already contains the logic to render the
matches in the viewer, so it should also take care of scrolling the
selected match into view.
This commit is contained in:
Tim van der Meij 2018-09-12 15:42:29 +02:00
parent ede414554e
commit 67e1e39f99
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762
2 changed files with 13 additions and 25 deletions

View File

@ -16,7 +16,6 @@
import { createPromiseCapability } from 'pdfjs-lib';
import { getCharacterType } from './pdf_find_utils';
import { getGlobalEventBus } from './dom_events';
import { scrollIntoView } from './ui_utils';
const FindState = {
FOUND: 0,
@ -25,8 +24,6 @@ const FindState = {
PENDING: 3,
};
const FIND_SCROLL_OFFSET_TOP = -50;
const FIND_SCROLL_OFFSET_LEFT = -400;
const FIND_TIMEOUT = 250; // ms
const CHARACTERS_TO_NORMALIZE = {
@ -130,26 +127,6 @@ class PDFFindController {
});
}
/**
* Called from the text layer when match presentation is updated.
*
* @param {number} pageIndex - The index of the page.
* @param {number} matchIndex - The index of the match.
* @param {Array} elements - Text layer `div` elements.
* @param {number} beginIdx - Start index of the `div` array for the match.
*/
updateMatchPosition(pageIndex, matchIndex, elements, beginIdx) {
if (this.selected.matchIdx === matchIndex &&
this.selected.pageIdx === pageIndex) {
const spot = {
top: FIND_SCROLL_OFFSET_TOP,
left: FIND_SCROLL_OFFSET_LEFT,
};
scrollIntoView(elements[beginIdx], spot,
/* skipOverflowHiddenElements = */ true);
}
}
_normalize(text) {
return text.replace(this._normalizationRegex, function(ch) {
return CHARACTERS_TO_NORMALIZE[ch];

View File

@ -15,8 +15,11 @@
import { getGlobalEventBus } from './dom_events';
import { renderTextLayer } from 'pdfjs-lib';
import { scrollIntoView } from './ui_utils';
const EXPAND_DIVS_TIMEOUT = 300; // ms
const MATCH_SCROLL_OFFSET_TOP = -50;
const MATCH_SCROLL_OFFSET_LEFT = -400;
/**
* @typedef {Object} TextLayerBuilderOptions
@ -243,9 +246,17 @@ class TextLayerBuilder {
let isSelected = (isSelectedPage && i === selectedMatchIdx);
let highlightSuffix = (isSelected ? ' selected' : '');
// Scroll the selected match into view.
if (this.findController) {
this.findController.updateMatchPosition(pageIdx, i, textDivs,
begin.divIdx);
if (this.findController.selected.matchIdx === i &&
this.findController.selected.pageIdx === pageIdx) {
const spot = {
top: MATCH_SCROLL_OFFSET_TOP,
left: MATCH_SCROLL_OFFSET_LEFT,
};
scrollIntoView(textDivs[begin.divIdx], spot,
/* skipOverflowHiddenElements = */ true);
}
}
// Match inside new div.