From b13f5d6a0c3914c860fb629f3093890587da8aaf Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 24 Dec 2019 21:21:15 +0530 Subject: [PATCH] Viewer: Allow modifying the current selection using Ctrl+Shift+arrow. Fixes #1855942 [[Enhancements] Comments and select text with keyboard](https://bugs.launchpad.net/calibre/+bug/1855942) --- src/pyj/read_book/iframe.pyj | 5 +++++ src/pyj/read_book/shortcuts.pyj | 12 ++++++++++++ src/pyj/read_book/view.pyj | 5 ++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index 50fde08041..ea8d5ee03a 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -106,11 +106,16 @@ class IframeBoss: 'get_current_cfi': self.get_current_cfi, 'set_forward_keypresses': self.set_forward_keypresses, 'set_reference_mode': self.set_reference_mode, + 'modify_selection': self.modify_selection, } self.comm = IframeClient(handlers) self.last_window_ypos = 0 self.length_before = None + def modify_selection(self, data): + sel = window.getSelection() + sel.modify('extend', data.direction, data.granularity) + def set_forward_keypresses(self, data): self.forward_keypresses = data.forward diff --git a/src/pyj/read_book/shortcuts.pyj b/src/pyj/read_book/shortcuts.pyj index efd4c3e112..99f3cb1937 100644 --- a/src/pyj/read_book/shortcuts.pyj +++ b/src/pyj/read_book/shortcuts.pyj @@ -246,6 +246,18 @@ def shortcuts_definition(): _('Search for next occurrence of selected text'), ), + 'extend_selection_by_word': desc( + v"['Ctrl+Shift+ArrowRight']", + 'ui', + _('Alter the current selection forward by a word'), + ), + + 'shrink_selection_by_word': desc( + v"['Ctrl+Shift+ArrowLeft']", + 'ui', + _('Alter the current selection backwards by a word'), + ), + 'show_chrome': desc( v"['Escape', 'ContextMenu']", 'ui', diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index 1557b58e40..2208ad31d5 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -405,7 +405,10 @@ class View: self.overlay.show_metadata() elif data.name is 'goto_location': self.overlay.show_ask_for_location() - + elif data.name is 'shrink_selection_by_word': + self.iframe_wrapper.send_message('modify_selection', direction='backward', granularity='word') + elif data.name is 'extend_selection_by_word': + self.iframe_wrapper.send_message('modify_selection', direction='forward', granularity='word') def on_selection_change(self, data): self.currently_showing.selected_text = data.text