Get highlight range as CFI

This commit is contained in:
Kovid Goyal 2020-03-24 14:30:30 +05:30
parent 5d9515c115
commit b887ca602a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 22 additions and 2 deletions

View File

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

View File

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