| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
from elementmaker import E |
| 6 |
from gettext import gettext as _ |
| 7 |
from uuid import short_uuid |
| 8 |
|
| 9 |
from book_list.globals import get_session_data |
| 10 |
from book_list.theme import get_color |
| 11 |
from dom import change_icon_image, clear, svgicon, unique_id |
| 12 |
from modals import create_custom_dialog, error_dialog, question_dialog |
| 13 |
from read_book.globals import runtime, ui_operations |
| 14 |
from read_book.highlights import ( |
| 15 |
ICON_SIZE, EditNotesAndColors, HighlightStyle, all_styles, |
| 16 |
get_current_link_prefix, link_to_epubcfi, render_notes |
| 17 |
) |
| 18 |
from read_book.shortcuts import shortcut_for_key_event |
| 19 |
from read_book.toc import family_for_toc_node, get_toc_nodes_bordering_spine_item |
| 20 |
from utils import parse_url_params |
| 21 |
from widgets import create_button |
| 22 |
|
| 23 |
DRAG_SCROLL_ZONE_MIN_HEIGHT = 10 |
| 24 |
BUTTON_MARGIN = '0.5rem' |
| 25 |
|
| 26 |
# Utils {{{ |
| 27 |
|
| 28 |
|
| 29 |
def get_margins(): |
| 30 |
return { |
| 31 |
'top': document.getElementById('book-top-margin').offsetHeight, |
| 32 |
'left': document.getElementById('book-left-margin').offsetWidth, |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
def get_overlay_origin(): |
| 37 |
c = document.getElementById('book-selection-bar-overlay') |
| 38 |
if not c: |
| 39 |
return {'left': 0, 'top': 0} |
| 40 |
r = c.getBoundingClientRect() |
| 41 |
return {'left': r.left or 0, 'top': r.top or 0} |
| 42 |
|
| 43 |
|
| 44 |
def map_boundaries(cs, vertical, rtl): |
| 45 |
margins = get_margins() |
| 46 |
|
| 47 |
def map_boundary(b): |
| 48 |
x_offset = y_offset = 0 |
| 49 |
if vertical: |
| 50 |
if b.selected_prev: |
| 51 |
y_offset = b.height |
| 52 |
else: |
| 53 |
if rtl: |
| 54 |
# Horizontal RTL |
| 55 |
if not b.selected_prev: |
| 56 |
x_offset = b.width |
| 57 |
else: |
| 58 |
# Horizontal LTR |
| 59 |
if b.selected_prev: |
| 60 |
x_offset = b.width |
| 61 |
|
| 62 |
return { |
| 63 |
'x': (b.x or 0) + x_offset + margins.left, |
| 64 |
'y': (b.y or 0) + y_offset + margins.top, |
| 65 |
'height': b.height or 0, |
| 66 |
'width': b.width or 0, |
| 67 |
'onscreen': b.onscreen, |
| 68 |
} |
| 69 |
|
| 70 |
return map_boundary(cs.start), map_boundary(cs.end) |
| 71 |
|
| 72 |
|
| 73 |
def map_to_iframe_coords(point, margins): |
| 74 |
origin = get_overlay_origin() |
| 75 |
point.x -= (margins.left + origin.left) |
| 76 |
point.y -= (margins.top + origin.top) |
| 77 |
return point |
| 78 |
|
| 79 |
|
| 80 |
def near_element(elem, x, y): |
| 81 |
r = elem.getBoundingClientRect() |
| 82 |
extend_by = 15 |
| 83 |
left = r.left - extend_by |
| 84 |
top = r.top - extend_by |
| 85 |
right = r.right + extend_by |
| 86 |
bottom = r.bottom + extend_by |
| 87 |
return left <= x <= right and top <= y <= bottom |
| 88 |
|
| 89 |
|
| 90 |
def position_bar_avoiding_handles(lh, rh, left, top, bar_width, bar_height, available_width, available_height, buffer): |
| 91 |
# adjust position to minimize overlap with handles |
| 92 |
|
| 93 |
def bar_rect(left, top): |
| 94 |
return {'left': left, 'top': top, 'right': left + bar_width, 'bottom': top + bar_height} |
| 95 |
|
| 96 |
def overlaps_a_handle(left, top): |
| 97 |
b = bar_rect(left, top) |
| 98 |
if elements_overlap(lh, b): |
| 99 |
return lh |
| 100 |
if elements_overlap(rh, b): |
| 101 |
return rh |
| 102 |
|
| 103 |
if not overlaps_a_handle(left, top): |
| 104 |
return |
| 105 |
|
| 106 |
if Math.abs(lh.top - rh.top) < lh.height + buffer: |
| 107 |
# handles close vertically, place either above or below |
| 108 |
bottom = max(lh.bottom, rh.bottom) |
| 109 |
has_space_below = bottom + bar_height < available_height - buffer |
| 110 |
if has_space_below: |
| 111 |
return {'top': bottom, 'put_below': True} |
| 112 |
return {'top': min(lh.top, rh.top), 'put_below': False} |
| 113 |
|
| 114 |
b = bar_rect(left, top) |
| 115 |
if lh.left > rh.left: |
| 116 |
lh, rh = rh, lh |
| 117 |
left_overlaps = elements_overlap(lh, b) |
| 118 |
right_overlaps = elements_overlap(rh, b) |
| 119 |
if not left_overlaps or not right_overlaps: |
| 120 |
# overlapping a single handle, see if we can move horizontally |
| 121 |
h = lh if left_overlaps else rh |
| 122 |
d1 = d2 = 2 * available_width |
| 123 |
q1 = h.left - bar_width - 1 |
| 124 |
if q1 > -buffer and not overlaps_a_handle(q1, top): |
| 125 |
d1 = abs(left - q1) |
| 126 |
q2 = h.right + 1 |
| 127 |
if q2 + bar_width < available_width + buffer and not overlaps_a_handle(q2, top): |
| 128 |
d2 = abs(left - q2) |
| 129 |
d = min(d1, d2) |
| 130 |
if d < available_width: |
| 131 |
return {'left': q1 if d is d1 else q2} |
| 132 |
|
| 133 |
# try to place either to left of both handles, between both handles, to |
| 134 |
# the right of both |
| 135 |
d1 = d2 = d3 = 2 * available_width |
| 136 |
q1 = lh.left - bar_width - 1 |
| 137 |
if q1 > -buffer and not overlaps_a_handle(q1, top): |
| 138 |
d1 = abs(left - q1) |
| 139 |
q2 = lh.right + 1 |
| 140 |
if q2 + bar_width < rh.left + buffer and not overlaps_a_handle(q2, top): |
| 141 |
d2 = abs(left - q2) |
| 142 |
q3 = rh.right + 1 |
| 143 |
if q3 + bar_width < available_width + buffer and not overlaps_a_handle(q3, top): |
| 144 |
d3 = abs(left - q3) |
| 145 |
d = min(d1, d2, d3) |
| 146 |
if d < available_width: |
| 147 |
return {'left': q1 if d is d1 else (q2 if d is d2 else q3)} |
| 148 |
|
| 149 |
# look above both vertically, between both and below both |
| 150 |
th, bh = [lh, rh] if lh.top <= rh.top else [rh, lh] |
| 151 |
d1 = d2 = d3 = 2 * available_height |
| 152 |
q1 = th.top - bar_height - 1 |
| 153 |
if q1 > -buffer and not overlaps_a_handle(left, q1): |
| 154 |
d1 = abs(top - q1) |
| 155 |
q2 = th.bottom + 1 |
| 156 |
if q2 + bar_height < bh.top + buffer and not overlaps_a_handle(left, q2): |
| 157 |
d2 = abs(top - q2) |
| 158 |
q3 = bh.bottom + 1 |
| 159 |
if q3 + bar_height < available_height + buffer and not overlaps_a_handle(left, q3): |
| 160 |
d3 = abs(top - q3) |
| 161 |
d = min(d1, d2, d3) |
| 162 |
if d < available_height: |
| 163 |
return {'top': (q1 + bar_height) if d is d1 else (q2 if d is d2 else q3), 'put_below': d is not d1} |
| 164 |
|
| 165 |
# look in the four corners |
| 166 |
if not overlaps_a_handle(buffer, buffer): |
| 167 |
return {'left': buffer, 'top': buffer + bar_height, 'put_below': False} |
| 168 |
if not overlaps_a_handle(available_width - bar_width, buffer): |
| 169 |
return {'left': available_width - bar_width, 'top': buffer + bar_height, 'put_below': False} |
| 170 |
if not overlaps_a_handle(buffer, available_height - bar_height): |
| 171 |
return {'left': buffer, 'top': available_height - bar_height, 'put_below': True} |
| 172 |
if not overlaps_a_handle(available_width - bar_width, available_height - bar_height): |
| 173 |
return {'left': available_width - bar_width, 'top': available_height - bar_height, 'put_below': True} |
| 174 |
|
| 175 |
# give up should be relatively rare |
| 176 |
|
| 177 |
|
| 178 |
def quick_highlight_icon(name, tooltip, hcolor): |
| 179 |
svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg') |
| 180 |
svg.setAttribute('style', f'fill: currentColor; height: 2ex; width: 2ex; vertical-align: text-top; margin: 0') |
| 181 |
u = document.createElementNS('http://www.w3.org/2000/svg', 'use') |
| 182 |
svg.appendChild(u) |
| 183 |
svg.firstChild.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', '#icon-' + name) |
| 184 |
ans = E.div( |
| 185 |
style=f'width: {ICON_SIZE}; height: {ICON_SIZE}; display: flex; flex-direction: column', |
| 186 |
title=tooltip or '', |
| 187 |
svg, |
| 188 |
E.div(style=f'width: {ICON_SIZE}; height: 1ex; background-color: {hcolor}; color: {hcolor}; margin: 0') |
| 189 |
) |
| 190 |
return ans |
| 191 |
|
| 192 |
|
| 193 |
def all_actions(): |
| 194 |
def a(icon, text, func, needs_highlight): |
| 195 |
return { |
| 196 |
'icon': icon, 'text': text, 'function_name': func, 'needs_highlight': v'!!needs_highlight', |
| 197 |
'icon_function': def (hcolor): |
| 198 |
return svgicon(icon, ICON_SIZE, ICON_SIZE, text) |
| 199 |
} |
| 200 |
|
| 201 |
if not all_actions.ans: |
| 202 |
all_actions.ans = { |
| 203 |
'copy': a('copy', _('Copy to clipboard') + ' [Ctrl+C]', 'copy_to_clipboard'), |
| 204 |
'lookup': a('library', _('Lookup/search selected word') + ' [L]', 'lookup'), |
| 205 |
'quick_highlight': a('highlight', _('Quick highlight in current style') + ' [Q]', 'quick_highlight'), |
| 206 |
'highlight': a('highlight', _('Highlight selection') + ' [H]', 'create_highlight'), |
| 207 |
'search': a('search', _('Search for selection in the book') + ' [F]', 'book_search'), |
| 208 |
'bookmark': a('bookmark', _('Create a bookmark') + ' [Ctrl+Alt+B]', 'new_bookmark'), |
| 209 |
'search_net': a('global-search', _('Search for selection on the net') + ' [S]', 'internet_search'), |
| 210 |
'remove_highlight': a('trash', _('Remove this highlight') + _(' [Delete or D]'), 'remove_highlight', True), |
| 211 |
'clear': a('close', _('Clear selection') + ' [Esc]', 'clear_selection'), |
| 212 |
'speak': a('bullhorn', _('Read aloud') + ' [T]', 'speak_aloud'), |
| 213 |
'cite': a('reference-mode', _('Copy a citation to this text') + ' [X]', 'cite'), |
| 214 |
} |
| 215 |
qh = all_actions.ans.quick_highlight |
| 216 |
qh.icon_function = quick_highlight_icon.bind(None, qh.icon, qh.text) |
| 217 |
return all_actions.ans |
| 218 |
|
| 219 |
|
| 220 |
def selection_handle(): |
| 221 |
ans = svgicon('selection-handle') |
| 222 |
s = ans.style |
| 223 |
s.position = 'absolute' |
| 224 |
s.boxSizing = 'border-box' |
| 225 |
s.touchAction = 'none' |
| 226 |
return ans |
| 227 |
|
| 228 |
|
| 229 |
def set_handle_color(handle, bg, fg): |
| 230 |
use = handle.querySelector('use') |
| 231 |
use.style.stroke = fg |
| 232 |
use.style.fill = bg |
| 233 |
|
| 234 |
|
| 235 |
def elements_overlap(a, b): |
| 236 |
return a.left < b.right and b.left < a.right and a.top < b.bottom and b.top < a.bottom |
| 237 |
|
| 238 |
|
| 239 |
HIDDEN = 0 |
| 240 |
WAITING = 1 |
| 241 |
DRAGGING = 2 |
| 242 |
EDITING = 3 |
| 243 |
# }}} |
| 244 |
|
| 245 |
|
| 246 |
class SelectionBar: |
| 247 |
def __init__(self, view): |
| 248 |
self.view = view |
| 249 |
self.current_highlight_style = HighlightStyle(get_session_data().get('highlight_style')) |
| 250 |
self.current_notes = '' |
| 251 |
self.state = HIDDEN |
| 252 |
self.start_handle_id = unique_id('handle') |
| 253 |
self.end_handle_id = unique_id('handle') |
| 254 |
self.bar_id = unique_id('bar') |
| 255 |
self.editor_id = unique_id('editor') |
| 256 |
self.quick_highlight_styles = [] |
| 257 |
# Sensible defaults until we get information from a selection message. |
| 258 |
self.ltr = True |
| 259 |
self.rtl = False |
| 260 |
self.vertical = False |
| 261 |
container = self.container |
| 262 |
container.style.overflow = 'hidden' |
| 263 |
container.addEventListener('click', self.container_clicked, {'passive': False}) |
| 264 |
container.addEventListener('contextmenu', self.container_context_menu_requested, {'passive': False}) |
| 265 |
container.addEventListener('mouseup', self.mouseup_on_container, {'passive': False}) |
| 266 |
container.addEventListener('mousemove', self.mousemove_on_container, {'passive': False}) |
| 267 |
container.addEventListener('touchmove', self.touchmove_on_container, {'passive': False}) |
| 268 |
container.addEventListener('touchend', self.touchend_on_container, {'passive': False}) |
| 269 |
container.addEventListener('touchcancel', self.touchend_on_container, {'passive': False}) |
| 270 |
container.addEventListener('keydown', self.on_keydown, {'passive': False}) |
| 271 |
container.addEventListener('wheel', self.on_wheel, {'passive': False}) |
| 272 |
container.setAttribute('tabindex', '0') |
| 273 |
|
| 274 |
self.dragging_handle = None |
| 275 |
self.position_in_handle = {'x': 0, 'y': 0} |
| 276 |
self.active_touch = None |
| 277 |
self.drag_scroll_timer = None |
| 278 |
self.last_drag_scroll_at = None |
| 279 |
self.start_line_length = self.end_line_length = 0 |
| 280 |
self.current_editor = None |
| 281 |
|
| 282 |
start_handle = selection_handle() |
| 283 |
start_handle.id = self.start_handle_id |
| 284 |
end_handle = selection_handle() |
| 285 |
end_handle.id = self.end_handle_id |
| 286 |
for h in (start_handle, end_handle): |
| 287 |
h.addEventListener('mousedown', self.mousedown_on_handle, {'passive': False}) |
| 288 |
h.addEventListener('touchstart', self.touchstart_on_handle, {'passive': False}) |
| 289 |
container.appendChild(h) |
| 290 |
container.appendChild(E.div( |
| 291 |
id=self.bar_id, |
| 292 |
style='position: absolute; border: solid 1px currentColor; border-radius: 5px;' |
| 293 |
'left: 0; top: 0; display: flex; flex-direction: column;' |
| 294 |
)) |
| 295 |
|
| 296 |
editor_div = E.div(id=self.editor_id, style='position: absolute') |
| 297 |
container.appendChild(editor_div) |
| 298 |
editor_div.addEventListener('click', self.editor_container_clicked, {'passive': False}) |
| 299 |
editor_div.addEventListener('wheel', def (ev): ev.stopPropagation();, {'passive': True}) |
| 300 |
|
| 301 |
# bar and handles markup {{{ |
| 302 |
|
| 303 |
def set_handle_colors(self): |
| 304 |
handle_fill = get_color('window-background') |
| 305 |
fg = self.view.current_color_scheme.foreground |
| 306 |
for h in (self.start_handle, self.end_handle): |
| 307 |
set_handle_color(h, handle_fill, fg) |
| 308 |
|
| 309 |
def build_bar(self, annot_id): |
| 310 |
notes = self.annotations_manager.notes_for_highlight(annot_id) |
| 311 |
bar_container = self.bar |
| 312 |
clear(bar_container) |
| 313 |
bar_container.style.maxWidth = 'min(50rem, 90vw)' |
| 314 |
bar_container.style.backgroundColor = get_color('window-background') |
| 315 |
notes_container = E.div() |
| 316 |
notes_container.addEventListener('wheel', def (evt): evt.stopPropagation();, {'passive': False}) |
| 317 |
for x in [ |
| 318 |
E.div(style='height: 4ex; display: flex; align-items: center; padding: 5px; justify-content: center'), |
| 319 |
E.hr(style='border-top: solid 1px; margin: 0; padding: 0; display: none'), |
| 320 |
E.div( |
| 321 |
style='display: none; padding: 5px;', |
| 322 |
notes_container, |
| 323 |
) |
| 324 |
]: |
| 325 |
bar_container.appendChild(x) |
| 326 |
bar = bar_container.firstChild |
| 327 |
hs = self.current_highlight_style.highlight_shade(self.view.current_color_scheme.is_dark_theme) |
| 328 |
|
| 329 |
def cb(ac, callback): |
| 330 |
ans = ac.icon_function(hs) |
| 331 |
ans.addEventListener('click', def (ev): |
| 332 |
ev.stopPropagation(), ev.preventDefault() |
| 333 |
callback(ev) |
| 334 |
self.view.focus_iframe() |
| 335 |
) |
| 336 |
ans.classList.add('simple-link') |
| 337 |
ans.style.marginLeft = ans.style.marginRight = BUTTON_MARGIN |
| 338 |
return ans |
| 339 |
|
| 340 |
actions = all_actions() |
| 341 |
sd = get_session_data() |
| 342 |
for acname in sd.get('selection_bar_actions'): |
| 343 |
ac = actions[acname] |
| 344 |
if ac and (not ac.needs_highlight or v'!!annot_id'): |
| 345 |
bar.appendChild(cb(ac, self[ac.function_name])) |
| 346 |
selection_bar_quick_highlights = sd.get('selection_bar_quick_highlights') |
| 347 |
self.quick_highlight_styles = [] |
| 348 |
if selection_bar_quick_highlights?.length: |
| 349 |
self.show_quick_highlight_buttons(bar, selection_bar_quick_highlights) |
| 350 |
self.show_notes(bar_container, notes) |
| 351 |
return bar_container |
| 352 |
|
| 353 |
def show_quick_highlight_buttons(self, bar, actions): |
| 354 |
all = {x.key: x for x in all_styles()} |
| 355 |
actions = [a for a in actions if all[a]] |
| 356 |
if not actions.length: |
| 357 |
return |
| 358 |
actions.pysort(key=def (a): return (all[a].friendly_name or '').toLowerCase();) |
| 359 |
self.quick_highlight_styles = actions |
| 360 |
bar.appendChild(E.div( |
| 361 |
style=f'background: currentColor; width: 1px; height: {ICON_SIZE}; margin-left: {BUTTON_MARGIN}; margin-right: {BUTTON_MARGIN}' |
| 362 |
)) |
| 363 |
dark = self.view.current_color_scheme.is_dark_theme |
| 364 |
for i, key in enumerate(actions): |
| 365 |
hs = all[key] |
| 366 |
if i < 9: |
| 367 |
sc = i + 1 |
| 368 |
elif i == 10: |
| 369 |
sc = 0 |
| 370 |
else: |
| 371 |
sc = None |
| 372 |
sw = E.div( |
| 373 |
class_='simple-link', style=f'margin-left: {BUTTON_MARGIN}; margin-right: {BUTTON_MARGIN}', |
| 374 |
title=_('Highlight using: {name}{sc}').format(name=hs.friendly_name, sc=f' [{sc}]' if sc is not None else ''), |
| 375 |
onclick=self.quick_highlight_with_style.bind(None, hs), |
| 376 |
) |
| 377 |
hs.make_swatch(sw, dark) |
| 378 |
bar.appendChild(sw) |
| 379 |
|
| 380 |
def show_notes(self, bar, notes): |
| 381 |
notes = (notes or '').strip() |
| 382 |
if not notes: |
| 383 |
return |
| 384 |
notes_container = bar.lastChild |
| 385 |
c = notes_container.lastChild |
| 386 |
notes_container.style.display = notes_container.previousSibling.style.display = 'block' |
| 387 |
c.style.overflow = 'auto' |
| 388 |
c.style.maxHeight = 'min(20ex, 40vh)' |
| 389 |
render_notes(notes, c, True) |
| 390 |
# }}} |
| 391 |
|
| 392 |
# accessors {{{ |
| 393 |
@property |
| 394 |
def container(self): |
| 395 |
return document.getElementById('book-selection-bar-overlay') |
| 396 |
|
| 397 |
@property |
| 398 |
def bar(self): |
| 399 |
return document.getElementById(self.bar_id) |
| 400 |
|
| 401 |
@property |
| 402 |
def start_handle(self): |
| 403 |
return document.getElementById(self.start_handle_id) |
| 404 |
|
| 405 |
@property |
| 406 |
def end_handle(self): |
| 407 |
return document.getElementById(self.end_handle_id) |
| 408 |
|
| 409 |
@property |
| 410 |
def editor(self): |
| 411 |
return document.getElementById(self.editor_id) |
| 412 |
|
| 413 |
@property |
| 414 |
def annotations_manager(self): |
| 415 |
return self.view.annotations_manager |
| 416 |
|
| 417 |
@property |
| 418 |
def current_handle_position(self): |
| 419 |
sh, eh = self.start_handle, self.end_handle |
| 420 |
sbr, ebr = sh.getBoundingClientRect(), eh.getBoundingClientRect() |
| 421 |
|
| 422 |
if not self.vertical: |
| 423 |
# Horizontal LTR (i.e. English) |
| 424 |
if not self.rtl: |
| 425 |
return { |
| 426 |
'start': { |
| 427 |
'onscreen': sh.style.display is not 'none', |
| 428 |
'x': Math.round(sbr.right), 'y': Math.round(sbr.bottom - self.start_line_length // 2) |
| 429 |
}, |
| 430 |
'end': { |
| 431 |
'onscreen': eh.style.display is not 'none', |
| 432 |
'x': Math.round(ebr.left), 'y': Math.round(ebr.bottom - self.end_line_length // 2) |
| 433 |
} |
| 434 |
} |
| 435 |
# Horizontal RTL (i.e. Hebrew, Arabic) |
| 436 |
else: |
| 437 |
return { |
| 438 |
'start': { |
| 439 |
'onscreen': sh.style.display is not 'none', |
| 440 |
'x': Math.round(sbr.left), 'y': Math.round(sbr.bottom - self.start_line_length // 2) |
| 441 |
}, |
| 442 |
'end': { |
| 443 |
'onscreen': eh.style.display is not 'none', |
| 444 |
'x': Math.round(ebr.right), 'y': Math.round(ebr.bottom - self.end_line_length // 2) |
| 445 |
} |
| 446 |
} |
| 447 |
# Vertical RTL (i.e. Traditional Chinese, Japanese) |
| 448 |
else if self.rtl: |
| 449 |
return { |
| 450 |
'start': { |
| 451 |
'onscreen': sh.style.display is not 'none', |
| 452 |
'x': Math.round(sbr.left + self.start_line_length // 2), 'y': Math.round(sbr.bottom) |
| 453 |
}, |
| 454 |
'end': { |
| 455 |
'onscreen': eh.style.display is not 'none', |
| 456 |
'x': Math.round(ebr.right - self.end_line_length // 2), 'y': Math.round(ebr.top) |
| 457 |
} |
| 458 |
} |
| 459 |
# Vertical LTR (i.e. Mongolian) |
| 460 |
else: |
| 461 |
return { |
| 462 |
'start': { |
| 463 |
'onscreen': sh.style.display is not 'none', |
| 464 |
'x': Math.round(sbr.right - self.end_line_length // 2), 'y': Math.round(sbr.bottom) |
| 465 |
}, |
| 466 |
'end': { |
| 467 |
'onscreen': eh.style.display is not 'none', |
| 468 |
'x': Math.round(ebr.left + self.start_line_length // 2), 'y': Math.round(ebr.top) |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
# }}} |
| 473 |
|
| 474 |
# event handlers {{{ |
| 475 |
def mousedown_on_handle(self, ev): |
| 476 |
ev.stopPropagation(), ev.preventDefault() |
| 477 |
if self.state is WAITING: |
| 478 |
self.start_handle_drag(ev, ev.currentTarget) |
| 479 |
|
| 480 |
def touchstart_on_handle(self, ev): |
| 481 |
ev.stopPropagation(), ev.preventDefault() |
| 482 |
if self.state is WAITING: |
| 483 |
for touch in ev.changedTouches: |
| 484 |
self.active_touch = touch.identifier |
| 485 |
self.start_handle_drag(touch, ev.currentTarget) |
| 486 |
break |
| 487 |
|
| 488 |
def start_handle_drag(self, ev, handle): |
| 489 |
self.state = DRAGGING |
| 490 |
self.dragging_handle = handle.id |
| 491 |
r = handle.getBoundingClientRect() |
| 492 |
self.position_in_handle.x = Math.round(ev.clientX - r.left) |
| 493 |
self.position_in_handle.y = Math.round(ev.clientY - r.top) |
| 494 |
|
| 495 |
def container_context_menu_requested(self, ev): |
| 496 |
pos = {'x': ev.clientX, 'y': ev.clientY} |
| 497 |
map_to_iframe_coords(pos, get_margins()) |
| 498 |
self.send_message('extend-to-point', pos=pos) |
| 499 |
|
| 500 |
def container_clicked(self, ev): |
| 501 |
ev.stopPropagation(), ev.preventDefault() |
| 502 |
if self.state is EDITING: |
| 503 |
self.hide_editor(True) |
| 504 |
if self.state is WAITING: |
| 505 |
now = window.performance.now() |
| 506 |
if ev.shiftKey: |
| 507 |
pos = {'x': ev.clientX, 'y': ev.clientY} |
| 508 |
map_to_iframe_coords(pos, get_margins()) |
| 509 |
self.send_message('extend-to-point', pos=pos) |
| 510 |
return |
| 511 |
if self.last_double_click_at and now - self.last_double_click_at < 500: |
| 512 |
self.send_message('extend-to-paragraph') |
| 513 |
return |
| 514 |
for x in (self.bar, self.start_handle, self.end_handle): |
| 515 |
if near_element(x, ev.clientX, ev.clientY): |
| 516 |
return |
| 517 |
self.clear_selection() |
| 518 |
|
| 519 |
def mousemove_on_container(self, ev): |
| 520 |
if self.state is not DRAGGING: |
| 521 |
return |
| 522 |
ev.stopPropagation(), ev.preventDefault() |
| 523 |
self.move_handle(ev) |
| 524 |
|
| 525 |
def touchmove_on_container(self, ev): |
| 526 |
if self.state is not DRAGGING: |
| 527 |
return |
| 528 |
ev.stopPropagation(), ev.preventDefault() |
| 529 |
for touch in ev.changedTouches: |
| 530 |
if touch.identifier is self.active_touch: |
| 531 |
self.move_handle(touch) |
| 532 |
return |
| 533 |
|
| 534 |
def move_handle(self, ev): |
| 535 |
handle = document.getElementById(self.dragging_handle) |
| 536 |
s = handle.style |
| 537 |
rect = self.container.getBoundingClientRect() |
| 538 |
s.left = (ev.clientX - rect.left - self.position_in_handle.x) + 'px' |
| 539 |
s.top = (ev.clientY - rect.top - self.position_in_handle.y) + 'px' |
| 540 |
margins = get_margins() |
| 541 |
pos = self.current_handle_position |
| 542 |
if self.dragging_handle is self.start_handle_id: |
| 543 |
start = True |
| 544 |
position = map_to_iframe_coords(pos.start, margins) |
| 545 |
else: |
| 546 |
start = False |
| 547 |
position = map_to_iframe_coords(pos.end, margins) |
| 548 |
self.send_message('move-end-of-selection', start=start, pos=position) |
| 549 |
c = self.container |
| 550 |
rect = c.getBoundingClientRect() |
| 551 |
t = document.getElementById('book-top-margin').offsetHeight |
| 552 |
top = rect.top + max(t, DRAG_SCROLL_ZONE_MIN_HEIGHT) |
| 553 |
t = document.getElementById('book-bottom-margin').offsetHeight |
| 554 |
bottom = rect.bottom - max(t, DRAG_SCROLL_ZONE_MIN_HEIGHT) |
| 555 |
if ev.clientY < top or ev.clientY > bottom: |
| 556 |
self.run_drag_scroll(ev.clientY, top, bottom) |
| 557 |
else: |
| 558 |
self.end_drag_scroll() |
| 559 |
|
| 560 |
def end_handle_drag(self): |
| 561 |
self.end_drag_scroll() |
| 562 |
handle = self.dragging_handle |
| 563 |
self.dragging_handle = None |
| 564 |
self.state = WAITING |
| 565 |
# this is done after the event loop ticks because otherwise if |
| 566 |
# update_position moves the handle a click event is delivered to the |
| 567 |
# container, calling container_clicked() and clearing the selection |
| 568 |
window.setTimeout(self.update_position.bind(None, handle), 0) |
| 569 |
|
| 570 |
def mouseup_on_container(self, ev): |
| 571 |
if self.state is DRAGGING: |
| 572 |
ev.preventDefault(), ev.stopPropagation() |
| 573 |
self.end_handle_drag() |
| 574 |
|
| 575 |
def touchend_on_container(self, ev): |
| 576 |
if self.state is DRAGGING: |
| 577 |
ev.preventDefault(), ev.stopPropagation() |
| 578 |
for touch in ev.changedTouches: |
| 579 |
if touch.identifier is self.active_touch: |
| 580 |
self.active_touch = None |
| 581 |
self.end_handle_drag() |
| 582 |
return |
| 583 |
|
| 584 |
def on_wheel(self, ev): |
| 585 |
ev.stopPropagation(), ev.preventDefault() |
| 586 |
if self.state is not EDITING: |
| 587 |
self.view.send_wheel_event_to_iframe(ev, 'selection-mode') |
| 588 |
|
| 589 |
def on_keydown(self, ev): |
| 590 |
ev.stopPropagation(), ev.preventDefault() |
| 591 |
if ev.key is 'Escape': |
| 592 |
if self.state is EDITING: |
| 593 |
self.hide_editor() |
| 594 |
else: |
| 595 |
self.clear_selection() |
| 596 |
return |
| 597 |
if self.state is WAITING and not ev.ctrlKey and not ev.altKey and not ev.metaKey: |
| 598 |
k = ev.key.toLowerCase() |
| 599 |
if k is 'l': |
| 600 |
self.lookup() |
| 601 |
return |
| 602 |
if k is 'q': |
| 603 |
self.quick_highlight() |
| 604 |
return |
| 605 |
if k is 'h': |
| 606 |
self.create_highlight() |
| 607 |
return |
| 608 |
if k is 'f': |
| 609 |
self.book_search() |
| 610 |
return |
| 611 |
if k is 's': |
| 612 |
self.internet_search() |
| 613 |
return |
| 614 |
if k is 't': |
| 615 |
self.speak_aloud() |
| 616 |
return |
| 617 |
if k is 'x': |
| 618 |
self.cite() |
| 619 |
return |
| 620 |
if k is 'delete' or k is 'd': |
| 621 |
self.remove_highlight() |
| 622 |
return |
| 623 |
if k in '0123456789': |
| 624 |
all = {x.key: x for x in all_styles()} |
| 625 |
k = int(k) |
| 626 |
if k is 0: |
| 627 |
k = 9 |
| 628 |
else: |
| 629 |
k -= 1 |
| 630 |
key = self.quick_highlight_styles[k] |
| 631 |
if key: |
| 632 |
hs = all[key] |
| 633 |
if hs: |
| 634 |
self.quick_highlight_with_style(hs) |
| 635 |
return |
| 636 |
sc_name = shortcut_for_key_event(ev, self.view.keyboard_shortcut_map) |
| 637 |
if not sc_name: |
| 638 |
return |
| 639 |
forwarded = { |
| 640 |
'toggle_highlights': True, |
| 641 |
'edit_book': True, |
| 642 |
'select_all': True, |
| 643 |
'extend_selection_to_end_of_line': True, |
| 644 |
'extend_selection_to_start_of_line': True, |
| 645 |
} |
| 646 |
if sc_name is 'show_chrome': |
| 647 |
self.clear_selection() |
| 648 |
elif sc_name is 'copy_to_clipboard': |
| 649 |
self.copy_to_clipboard() |
| 650 |
elif sc_name is 'toggle_lookup': |
| 651 |
self.lookup() |
| 652 |
elif sc_name in ('up', 'down', 'pageup', 'pagedown', 'left', 'right'): |
| 653 |
self.send_message('trigger-shortcut', name=sc_name) |
| 654 |
elif sc_name is 'start_search': |
| 655 |
self.book_search() |
| 656 |
elif sc_name is 'new_bookmark': |
| 657 |
self.new_bookmark() |
| 658 |
elif sc_name.startsWith('shrink_selection_by_') or sc_name.startsWith('extend_selection_by_') or forwarded[sc_name]: |
| 659 |
self.view.on_handle_shortcut({'name': sc_name}) |
| 660 |
|
| 661 |
def report_failed_edit_highlight(self, annot_id): |
| 662 |
notes = self.annotations_manager.notes_for_highlight(annot_id) |
| 663 |
has_notes = bool(notes) |
| 664 |
title = _('Highlight text missing') |
| 665 |
text = _( |
| 666 |
'The text associated with this highlight could not be found in the book.' |
| 667 |
' This can happen if the book was modified. This highlight will be automatically removed.' |
| 668 |
) |
| 669 |
if runtime.is_standalone_viewer or not has_notes: |
| 670 |
if has_notes: |
| 671 |
ui_operations.copy_selection(notes) |
| 672 |
text += ' ' + _('The notes for this highlight have been copied to the clipboard.') |
| 673 |
error_dialog(title, text) |
| 674 |
else: |
| 675 |
create_custom_dialog(title, def (parent, close_modal): |
| 676 |
parent.appendChild(E.div( |
| 677 |
E.div(text), |
| 678 |
E.div( |
| 679 |
class_='button-box', |
| 680 |
create_button(_('OK'), None, close_modal), |
| 681 |
create_button( |
| 682 |
_('Copy notes to clipboard'), None, def (): |
| 683 |
ui_operations.copy_selection(notes) |
| 684 |
close_modal() |
| 685 |
, highlight = True), |
| 686 |
) |
| 687 |
)) |
| 688 |
) |
| 689 |
self.remove_highlight_with_id(annot_id) |
| 690 |
# }}} |
| 691 |
|
| 692 |
# drag scroll {{{ |
| 693 |
def run_drag_scroll(self, mouse_y, top, bottom): |
| 694 |
backwards = mouse_y <= top |
| 695 |
self.do_one_drag_scroll(backwards, top - mouse_y if backwards else mouse_y - bottom) |
| 696 |
|
| 697 |
def do_one_drag_scroll(self, backwards, distance_from_boundary): |
| 698 |
window.clearTimeout(self.drag_scroll_timer) |
| 699 |
self.drag_scroll_timer = None |
| 700 |
if self.state is not DRAGGING: |
| 701 |
return |
| 702 |
sd = get_session_data() |
| 703 |
in_flow_mode = sd.get('read_mode') is 'flow' |
| 704 |
interval = 1000 / sd.get('lines_per_sec_smooth') if in_flow_mode else 1200 |
| 705 |
self.drag_scroll_timer = window.setTimeout(self.do_one_drag_scroll.bind(None, backwards, distance_from_boundary), interval) |
| 706 |
now = window.performance.now() |
| 707 |
if self.last_drag_scroll_at is None: |
| 708 |
# don't jump a page immediately in paged mode |
| 709 |
if in_flow_mode: |
| 710 |
self.send_drag_scroll_message(backwards, 'left' if self.dragging_handle is self.start_handle_id else 'right', True) |
| 711 |
self.last_drag_scroll_at = now |
| 712 |
elif now - self.last_drag_scroll_at > interval: |
| 713 |
self.send_drag_scroll_message(backwards, 'left' if self.dragging_handle is self.start_handle_id else 'right', True) |
| 714 |
self.last_drag_scroll_at = now |
| 715 |
|
| 716 |
def send_drag_scroll_message(self, backwards, handle, extend_selection): |
| 717 |
self.send_message( |
| 718 |
'drag-scroll', backwards=backwards, handle=handle, extents=self.current_handle_position, |
| 719 |
extend_selection=extend_selection) |
| 720 |
|
| 721 |
def end_drag_scroll(self): |
| 722 |
if self.drag_scroll_timer is not None: |
| 723 |
window.clearTimeout(self.drag_scroll_timer) |
| 724 |
self.drag_scroll_timer = None |
| 725 |
self.last_drag_scroll_at = None |
| 726 |
|
| 727 |
# }}} |
| 728 |
|
| 729 |
# show and hide {{{ |
| 730 |
def hide(self): |
| 731 |
was_shown = self.state is not HIDDEN |
| 732 |
self.state = HIDDEN |
| 733 |
self.container.style.display = 'none' |
| 734 |
self.view.focus_iframe() |
| 735 |
if not was_shown and self.spoken_aloud_at_least_once: |
| 736 |
ui_operations.speak_simple_text('') # cancel any on-going speech |
| 737 |
self.spoken_aloud_at_least_once = False |
| 738 |
|
| 739 |
def show(self): |
| 740 |
sd = get_session_data() |
| 741 |
if self.state is HIDDEN: |
| 742 |
if sd.get('show_selection_bar'): |
| 743 |
self.container.style.display = 'block' |
| 744 |
self.state = WAITING |
| 745 |
self.focus() |
| 746 |
|
| 747 |
@property |
| 748 |
def is_visible(self): |
| 749 |
return self.container.style.display is not 'none' |
| 750 |
|
| 751 |
def focus(self): |
| 752 |
self.container.focus() |
| 753 |
|
| 754 |
def update_position(self, dragged_handle): |
| 755 |
container = self.container |
| 756 |
cs = self.view.currently_showing.selection |
| 757 |
self.bar.style.display = 'none' |
| 758 |
self.set_handle_colors() |
| 759 |
if self.state is DRAGGING: |
| 760 |
self.show() |
| 761 |
# needed for drag scrolling |
| 762 |
self.position_undragged_handle() |
| 763 |
return |
| 764 |
if self.state is EDITING: |
| 765 |
self.start_handle.style.display = 'none' |
| 766 |
self.end_handle.style.display = 'none' |
| 767 |
self.show() |
| 768 |
self.place_editor() |
| 769 |
return |
| 770 |
self.start_handle.style.display = 'none' |
| 771 |
self.end_handle.style.display = 'none' |
| 772 |
self.editor.style.display = 'none' |
| 773 |
|
| 774 |
if not cs or cs.empty or jstype(cs.drag_mouse_position?.x) is 'number' or cs.selection_change_caused_by_search: |
| 775 |
return self.hide() |
| 776 |
|
| 777 |
if not cs.start.onscreen and not cs.end.onscreen: |
| 778 |
return self.hide() |
| 779 |
|
| 780 |
self.rtl = cs.rtl |
| 781 |
self.ltr = not self.rtl |
| 782 |
self.vertical = cs.vertical |
| 783 |
for h in (self.start_handle, self.end_handle): |
| 784 |
if h.vertical is not self.vertical: |
| 785 |
h.vertical = self.vertical |
| 786 |
change_icon_image(h, 'selection-handle-vertical' if h.vertical else 'selection-handle') |
| 787 |
|
| 788 |
self.show() |
| 789 |
self.bar.style.display = self.start_handle.style.display = self.end_handle.style.display = 'block' |
| 790 |
start, end = map_boundaries(cs, self.vertical, self.rtl) |
| 791 |
bar = self.build_bar(cs.annot_id) |
| 792 |
bar_height = bar.offsetHeight |
| 793 |
bar_width = bar.offsetWidth |
| 794 |
buffer = 2 |
| 795 |
limits = { |
| 796 |
'top': buffer, 'bottom': container.offsetHeight - bar_height - buffer, |
| 797 |
# - 10 ensures we don't cover scroll bar |
| 798 |
'left': buffer, 'right': container.offsetWidth - bar_width - buffer - 10 |
| 799 |
} |
| 800 |
start_handle, end_handle = self.start_handle, self.end_handle |
| 801 |
self.position_handles(start_handle, end_handle, start, end) |
| 802 |
|
| 803 |
def place_vertically(pos, put_below): |
| 804 |
if put_below: |
| 805 |
top = pos |
| 806 |
bar.style.flexDirection = 'column' |
| 807 |
else: |
| 808 |
top = pos - bar_height - buffer |
| 809 |
bar.style.flexDirection = 'column-reverse' |
| 810 |
bar.style.top = top + 'px' |
| 811 |
return top |
| 812 |
|
| 813 |
# We try to place the bar near the last dragged handle so it shows up |
| 814 |
# close to current mouse position. We assume it is the "end" handle. |
| 815 |
if dragged_handle: |
| 816 |
if dragged_handle is not self.end_handle_id: |
| 817 |
start, end = end, start |
| 818 |
else: |
| 819 |
if not cs.start_is_anchor: |
| 820 |
start, end = end, start |
| 821 |
if not end.onscreen and start.onscreen: |
| 822 |
start, end = end, start |
| 823 |
|
| 824 |
end_after_start = start.y < end.y or (start.y is end.y and start.x < end.x) |
| 825 |
# vertical position |
| 826 |
if end_after_start: |
| 827 |
has_space_below = end.y + end.height < container.offsetHeight - bar_height - buffer |
| 828 |
put_below = has_space_below |
| 829 |
else: |
| 830 |
has_space_above = end.y - bar_height - buffer > 0 |
| 831 |
put_below = not has_space_above |
| 832 |
top = place_vertically(end.y + end.height if put_below else end.y, put_below) |
| 833 |
|
| 834 |
# horizontal position |
| 835 |
left = end.x - bar_width // 2 |
| 836 |
left = max(limits.left, min(left, limits.right)) |
| 837 |
bar.style.left = left + 'px' |
| 838 |
sh, eh = start_handle.getBoundingClientRect(), end_handle.getBoundingClientRect() |
| 839 |
changed = position_bar_avoiding_handles(sh, eh, left, top, bar_width, bar_height, container.offsetWidth - 10, container.offsetHeight, buffer) |
| 840 |
if changed: |
| 841 |
if changed.top?: |
| 842 |
place_vertically(changed.top, changed.put_below) |
| 843 |
if changed.left?: |
| 844 |
bar.style.left = changed.left + 'px' |
| 845 |
|
| 846 |
def place_single_handle(self, selection_size, handle, boundary, is_start): |
| 847 |
s = handle.style |
| 848 |
s.display = 'block' if boundary.onscreen else 'none' |
| 849 |
|
| 850 |
# Cap this to prevent very large handles when selecting images. |
| 851 |
selection_size = max(10, min(selection_size, 60)) |
| 852 |
|
| 853 |
if self.vertical: |
| 854 |
width = selection_size * 2 |
| 855 |
height = int(width * 2 / 3) |
| 856 |
else: |
| 857 |
height = selection_size * 2 |
| 858 |
width = int(height * 2 / 3) |
| 859 |
|
| 860 |
s.width = f'{width}px' |
| 861 |
s.height = f'{height}px' |
| 862 |
s.transform = 'none' |
| 863 |
if not self.vertical: |
| 864 |
bottom = boundary.y + boundary.height |
| 865 |
top = bottom - height |
| 866 |
s.top = f'{top}px' |
| 867 |
if is_start: |
| 868 |
# Horizontal, start, LTR |
| 869 |
if self.ltr: |
| 870 |
s.left = (boundary.x - width) + 'px' |
| 871 |
self.start_line_length = selection_size |
| 872 |
# Horizontal, start, RTL |
| 873 |
else: |
| 874 |
s.left = (boundary.x) + 'px' |
| 875 |
self.start_line_length = selection_size |
| 876 |
s.transform = 'scaleX(-1)' |
| 877 |
else: |
| 878 |
# Horizontal, end, LTR |
| 879 |
if self.ltr: |
| 880 |
s.left = boundary.x + 'px' |
| 881 |
self.end_line_length = selection_size |
| 882 |
s.transform = 'scaleX(-1)' |
| 883 |
# Horizontal, end, RTL |
| 884 |
else: |
| 885 |
s.left = (boundary.x - width) + 'px' |
| 886 |
self.end_line_length = selection_size |
| 887 |
else: |
| 888 |
if is_start: |
| 889 |
# Vertical, start, RTL |
| 890 |
if self.rtl: |
| 891 |
s.top = boundary.y - height + 'px' |
| 892 |
s.left = boundary.x + 'px' |
| 893 |
self.start_line_length = selection_size |
| 894 |
s.transform = 'scaleX(-1) scaleY(-1)' |
| 895 |
# Vertical, start, LTR |
| 896 |
else: |
| 897 |
s.top = boundary.y - height + 'px' |
| 898 |
s.left = boundary.x - width + boundary.width + 'px' |
| 899 |
self.start_line_length = selection_size |
| 900 |
s.transform = 'scaleY(-1)' |
| 901 |
else: |
| 902 |
# Vertical, end, RTL |
| 903 |
if self.rtl: |
| 904 |
s.top = boundary.y + 'px' |
| 905 |
s.left = boundary.x - width + boundary.width + 'px' |
| 906 |
self.end_line_length = selection_size |
| 907 |
# Vertical, end, LTR |
| 908 |
else: |
| 909 |
s.top = boundary.y + 'px' |
| 910 |
s.left = boundary.x + 'px' |
| 911 |
self.end_line_length = selection_size |
| 912 |
s.transform = 'scaleX(-1)' |
| 913 |
|
| 914 |
def position_handles(self, start_handle, end_handle, start, end): |
| 915 |
if self.vertical: |
| 916 |
selection_size = max(start.width, end.width) |
| 917 |
else: |
| 918 |
selection_size = max(start.height, end.height) |
| 919 |
self.place_single_handle(selection_size, start_handle, start, True) |
| 920 |
self.place_single_handle(selection_size, end_handle, end, False) |
| 921 |
|
| 922 |
def position_undragged_handle(self): |
| 923 |
cs = self.view.currently_showing.selection |
| 924 |
start, end = map_boundaries(cs, self.vertical, self.rtl) |
| 925 |
if not self.vertical: |
| 926 |
selection_size = max(start.height, end.height) |
| 927 |
else: |
| 928 |
selection_size = max(start.width, end.width) |
| 929 |
if self.dragging_handle is self.start_handle_id: |
| 930 |
handle = self.end_handle |
| 931 |
boundary = end |
| 932 |
is_start = False |
| 933 |
else: |
| 934 |
handle = self.start_handle |
| 935 |
boundary = start |
| 936 |
is_start = True |
| 937 |
self.place_single_handle(selection_size, handle, boundary, is_start) |
| 938 |
# }}} |
| 939 |
|
| 940 |
# Editor {{{ |
| 941 |
def show_editor(self, highlight_style, notes): |
| 942 |
for x in (self.bar, self.start_handle, self.end_handle): |
| 943 |
x.style.display = 'none' |
| 944 |
container = self.editor |
| 945 |
clear(container) |
| 946 |
container.style.display = 'block' |
| 947 |
self.state = EDITING |
| 948 |
cs = self.view.currently_showing.selection |
| 949 |
self.current_editor = EditNotesAndColors( |
| 950 |
container, self.view.current_color_scheme.is_dark_theme, notes, highlight_style, cs.annot_id, self.hide_editor) |
| 951 |
self.place_editor() |
| 952 |
|
| 953 |
def place_editor(self): |
| 954 |
if self.current_editor is None: |
| 955 |
return |
| 956 |
ed = self.editor |
| 957 |
cs = self.view.currently_showing.selection |
| 958 |
start, end = map_boundaries(cs, self.vertical, self.rtl) |
| 959 |
if not start.onscreen and not end.onscreen: |
| 960 |
return |
| 961 |
width, height = ed.offsetWidth, ed.offsetHeight |
| 962 |
if not start.onscreen: |
| 963 |
start = end |
| 964 |
if not end.onscreen: |
| 965 |
end = start |
| 966 |
top, bottom = [start, end] if start.y <= end.y else [end, start] |
| 967 |
container = self.container |
| 968 |
space_above, space_below = top.y, container.offsetHeight - (bottom.y + bottom.height) |
| 969 |
if height <= min(space_above, space_below): |
| 970 |
put_above = space_above < space_below |
| 971 |
else: |
| 972 |
put_above = space_above > space_below |
| 973 |
if put_above: |
| 974 |
y = max(0, top.y - height) |
| 975 |
else: |
| 976 |
y = bottom.y + bottom.height |
| 977 |
if y + height > container.offsetHeight: |
| 978 |
y = container.offsetHeight - height |
| 979 |
x = max(0, end.x - width // 2) |
| 980 |
if x + width > container.offsetWidth: |
| 981 |
x = container.offsetWidth - width |
| 982 |
ed.style.left = x + 'px' |
| 983 |
ed.style.top = y + 'px' |
| 984 |
|
| 985 |
def hide_editor(self, apply_changes): |
| 986 |
if not apply_changes: |
| 987 |
self.state = WAITING |
| 988 |
self.update_position() |
| 989 |
self.focus() |
| 990 |
return |
| 991 |
ed = self.current_editor |
| 992 |
self.current_editor = None |
| 993 |
self.current_highlight_style = ed.current_style |
| 994 |
self.current_notes = ed.current_notes |
| 995 |
self.send_message( |
| 996 |
'apply-highlight', style=self.current_highlight_style.style, uuid=short_uuid(), existing=ed.annot_id, |
| 997 |
has_notes=v'!!self.current_notes', notes=self.current_notes |
| 998 |
) |
| 999 |
self.state = WAITING |
| 1000 |
self.update_position() |
| 1001 |
get_session_data().set('highlight_style', self.current_highlight_style.style) |
| 1002 |
self.focus() |
| 1003 |
|
| 1004 |
def add_notes_or_create_highlight(self, notes, style): |
| 1005 |
if self.state is EDITING: |
| 1006 |
self.hide_editor() |
| 1007 |
annot_id = self.view.currently_showing.selection.annot_id |
| 1008 |
if annot_id: |
| 1009 |
am = self.annotations_manager |
| 1010 |
existing_notes = am.notes_for_highlight(annot_id) |
| 1011 |
if existing_notes: |
| 1012 |
notes = existing_notes + '\n\n------------------------------------\n\n' + notes |
| 1013 |
q = am.style_for_highlight(annot_id) |
| 1014 |
if q: |
| 1015 |
style = q |
| 1016 |
self.current_notes = notes |
| 1017 |
self.send_message( |
| 1018 |
'apply-highlight', style=style, uuid=short_uuid(), has_notes=v'!!self.current_notes', existing=annot_id, |
| 1019 |
notes=self.current_notes, |
| 1020 |
) |
| 1021 |
self.state = WAITING |
| 1022 |
self.update_position() |
| 1023 |
self.focus() |
| 1024 |
|
| 1025 |
def editor_container_clicked(self, ev): |
| 1026 |
ev.stopPropagation(), ev.preventDefault() |
| 1027 |
# }}} |
| 1028 |
|
| 1029 |
# Actions {{{ |
| 1030 |
def copy_to_clipboard(self): |
| 1031 |
self.view.copy_to_clipboard() |
| 1032 |
|
| 1033 |
def lookup(self): |
| 1034 |
if ui_operations.toggle_lookup: |
| 1035 |
ui_operations.toggle_lookup(True) |
| 1036 |
else: |
| 1037 |
self.view.overlay.show_word_actions(self.view.currently_showing.selection.text) |
| 1038 |
|
| 1039 |
def book_search(self): |
| 1040 |
cs = self.view.currently_showing?.selection?.text |
| 1041 |
if not cs or len(str.strip(cs)) < 2: |
| 1042 |
return error_dialog(_('Too little text'), _('Cannot search as too little text is selected. You must select at least two characters.')) |
| 1043 |
self.view.show_search(True) |
| 1044 |
self.clear_selection() |
| 1045 |
|
| 1046 |
def new_bookmark(self): |
| 1047 |
self.view.new_bookmark() |
| 1048 |
|
| 1049 |
def internet_search(self): |
| 1050 |
text = self.view.currently_showing.selection.text |
| 1051 |
if text: |
| 1052 |
q = encodeURIComponent(text) |
| 1053 |
url = get_session_data().get('net_search_url').format(q=q) |
| 1054 |
ui_operations.open_url(url) |
| 1055 |
|
| 1056 |
def clear_selection(self): |
| 1057 |
self.view.on_handle_shortcut({'name': 'clear_selection'}) |
| 1058 |
self.hide() |
| 1059 |
|
| 1060 |
def speak_aloud(self): |
| 1061 |
text = self.view.currently_showing.selection.text |
| 1062 |
if text: |
| 1063 |
ui_operations.speak_simple_text(text) |
| 1064 |
self.spoken_aloud_at_least_once = True |
| 1065 |
|
| 1066 |
def cite(self): |
| 1067 |
self.send_message('cite-current-selection') |
| 1068 |
|
| 1069 |
def create_highlight(self): |
| 1070 |
cs = self.view.currently_showing.selection |
| 1071 |
hs = self.current_highlight_style |
| 1072 |
notes = '' |
| 1073 |
if cs.annot_id: |
| 1074 |
am = self.annotations_manager |
| 1075 |
q = am.style_for_highlight(cs.annot_id) |
| 1076 |
if q: |
| 1077 |
hs = HighlightStyle(q) |
| 1078 |
notes = am.notes_for_highlight(cs.annot_id) or notes |
| 1079 |
self.show() |
| 1080 |
self.show_editor(hs, notes) |
| 1081 |
|
| 1082 |
def quick_highlight(self): |
| 1083 |
cs = self.view.currently_showing.selection |
| 1084 |
if cs.text: |
| 1085 |
if cs.annot_id: |
| 1086 |
am = self.annotations_manager |
| 1087 |
self.current_notes = am.notes_for_highlight(cs.annot_id) or '' |
| 1088 |
self.send_message( |
| 1089 |
'apply-highlight', |
| 1090 |
style=self.current_highlight_style.style, |
| 1091 |
uuid=short_uuid(), |
| 1092 |
existing=cs.annot_id, |
| 1093 |
has_notes=v'!!self.current_notes', |
| 1094 |
notes=self.current_notes |
| 1095 |
) |
| 1096 |
else: |
| 1097 |
self.current_notes = '' |
| 1098 |
self.send_message('apply-highlight', style=self.current_highlight_style.style, uuid=short_uuid(), has_notes=False, notes='') |
| 1099 |
self.state = WAITING |
| 1100 |
self.update_position() |
| 1101 |
|
| 1102 |
def quick_highlight_with_style(self, hs): |
| 1103 |
self.current_highlight_style = hs |
| 1104 |
self.quick_highlight() |
| 1105 |
|
| 1106 |
def remove_highlight(self): |
| 1107 |
annot_id = self.view.currently_showing.selection.annot_id |
| 1108 |
if annot_id: |
| 1109 |
q = _('Are you sure you want to delete this highlight permanently?') |
| 1110 |
text = self.annotations_manager.text_for_highlight(annot_id) |
| 1111 |
if text and text.length: |
| 1112 |
if text.length > 20: |
| 1113 |
text = text[:20] + '…' |
| 1114 |
notes = self.annotations_manager.notes_for_highlight(annot_id) |
| 1115 |
if notes and notes.length: |
| 1116 |
q = _('Are you sure you want to delete the highlighting of "{}" and the associated notes permanently?').format(text) |
| 1117 |
else: |
| 1118 |
q = _('Are you sure you want to delete the highlighting of "{}" permanently?').format(text) |
| 1119 |
question_dialog( |
| 1120 |
_('Are you sure?'), q, |
| 1121 |
def (yes): |
| 1122 |
if yes: |
| 1123 |
self.remove_highlight_with_id(annot_id) |
| 1124 |
, skip_dialog_name = 'confirm_remove_highlight' |
| 1125 |
) |
| 1126 |
|
| 1127 |
def remove_highlight_with_id(self, annot_id): |
| 1128 |
self.send_message('remove-highlight', uuid=annot_id) |
| 1129 |
self.annotations_manager.delete_highlight(annot_id) |
| 1130 |
|
| 1131 |
def edit_highlight(self, annot_id): |
| 1132 |
self.send_message('edit-highlight', uuid=annot_id) |
| 1133 |
# }}} |
| 1134 |
|
| 1135 |
# Interact with iframe {{{ |
| 1136 |
|
| 1137 |
def send_message(self, type, **kw): |
| 1138 |
self.view.iframe_wrapper.send_message('annotations', type=type, **kw) |
| 1139 |
|
| 1140 |
def notes_edited(self, annot_id): |
| 1141 |
notes = self.annotations_manager.notes_for_highlight(annot_id) |
| 1142 |
self.send_message('notes-edited', uuid=annot_id, has_notes=bool(notes), notes=notes) |
| 1143 |
|
| 1144 |
def handle_message(self, msg): |
| 1145 |
if msg.type is 'highlight-applied': |
| 1146 |
notes = self.current_notes |
| 1147 |
self.current_notes = '' |
| 1148 |
if not msg.ok: |
| 1149 |
return error_dialog( |
| 1150 |
_('Highlighting failed'), |
| 1151 |
_('Failed to apply highlighting, try adjusting extent of highlight') |
| 1152 |
) |
| 1153 |
toc_family = [] |
| 1154 |
if msg.anchor_before: |
| 1155 |
toc_family = family_for_toc_node(msg.anchor_before.id) |
| 1156 |
else: |
| 1157 |
before = get_toc_nodes_bordering_spine_item()[0] |
| 1158 |
if before: |
| 1159 |
toc_family = family_for_toc_node(before.id) |
| 1160 |
self.annotations_manager.add_highlight(msg, msg.style, notes, toc_family) |
| 1161 |
elif msg.type is 'highlight-overlapped': |
| 1162 |
question_dialog( |
| 1163 |
_('Are you sure?'), _('This highlight overlaps existing highlights. Creating it will cause notes' |
| 1164 |
' in the existing highlights to be lost. Create it anyway?'), |
| 1165 |
def (yes): |
| 1166 |
if yes: |
| 1167 |
self.send_message( |
| 1168 |
'apply-highlight-overwrite', style=msg.style, uuid=msg.uuid, existing=msg.existing, has_notes=msg.has_notes, notes=msg.notes) |
| 1169 |
else: |
| 1170 |
if self.current_notes: |
| 1171 |
self.show_editor(self.current_highlight_style, self.current_notes) |
| 1172 |
, |
| 1173 |
) |
| 1174 |
elif msg.type is 'edit-highlight': |
| 1175 |
if self.state is WAITING: |
| 1176 |
self.create_highlight() |
| 1177 |
elif msg.type is 'edit-highlight-failed': |
| 1178 |
self.report_failed_edit_highlight(msg.uuid) |
| 1179 |
elif msg.type is 'double-click': |
| 1180 |
self.last_double_click_at = window.performance.now() |
| 1181 |
elif msg.type is 'cite-data': |
| 1182 |
annot_id = self.view.currently_showing.selection.annot_id |
| 1183 |
# If selected text is highlighted |
| 1184 |
if annot_id: |
| 1185 |
data = self.annotations_manager.data_for_highlight(annot_id) |
| 1186 |
spine_index = self.view.currently_showing.spine_index |
| 1187 |
cfi = self.annotations_manager.cfi_for_highlight(annot_id, spine_index) |
| 1188 |
|
| 1189 |
if runtime.is_standalone_viewer: |
| 1190 |
url = get_current_link_prefix() + '?open_at=' + cfi |
| 1191 |
else: |
| 1192 |
url = get_current_link_prefix() + f"bookpos={cfi}" |
| 1193 |
for key, value in Object.entries(parse_url_params()): |
| 1194 |
if key != 'bookpos': |
| 1195 |
url = url + f"&{key}={value}" |
| 1196 |
info = {'text': data.highlighted_text, |
| 1197 |
'url': url, |
| 1198 |
'timestamp': data.timestamp, |
| 1199 |
'style_type': data.style.type, |
| 1200 |
'style_kind': data.style.kind, |
| 1201 |
'style_which': data.style.which, |
| 1202 |
'chapter': data.toc_family_titles[0] if data.toc_family_titles[0] else '', |
| 1203 |
'notes': data.notes if 'notes' in data else '', |
| 1204 |
} |
| 1205 |
copy_info = get_session_data().get('cite_hl_template').format(**info) |
| 1206 |
# If selected text isn't highlighted |
| 1207 |
else: |
| 1208 |
spine_index = self.view.currently_showing.spine_index |
| 1209 |
spine_index = (1 + spine_index) * 2 |
| 1210 |
cfi = msg.bounds.start |
| 1211 |
link_prefix = get_current_link_prefix() |
| 1212 |
if not link_prefix: |
| 1213 |
return self.view.show_not_a_library_book_error() |
| 1214 |
text = msg.highlighted_text.replace(/\[/g, r'\[').replace(/\]/g, r'\]') |
| 1215 |
url = link_to_epubcfi(f'epubcfi(/{spine_index}{cfi})', link_prefix) |
| 1216 |
info = {'text': text, |
| 1217 |
'url': url, |
| 1218 |
} |
| 1219 |
copy_info = get_session_data().get('cite_text_template').format(**info) |
| 1220 |
|
| 1221 |
ui_operations.copy_selection(copy_info) |
| 1222 |
else: |
| 1223 |
print('Ignoring annotations message with unknown type:', msg.type) |
| 1224 |
|
| 1225 |
# }}} |