Add a button to copy the current selection when creating highlights

This commit is contained in:
Kovid Goyal 2020-04-13 12:33:07 +05:30
parent 409ac0b9f8
commit a3e0461c73
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 19 additions and 2 deletions

View File

@ -126,7 +126,7 @@ BAR_SIZE = 32
def create_bar(): def create_bar():
ans = E.div( ans = E.div(
id=unique_id('annot-bar'), 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 return ans
@ -167,7 +167,11 @@ class CreateAnnotation:
container.appendChild(tb) container.appendChild(tb)
button(tb, 'close', _('Cancel creation of highlight'), self.hide) button(tb, 'close', _('Cancel creation of highlight'), self.hide)
button(tb, 'chevron-up', _('Scroll up'), self.scroll_up) 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') middle = E.div(id=unique_id('middle'), style='display: none')
self.middle_id = middle.id self.middle_id = middle.id
@ -200,6 +204,9 @@ class CreateAnnotation:
container.addEventListener('mousemove', self.mousemove_on_container, {'passive': False}) container.addEventListener('mousemove', self.mousemove_on_container, {'passive': False})
container.addEventListener('keydown', self.on_keydown, {'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): def scroll_up(self):
self.send_message('scroll', backwards=True) self.send_message('scroll', backwards=True)

View File

@ -131,6 +131,7 @@ class IframeBoss:
'show_search_result': self.show_search_result, 'show_search_result': self.show_search_result,
'handle_navigation_shortcut': self.on_handle_navigation_shortcut, 'handle_navigation_shortcut': self.on_handle_navigation_shortcut,
'annotations': self.annotations_msg_received, 'annotations': self.annotations_msg_received,
'copy_selection': self.copy_selection,
} }
self.comm = IframeClient(handlers) self.comm = IframeClient(handlers)
self.last_window_ypos = 0 self.last_window_ypos = 0
@ -698,5 +699,10 @@ class IframeBoss:
unwrap_crw(crw) unwrap_crw(crw)
v'delete self.annot_id_uuid_map[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(): def main():
main.boss = IframeBoss() main.boss = IframeBoss()

View File

@ -263,6 +263,10 @@ class View:
ui_operations.search_result_not_found(data.search_result) ui_operations.search_result_not_found(data.search_result)
, ,
'annotations': self.on_annotations_message, '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' entry_point = None if runtime.is_standalone_viewer else 'read_book.iframe'
if runtime.is_standalone_viewer: if runtime.is_standalone_viewer: