From 7db1a9d0a4d01729eec65fc7c5e2ea9c488acf65 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 17 Aug 2020 21:29:45 +0530 Subject: [PATCH] Fix notes indicator not being updated when editing notes from the highlights panels --- src/pyj/range_utils.pyj | 6 ++++++ src/pyj/read_book/iframe.pyj | 14 ++++++++++++-- src/pyj/read_book/selection_bar.pyj | 4 ++++ src/pyj/read_book/view.pyj | 1 + 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/pyj/range_utils.pyj b/src/pyj/range_utils.pyj index 33f0c3cf2a..b0c5463bb4 100644 --- a/src/pyj/range_utils.pyj +++ b/src/pyj/range_utils.pyj @@ -152,6 +152,12 @@ def wrap_text_in_range(styler, r, class_to_add_to_last, process_wrapper): return crw, Object.keys(intersecting_wrappers) +def last_span_for_crw(crw): + nodes = document.querySelectorAll(f'span[data-calibre-range-wrapper="{crw}"]') + if nodes and nodes.length: + return nodes[-1] + + def reset_highlight_counter(): nonlocal wrapper_counter wrapper_counter = 0 diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index e70b8b9d22..c82789b08a 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -9,8 +9,8 @@ from select import move_end_of_selection, selection_extents, word_at_point from fs_images import fix_fullscreen_svg_images from iframe_comm import IframeClient from range_utils import ( - highlight_associated_with_selection, reset_highlight_counter, select_crw, - unwrap_all_crw, unwrap_crw, wrap_text_in_range + highlight_associated_with_selection, last_span_for_crw, reset_highlight_counter, + select_crw, unwrap_all_crw, unwrap_crw, wrap_text_in_range ) from read_book.cfi import cfi_for_selection, range_from_cfi from read_book.extract import get_elements @@ -752,6 +752,16 @@ class IframeBoss: window.setTimeout(def(): self.send_message('annotations', type='edit-highlight') , 50) + elif data.type is 'notes-edited': + cls = 'crw-has-dot' + crw_ = {v: k for k, v in Object.entries(annot_id_uuid_map)}[data.uuid] + if crw_: + node = last_span_for_crw(crw_) + if node: + if data.has_notes: + node.classList.add(cls) + else: + node.classList.remove(cls) elif data.type is 'remove-highlight': crw_ = {v: k for k, v in Object.entries(annot_id_uuid_map)}[data.uuid] if crw_: diff --git a/src/pyj/read_book/selection_bar.pyj b/src/pyj/read_book/selection_bar.pyj index af6c49fdbf..c9cf0b44be 100644 --- a/src/pyj/read_book/selection_bar.pyj +++ b/src/pyj/read_book/selection_bar.pyj @@ -828,6 +828,10 @@ class SelectionBar: def send_message(self, type, **kw): self.view.iframe_wrapper.send_message('annotations', type=type, **kw) + def notes_edited(self, annot_id): + notes = self.annotations_manager.notes_for_highlight(annot_id) + self.send_message('notes-edited', uuid=annot_id, has_notes=bool(notes)) + def handle_message(self, msg): if msg.type is 'highlight-applied': notes = self.current_notes diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index da858636e9..b865ce0fb8 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -1066,6 +1066,7 @@ class View: def set_notes_for_highlight(self, uuid, notes): if self.annotations_manager.set_notes_for_highlight(uuid, notes): + self.selection_bar.notes_edited(uuid) self.selection_bar.update_position() def on_next_spine_item(self, data):