From 05516bfd546c9ee34c82b1ed7d9b3619330cc5fe Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 21 Jul 2020 14:15:49 +0530 Subject: [PATCH] Track the selection position in the chrome --- src/pyj/read_book/iframe.pyj | 15 +++++++++++++-- src/pyj/read_book/view.pyj | 10 +++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index 3d38eb24db..eea76d5ed0 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -505,6 +505,9 @@ class IframeBoss: fpf = progress_frac() self.send_message( 'update_progress_frac', progress_frac=pf, file_progress_frac=fpf) + sel = window.getSelection() + if sel and not sel.isCollapsed: + self.send_message('update_selection_position', selection_extents=selection_extents(current_layout_mode() is 'flow')) def onresize(self): self.send_message('request_size') @@ -516,8 +519,16 @@ class IframeBoss: self.onresize_stage2() def onselectionchange(self): - if self.content_ready: - self.send_message('selectionchange', text=document.getSelection().toString()) + if not self.content_ready: + return + sel = window.getSelection() + text = '' + collapsed = not sel or sel.isCollapsed + if not collapsed: + text = sel.toString() + self.send_message( + 'selectionchange', text=text, empty=v'!!collapsed', + selection_extents=selection_extents(current_layout_mode() is 'flow')) def onresize_stage2(self): if scroll_viewport.width() is self.last_window_width and scroll_viewport.height() is self.last_window_height: diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index 7bbd97c959..136d66dfc3 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -261,6 +261,7 @@ class View: 'request_size': self.on_request_size, 'scroll_to_anchor': self.on_scroll_to_anchor, 'selectionchange': self.on_selection_change, + 'update_selection_position': self.update_selection_position, 'columns_per_screen_changed': self.on_columns_per_screen_changed, 'show_chrome': self.show_chrome, 'show_footnote': self.on_show_footnote, @@ -513,8 +514,15 @@ class View: def on_selection_change(self, data): self.currently_showing.selected_text = data.text + self.currently_showing.has_selection = not data.empty + self.currently_showing.selection_start = data.selection_extents.start + self.currently_showing.selection_end = data.selection_extents.end if ui_operations.selection_changed: - ui_operations.selection_changed(data.text) + ui_operations.selection_changed(self.currently_showing.selected_text) + + def update_selection_position(self, data): + self.currently_showing.selection_start = data.selection_extents.start + self.currently_showing.selection_end = data.selection_extents.end def on_columns_per_screen_changed(self, data): sd = get_session_data()