diff --git a/src/calibre/gui2/viewer/bookmarks.py b/src/calibre/gui2/viewer/bookmarks.py index 123a40e251..42c75b8bdf 100644 --- a/src/calibre/gui2/viewer/bookmarks.py +++ b/src/calibre/gui2/viewer/bookmarks.py @@ -333,10 +333,11 @@ class BookmarkManager(QWidget): if not ok or not title: return title = self.uniqify_bookmark_title(title) + cfi = (pos_data.get('selection_bounds') or {}).get('start') or pos_data['cfi'] bm = { 'title': title, 'pos_type': 'epubcfi', - 'pos': pos_data['cfi'], + 'pos': cfi, 'timestamp': utcnow().isoformat(), } bookmarks = self.get_bookmarks() diff --git a/src/calibre/gui2/viewer/ui.py b/src/calibre/gui2/viewer/ui.py index 802dd9b7c8..6d7d686848 100644 --- a/src/calibre/gui2/viewer/ui.py +++ b/src/calibre/gui2/viewer/ui.py @@ -148,7 +148,7 @@ class EbookViewer(MainWindow): self.bookmarks_widget = w = BookmarkManager(self) connect_lambda( w.create_requested, self, - lambda self: self.web_view.get_current_cfi(self.bookmarks_widget.create_new_bookmark)) + lambda self: self.web_view.trigger_shortcut('new_bookmark')) w.edited.connect(self.bookmarks_edited) w.activated.connect(self.bookmark_activated) w.toggle_requested.connect(self.toggle_bookmarks) @@ -167,7 +167,7 @@ class EbookViewer(MainWindow): self.web_view.search_result_not_found.connect(self.search_widget.search_result_not_found) self.web_view.toggle_bookmarks.connect(self.toggle_bookmarks) self.web_view.toggle_highlights.connect(self.toggle_highlights) - self.web_view.new_bookmark.connect(self.bookmarks_widget.create_requested) + self.web_view.new_bookmark.connect(self.bookmarks_widget.create_new_bookmark) self.web_view.toggle_inspector.connect(self.toggle_inspector) self.web_view.toggle_lookup.connect(self.toggle_lookup) self.web_view.quit.connect(self.quit) diff --git a/src/calibre/gui2/viewer/web_view.py b/src/calibre/gui2/viewer/web_view.py index 76cb7d8fb3..04abffc1cd 100644 --- a/src/calibre/gui2/viewer/web_view.py +++ b/src/calibre/gui2/viewer/web_view.py @@ -238,7 +238,7 @@ class ViewerBridge(Bridge): toggle_toc = from_js() toggle_bookmarks = from_js() toggle_highlights = from_js() - new_bookmark = from_js() + new_bookmark = from_js(object) toggle_inspector = from_js() toggle_lookup = from_js(object) show_search = from_js(object) @@ -436,7 +436,7 @@ class WebView(RestartingWebEngineView): find_next = pyqtSignal(object) toggle_bookmarks = pyqtSignal() toggle_highlights = pyqtSignal() - new_bookmark = pyqtSignal() + new_bookmark = pyqtSignal(object) toggle_inspector = pyqtSignal() toggle_lookup = pyqtSignal(object) quit = pyqtSignal() diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index e06ce71c79..e70b8b9d22 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -461,13 +461,25 @@ class IframeBoss: def get_current_cfi(self, data): cfi = current_cfi() csi = current_spine_item() + + def epubcfi(cfi): + return 'epubcfi(/{}{})'.format(2*(index+1), cfi) if cfi else None + if cfi and csi: index = csi.index if index > -1: - cfi = 'epubcfi(/{}{})'.format(2*(index+1), cfi) + selection = window.getSelection() + selcfi = seltext = None + if selection and not selection.isCollapsed: + seltext = selection.toString() + selcfi = cfi_for_selection() + selcfi.start = epubcfi(selcfi.start) + selcfi.end = epubcfi(selcfi.end) + cfi = epubcfi(cfi) self.send_message( 'report_cfi', cfi=cfi, progress_frac=self.calculate_progress_frac(), - file_progress_frac=progress_frac(), request_id=data.request_id) + file_progress_frac=progress_frac(), request_id=data.request_id, + selected_text=seltext, selection_bounds=selcfi) return self.send_message( 'report_cfi', cfi=None, progress_frac=0, file_progress_frac=0, request_id=data.request_id) diff --git a/src/pyj/read_book/selection_bar.pyj b/src/pyj/read_book/selection_bar.pyj index a2e11b31c1..90f8c7e774 100644 --- a/src/pyj/read_book/selection_bar.pyj +++ b/src/pyj/read_book/selection_bar.pyj @@ -169,6 +169,7 @@ def all_actions(): 'quick_highlight': a('highlight', _('Quick highlight in current style'), 'quick_highlight'), 'highlight': a('highlight', _('Highlight selection'), 'create_highlight'), 'search': a('search', _('Search for selection in the book'), 'book_search'), + 'bookmark': a('bookmark', _('Create a bookmark'), 'new_bookmark'), 'search_net': a('global-search', _('Search for selection on the net'), 'internet_search'), 'remove_highlight': a('trash', _('Remove this highlight'), 'remove_highlight', True), 'clear': a('close', _('Clear selection'), 'clear_selection'), @@ -490,6 +491,8 @@ class SelectionBar: self.send_message('trigger-shortcut', name=sc_name) elif sc_name is 'start_search': self.book_search() + elif sc_name is 'new_bookmark': + self.new_bookmark() elif sc_name is 'toggle_highlights': self.view.on_handle_shortcut({'name': sc_name}) # }}} @@ -749,6 +752,9 @@ class SelectionBar: self.view.show_search() self.clear_selection() + def new_bookmark(self): + self.view.new_bookmark() + def internet_search(self): text = self.view.currently_showing.selection.text if text: diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index 0b4547fd07..b133893c0c 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -451,7 +451,7 @@ class View: elif data.name is 'toggle_highlights': ui_operations.toggle_highlights() elif data.name is 'new_bookmark': - ui_operations.new_bookmark() + self.new_bookmark() elif data.name is 'toggle_inspector': ui_operations.toggle_inspector() elif data.name is 'toggle_lookup': @@ -544,6 +544,9 @@ class View: ui_operations.selection_changed(self.currently_showing.selection.text, self.currently_showing.selection.annot_id) self.selection_bar.update_position() + def new_bookmark(self): + self.get_current_cfi('new-bookmark', ui_operations.new_bookmark) + def update_selection_position(self, data): sel = self.currently_showing.selection sel.start = data.selection_extents.start @@ -1096,6 +1099,8 @@ class View: 'cfi': data.cfi, 'progress_frac': data.progress_frac, 'file_progress_frac': data.file_progress_frac, + 'selected_text': data.selected_text, + 'selection_bounds': data.selection_bounds, }) v'delete self.report_cfi_callbacks[data.request_id]' diff --git a/src/pyj/viewer-main.pyj b/src/pyj/viewer-main.pyj index e718fb06a9..82835aff25 100644 --- a/src/pyj/viewer-main.pyj +++ b/src/pyj/viewer-main.pyj @@ -323,8 +323,9 @@ if window is window.top: to_python.toggle_bookmarks() ui_operations.toggle_highlights = def(): to_python.toggle_highlights() - ui_operations.new_bookmark = def(): - to_python.new_bookmark() + ui_operations.new_bookmark = def(request_id, data): + if request_id is 'new-bookmark': + to_python.new_bookmark(data) ui_operations.toggle_inspector = def(): to_python.toggle_inspector() ui_operations.content_file_changed = def(name):