From cf03b4e45b869b7d5d19bbe6a09fd8a155de8051 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 1 Sep 2022 08:30:53 +0530 Subject: [PATCH] Content server: Use the chapter title as the base bookmark name when creating new bookmarks. Fixes #1986786 [No automatic name for bookmark in web viewer](https://bugs.launchpad.net/calibre/+bug/1986786) --- src/pyj/read_book/annotations.pyj | 4 ++-- src/pyj/read_book/bookmarks.pyj | 4 +++- src/pyj/read_book/toc.pyj | 24 ++++++++++++++++++------ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/pyj/read_book/annotations.pyj b/src/pyj/read_book/annotations.pyj index dcfea8a490..55197ecc3a 100644 --- a/src/pyj/read_book/annotations.pyj +++ b/src/pyj/read_book/annotations.pyj @@ -161,9 +161,9 @@ class AnnotationsManager: # {{{ self.sync_annots_to_server('bookmarks') return changed - def default_bookmark_title(self): + def default_bookmark_title(self, base_default_title): all_titles = {bm.title:True for bm in self.bookmarks if not bm.removed} - base_default_title = _('Bookmark') + base_default_title = base_default_title or _('Bookmark') c = 0 while True: c += 1 diff --git a/src/pyj/read_book/bookmarks.pyj b/src/pyj/read_book/bookmarks.pyj index ef93c6c352..e887cfe98e 100644 --- a/src/pyj/read_book/bookmarks.pyj +++ b/src/pyj/read_book/bookmarks.pyj @@ -9,6 +9,7 @@ from book_list.item_list import build_list, create_item, create_side_action from dom import ensure_id, set_css from modals import question_dialog from widgets import create_button +from read_book.toc import get_book_mark_title def goto_cfi(cfi, view): @@ -49,7 +50,8 @@ def create_bookmarks_list(annotations_manager, onclick): def create_new_bookmark(annotations_manager, data): - title = window.prompt(_('Enter title for bookmark:'), data.selected_text or annotations_manager.default_bookmark_title()) + base_default_title = get_book_mark_title() or _('Bookmark') + title = window.prompt(_('Enter title for bookmark:'), data.selected_text or annotations_manager.default_bookmark_title(base_default_title)) if not title: return False cfi = data.cfi diff --git a/src/pyj/read_book/toc.pyj b/src/pyj/read_book/toc.pyj index a92dbccf75..4ea1b7aee0 100644 --- a/src/pyj/read_book/toc.pyj +++ b/src/pyj/read_book/toc.pyj @@ -108,7 +108,7 @@ def get_current_toc_nodes(): return r -def get_highlighted_toc_nodes(toc, parent_map, id_map): +def get_highlighted_toc_nodes(toc, parent_map, id_map, skip_parents): data = update_visible_toc_nodes.data ans = {} if data.has_visible: @@ -120,11 +120,12 @@ def get_highlighted_toc_nodes(toc, parent_map, id_map): before = get_border_nodes(toc, id_map)[0] if before: ans[before.id] = True - for node_id in Object.keys(ans): - p = parent_map[node_id] - while p and p.title: - ans[p.id] = True - p = parent_map[p.id] + if not skip_parents: + for node_id in Object.keys(ans): + p = parent_map[node_id] + while p and p.title: + ans[p.id] = True + p = parent_map[p.id] return ans def get_toc_maps(toc): @@ -141,6 +142,17 @@ def get_toc_maps(toc): process_node(toc) return parent_map, id_map +def get_book_mark_title(): + toc = current_book().manifest.toc + parent_map, id_map = get_toc_maps(toc) + highlighted_toc_nodes = get_highlighted_toc_nodes(toc, parent_map, id_map, True) + for node_id in Object.keys(highlighted_toc_nodes): + node = id_map[node_id] + if node.title: + return node.title + return '' + + def create_toc_tree(toc, onclick): parent_map, id_map = get_toc_maps(toc) highlighted_toc_nodes = get_highlighted_toc_nodes(toc, parent_map, id_map)