Clicking on an existing highlight should edit it

This commit is contained in:
Kovid Goyal 2020-04-13 16:36:58 +05:30
parent bd9109bbee
commit b40951e5dc
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 41 additions and 5 deletions

View File

@ -53,6 +53,16 @@ def unwrap_crw(crw):
unwrap(node) unwrap(node)
def select_crw(crw):
nodes = document.querySelectorAll(f'span[data-calibre-range-wrapper="{crw}"]')
r = document.createRange()
r.setStart(nodes[0].firstChild, 0)
r.setEnd(nodes[-1].lastChild, nodes[-1].lastChild.nodeValue.length)
sel = window.getSelection()
sel.removeAllRanges()
sel.addRange(r)
def create_wrapper_function(wrapper_elem, r, intersecting_wrappers): def create_wrapper_function(wrapper_elem, r, intersecting_wrappers):
start_node = r.startContainer start_node = r.startContainer
end_node = r.endContainer end_node = r.endContainer
@ -106,3 +116,18 @@ def wrap_text_in_range(style, r):
def reset_highlight_counter(): def reset_highlight_counter():
nonlocal wrapper_counter nonlocal wrapper_counter
wrapper_counter = 0 wrapper_counter = 0
def set_selection_to_highlight():
sel = window.getSelection()
if not sel or not sel.getRangeAt(0):
return
r = sel.getRangeAt(0)
crw = None
for node in text_nodes_in_range(r):
crw = node.parentNode.dataset.calibreRangeWrapper
if crw:
break
if crw:
select_crw(crw)
return crw or None

View File

@ -137,6 +137,7 @@ class CreateAnnotation:
def __init__(self, view): def __init__(self, view):
self.view = view self.view = view
self.editing_annot_uuid = None
self.annotations_manager = self.view.annotations_manager self.annotations_manager = self.view.annotations_manager
self.state = WAITING_FOR_CLICK self.state = WAITING_FOR_CLICK
self.left_line_height = self.right_line_height = 8 self.left_line_height = self.right_line_height = 8
@ -313,7 +314,7 @@ class CreateAnnotation:
for k in Object.keys(self.current_highlight_style): for k in Object.keys(self.current_highlight_style):
style += f'{k}: {s[k]}; ' style += f'{k}: {s[k]}; '
self.send_message( self.send_message(
'apply-highlight', style=style, uuid=short_uuid() 'apply-highlight', style=style, uuid=short_uuid(), existing=self.editing_annot_uuid
) )
self.hide() self.hide()
@ -428,6 +429,7 @@ class CreateAnnotation:
def handle_message(self, msg): def handle_message(self, msg):
if msg.type is 'create-annotation': if msg.type is 'create-annotation':
self.editing_annot_uuid = None
if not self.is_visible: if not self.is_visible:
self.view.hide_overlays() self.view.hide_overlays()
self.state = WAITING_FOR_CLICK self.state = WAITING_FOR_CLICK
@ -440,6 +442,7 @@ class CreateAnnotation:
elif msg.type is 'position-handles': elif msg.type is 'position-handles':
if self.state is WAITING_FOR_CLICK: if self.state is WAITING_FOR_CLICK:
self.place_handles(msg.extents) self.place_handles(msg.extents)
self.editing_annot_uuid = msg.existing or None
elif msg.type is 'update-handles': elif msg.type is 'update-handles':
self.place_handles(msg.extents) self.place_handles(msg.extents)
if msg.from_scroll and not msg.selection_extended: if msg.from_scroll and not msg.selection_extended:

View File

@ -11,7 +11,10 @@ from select import (
from fs_images import fix_fullscreen_svg_images from fs_images import fix_fullscreen_svg_images
from iframe_comm import IframeClient from iframe_comm import IframeClient
from range_utils import reset_highlight_counter, unwrap_crw, wrap_text_in_range from range_utils import (
reset_highlight_counter, set_selection_to_highlight, unwrap_crw,
wrap_text_in_range
)
from read_book.cfi import ( from read_book.cfi import (
cfi_for_selection, range_from_cfi, scroll_to as scroll_to_cfi cfi_for_selection, range_from_cfi, scroll_to as scroll_to_cfi
) )
@ -635,10 +638,12 @@ class IframeBoss:
if data.type is 'set-selection': if data.type is 'set-selection':
set_selections_extents_to(data.extents) set_selections_extents_to(data.extents)
elif data.type is 'position-handles-at-point': elif data.type is 'position-handles-at-point':
extents = selection_extents_at_point(data.x, data.y, in_flow_mode)
annot_id = set_selection_to_highlight()
if annot_id:
extents = selection_extents(in_flow_mode)
self.send_message( self.send_message(
'annotations', 'annotations', type='position-handles', extents=extents, existing=self.annot_id_uuid_map[annot_id])
type='position-handles',
extents=selection_extents_at_point(data.x, data.y, in_flow_mode))
elif data.type is 'scroll': elif data.type is 'scroll':
if self.scroll_to_extend_annotation(data.backwards): if self.scroll_to_extend_annotation(data.backwards):
page_rect = {'width': current_page_width()} page_rect = {'width': current_page_width()}
@ -667,6 +672,9 @@ class IframeBoss:
unwrap_crw(crw) unwrap_crw(crw)
removed_highlights[self.annot_id_uuid_map[crw]] = True removed_highlights[self.annot_id_uuid_map[crw]] = True
v'delete self.annot_id_uuid_map[crw]' v'delete self.annot_id_uuid_map[crw]'
if data.existing and removed_highlights[data.existing]:
data.uuid = data.existing
v'delete removed_highlights[data.existing]'
removed_highlights = Object.keys(removed_highlights) removed_highlights = Object.keys(removed_highlights)
if removed_highlights.length is 1: if removed_highlights.length is 1:
data.uuid = removed_highlights[0] data.uuid = removed_highlights[0]