From 943f08e50e16abd29b092712b09b0472c72234ac Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 9 Mar 2021 20:39:34 +0530 Subject: [PATCH] When text is selected in the viewer select the same text when opening the book for editing --- src/calibre/gui2/viewer/ui.py | 7 +++++-- src/calibre/gui2/viewer/web_view.py | 4 ++-- src/pyj/read_book/selection_bar.pyj | 2 +- src/pyj/read_book/view.pyj | 2 +- src/pyj/viewer-main.pyj | 4 ++-- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/calibre/gui2/viewer/ui.py b/src/calibre/gui2/viewer/ui.py index 2a76b1d8be..936b857c58 100644 --- a/src/calibre/gui2/viewer/ui.py +++ b/src/calibre/gui2/viewer/ui.py @@ -641,7 +641,7 @@ class EbookViewer(MainWindow): self.highlights_widget.refresh(highlights) self.save_annotations() - def edit_book(self, file_name, progress_frac): + def edit_book(self, file_name, progress_frac, selected_text): import subprocess from calibre.ebooks.oeb.polish.main import SUPPORTED from calibre.utils.ipc.launch import exe_path, macos_edit_book_bundle_path @@ -661,8 +661,11 @@ class EbookViewer(MainWindow): exe = os.path.join(macos_edit_book_bundle_path(), exe) else: exe = exe_path(exe) + cmd = [exe] + if selected_text: + cmd += ['--select-text', selected_text] with sanitize_env_vars(): - subprocess.Popen([exe, path, file_name]) + subprocess.Popen(cmd + [path, file_name]) def save_state(self): with vprefs: diff --git a/src/calibre/gui2/viewer/web_view.py b/src/calibre/gui2/viewer/web_view.py index 3344e00591..9b62f84326 100644 --- a/src/calibre/gui2/viewer/web_view.py +++ b/src/calibre/gui2/viewer/web_view.py @@ -273,7 +273,7 @@ class ViewerBridge(Bridge): open_url = from_js(object) speak_simple_text = from_js(object) tts = from_js(object, object) - edit_book = from_js(object, object) + edit_book = from_js(object, object, object) create_view = to_js() start_book_load = to_js() @@ -473,7 +473,7 @@ class WebView(RestartingWebEngineView): scrollbar_context_menu = pyqtSignal(object, object, object) close_prep_finished = pyqtSignal(object) highlights_changed = pyqtSignal(object) - edit_book = pyqtSignal(object, object) + edit_book = pyqtSignal(object, object, object) shortcuts_changed = pyqtSignal(object) paged_mode_changed = pyqtSignal() standalone_misc_settings_changed = pyqtSignal(object) diff --git a/src/pyj/read_book/selection_bar.pyj b/src/pyj/read_book/selection_bar.pyj index ad1e325f7a..d3a5ad3c12 100644 --- a/src/pyj/read_book/selection_bar.pyj +++ b/src/pyj/read_book/selection_bar.pyj @@ -567,7 +567,7 @@ class SelectionBar: sc_name = shortcut_for_key_event(ev, self.view.keyboard_shortcut_map) if not sc_name: return - forwarded = {'toggle_highlights': True, 'extend_selection_by_word': True, 'shrink_selection_by_word': True} + forwarded = {'toggle_highlights': True, 'extend_selection_by_word': True, 'shrink_selection_by_word': True, 'edit_book': True} if sc_name is 'show_chrome': self.clear_selection() elif sc_name is 'copy_to_clipboard': diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index 5a2fe3b72c..23fc617815 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -556,7 +556,7 @@ class View: elif data.name is 'metadata': self.overlay.show_metadata() elif data.name is 'edit_book': - ui_operations.edit_book(current_spine_item(), self.current_file_progress_frac) + ui_operations.edit_book(current_spine_item(), self.current_file_progress_frac, self.currently_showing?.selection?.text) elif data.name is 'goto_location': self.overlay.show_ask_for_location() elif data.name is 'shrink_selection_by_word': diff --git a/src/pyj/viewer-main.pyj b/src/pyj/viewer-main.pyj index 735a58b117..f3a9965ad3 100644 --- a/src/pyj/viewer-main.pyj +++ b/src/pyj/viewer-main.pyj @@ -419,8 +419,8 @@ if window is window.top: to_python.speak_simple_text(text) ui_operations.tts = def(action, data): to_python.tts(action, data or v'{}') - ui_operations.edit_book = def (spine_name, frac): - to_python.edit_book(spine_name, frac) + ui_operations.edit_book = def (spine_name, frac, selected_text): + to_python.edit_book(spine_name, frac, selected_text or '') document.body.appendChild(E.div(id='view')) window.onerror = onerror