Allow adding a create bookmark button to the selection bar

This commit is contained in:
Kovid Goyal 2020-08-15 10:12:44 +05:30
parent f228431fb9
commit 9eea78f092
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
7 changed files with 35 additions and 10 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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()

View File

@ -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)

View File

@ -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:

View File

@ -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]'

View File

@ -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):