From 67e1e39f99fc78e4c423a4d3684275f70389c6a8 Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Wed, 12 Sep 2018 15:42:29 +0200 Subject: [PATCH] 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. --- web/pdf_find_controller.js | 23 ----------------------- web/text_layer_builder.js | 15 +++++++++++++-- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/web/pdf_find_controller.js b/web/pdf_find_controller.js index 030a1a484..815a3a93c 100644 --- a/web/pdf_find_controller.js +++ b/web/pdf_find_controller.js @@ -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]; diff --git a/web/text_layer_builder.js b/web/text_layer_builder.js index 5b4bf6ff8..da7833291 100644 --- a/web/text_layer_builder.js +++ b/web/text_layer_builder.js @@ -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.