From adf95e7a22bc4c97a13d4bb490b4b9e931594a7f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 9 Mar 2020 07:49:07 +0530 Subject: [PATCH] Viewer: Fix searching in flow mode not scrolling to display the search results. Fixes #1866519 [ebook-viewer 4.12 searching doesn't jump to results](https://bugs.launchpad.net/calibre/+bug/1866519) --- src/pyj/read_book/flow_mode.pyj | 12 ++++++++++++ src/pyj/read_book/iframe.pyj | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/pyj/read_book/flow_mode.pyj b/src/pyj/read_book/flow_mode.pyj index a7c1e8513f..80bc81d235 100644 --- a/src/pyj/read_book/flow_mode.pyj +++ b/src/pyj/read_book/flow_mode.pyj @@ -465,3 +465,15 @@ def auto_scroll_action(action): elif action is 'resume': auto_scroll_resume() return is_auto_scroll_active() + + +def ensure_selection_visible(): + s = window.getSelection() + if not s.anchorNode: + return + p = s.anchorNode + while p: + if p.scrollIntoView: + p.scrollIntoView() + return + p = p.parentNode diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index b5a2d7c883..cf4d256249 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -14,7 +14,7 @@ from read_book.flow_mode import ( anchor_funcs as flow_anchor_funcs, auto_scroll_action as flow_auto_scroll_action, flow_onwheel, flow_to_scroll_fraction, handle_gesture as flow_handle_gesture, handle_shortcut as flow_handle_shortcut, layout as flow_layout, - scroll_by_page as flow_scroll_by_page + scroll_by_page as flow_scroll_by_page, ensure_selection_visible ) from read_book.footnotes import is_footnote_link from read_book.globals import ( @@ -581,7 +581,9 @@ class IframeBoss: def show_search_result(self, data, from_load): if select_search_result(data.search_result): - if current_layout_mode() is not 'flow': + if current_layout_mode() is 'flow': + ensure_selection_visible() + else: snap_to_selection() else: self.send_message('search_result_not_found', search_result=data.search_result)