From a3e0461c7306b472a57a67cb27d8209a6e4561bd Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 13 Apr 2020 12:33:07 +0530 Subject: [PATCH] Add a button to copy the current selection when creating highlights --- src/pyj/read_book/create_annotation.pyj | 11 +++++++++-- src/pyj/read_book/iframe.pyj | 6 ++++++ src/pyj/read_book/view.pyj | 4 ++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/pyj/read_book/create_annotation.pyj b/src/pyj/read_book/create_annotation.pyj index a1642f9cbb..6e26c12141 100644 --- a/src/pyj/read_book/create_annotation.pyj +++ b/src/pyj/read_book/create_annotation.pyj @@ -126,7 +126,7 @@ BAR_SIZE = 32 def create_bar(): ans = E.div( id=unique_id('annot-bar'), - style=f'height: {BAR_SIZE}px; width: 100vw; display: flex; justify-content: space-between;', + style=f'height: {BAR_SIZE}px; max-height: {BAR_SIZE}px; width: 100vw; display: flex; justify-content: space-between;', ) return ans @@ -167,7 +167,11 @@ class CreateAnnotation: container.appendChild(tb) button(tb, 'close', _('Cancel creation of highlight'), self.hide) button(tb, 'chevron-up', _('Scroll up'), self.scroll_up) - button(tb, 'check', _('Finish creation of highlight'), self.accept) + tb.appendChild(E.span(style=f'height: {tb.style.height}')) + if ui_operations.copy_selection: + button(tb.lastChild, 'copy', _('Copy to clipboard'), self.copy_to_clipboard) + tb.lastChild.appendChild(E.span('\xa0')) + button(tb.lastChild, 'check', _('Finish creation of highlight'), self.accept) middle = E.div(id=unique_id('middle'), style='display: none') self.middle_id = middle.id @@ -200,6 +204,9 @@ class CreateAnnotation: container.addEventListener('mousemove', self.mousemove_on_container, {'passive': False}) container.addEventListener('keydown', self.on_keydown, {'passive': False}) + def copy_to_clipboard(self): + self.view.iframe_wrapper.send_message('copy_selection') + def scroll_up(self): self.send_message('scroll', backwards=True) diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index 42270900ed..45ceebc3ed 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -131,6 +131,7 @@ class IframeBoss: 'show_search_result': self.show_search_result, 'handle_navigation_shortcut': self.on_handle_navigation_shortcut, 'annotations': self.annotations_msg_received, + 'copy_selection': self.copy_selection, } self.comm = IframeClient(handlers) self.last_window_ypos = 0 @@ -698,5 +699,10 @@ class IframeBoss: unwrap_crw(crw) v'delete self.annot_id_uuid_map[crw]' + def copy_selection(self): + text = window.getSelection().toString() + if text: + self.send_message('copy_text_to_clipboard', text=text) + def main(): main.boss = IframeBoss() diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index 48a89a0b90..088948ad36 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -263,6 +263,10 @@ class View: ui_operations.search_result_not_found(data.search_result) , 'annotations': self.on_annotations_message, + 'copy_text_to_clipboard': def(data): + if ui_operations.copy_selection: + ui_operations.copy_selection(data.text) + , } entry_point = None if runtime.is_standalone_viewer else 'read_book.iframe' if runtime.is_standalone_viewer: