diff --git a/src/pyj/read_book/cfi.pyj b/src/pyj/read_book/cfi.pyj index d922f5f9dd..b50946d03e 100644 --- a/src/pyj/read_book/cfi.pyj +++ b/src/pyj/read_book/cfi.pyj @@ -670,6 +670,25 @@ def at_current(): # {{{ # }}} +def cfi_for_selection(r): # {{{ + if not r: + r = window.getSelection().getRangeAt(0) + + def pos(container, offset): + nt = container.nodeType + if nt is Node.TEXT_NODE or nt is Node.CDATA_SECTION_NODE or nt is Node.COMMENT_NODE: + return container, offset + if nt is Node.ELEMENT_NODE: + return container.childNodes[offset], 0 + return container, offset + + return { + 'start': encode(r.startContainer.ownerDocument, *pos(r.startContainer, r.startOffset)), + 'end': encode(r.endContainer.ownerDocument, *(r.endContainer, r.endOffset)), + } + +# }}} + if __name__ is '__main__': t = 'a^!,1' if unescape_from_cfi(escape_for_cfi(t)) is not t: diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index b1d8adae95..860fae7e13 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -12,7 +12,7 @@ from select import ( from fs_images import fix_fullscreen_svg_images from iframe_comm import IframeClient from range_utils import reset_highlight_counter, wrap_text_in_range -from read_book.cfi import scroll_to as scroll_to_cfi +from read_book.cfi import cfi_for_selection, scroll_to as scroll_to_cfi from read_book.extract import get_elements from read_book.find import reset_find_caches, select_search_result from read_book.flow_mode import ( @@ -642,11 +642,12 @@ class IframeBoss: elif data.type is 'set-highlight-style': set_selection_style(data.style) elif data.type is 'apply-highlight': + bounds = cfi_for_selection() annot_id = wrap_text_in_range(data.style) if annot_id is not None: window.getSelection().removeAllRanges() self.annot_id_uuid_map[annot_id] = data.uuid - self.send_message('annotations', type='highlight-applied', uuid=data.uuid, ok=annot_id is not None) + self.send_message('annotations', type='highlight-applied', uuid=data.uuid, ok=annot_id is not None, bounds=bounds) else: console.log('Ignoring annotations message to iframe with unknown type: ' + data.type)