| 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 |
|
| 8 |
from book_list.theme import get_color |
| 9 |
from dom import clear |
| 10 |
from read_book.shortcuts import shortcut_for_key_event |
| 11 |
|
| 12 |
|
| 13 |
class Hints: |
| 14 |
def __init__(self, view): |
| 15 |
self.view = view |
| 16 |
container = self.container |
| 17 |
container.setAttribute('tabindex', '0') |
| 18 |
container.style.overflow = 'hidden' |
| 19 |
container.addEventListener('keydown', self.on_keydown, {'passive': False}) |
| 20 |
container.addEventListener('click', self.container_clicked, {'passive': False}) |
| 21 |
self.reset() |
| 22 |
|
| 23 |
def reset(self): |
| 24 |
self.hints_map = {} |
| 25 |
self.current_prefix = '' |
| 26 |
|
| 27 |
@property |
| 28 |
def container(self): |
| 29 |
return document.getElementById('book-hints-overlay') |
| 30 |
|
| 31 |
@property |
| 32 |
def is_visible(self): |
| 33 |
return self.container.style.display is not 'none' |
| 34 |
|
| 35 |
def focus(self): |
| 36 |
self.container.focus() |
| 37 |
|
| 38 |
def hide(self): |
| 39 |
if self.is_visible: |
| 40 |
self.container.style.display = 'none' |
| 41 |
self.send_message('hide') |
| 42 |
self.reset() |
| 43 |
self.view.focus_iframe() |
| 44 |
|
| 45 |
def show(self): |
| 46 |
if not self.is_visible: |
| 47 |
self.reset() |
| 48 |
c = self.container |
| 49 |
c.style.display = 'block' |
| 50 |
clear(c) |
| 51 |
self.focus() |
| 52 |
self.send_message('show') |
| 53 |
|
| 54 |
def on_keydown(self, ev): |
| 55 |
ev.preventDefault(), ev.stopPropagation() |
| 56 |
if ev.key is 'Escape': |
| 57 |
self.hide() |
| 58 |
return |
| 59 |
if ev.key is 'Enter': |
| 60 |
if self.current_prefix: |
| 61 |
self.apply_prefix(True) |
| 62 |
return |
| 63 |
if ev.key is 'Backspace': |
| 64 |
if self.current_prefix: |
| 65 |
self.current_prefix = self.current_prefix[:-1] |
| 66 |
self.apply_prefix() |
| 67 |
return |
| 68 |
hint_keys = list('1234567890abcdefghijklmnopqrstuvwxyz') |
| 69 |
q = ev.key.toLowerCase() |
| 70 |
if hint_keys.indexOf(q) > -1: |
| 71 |
self.current_prefix += q |
| 72 |
self.apply_prefix() |
| 73 |
|
| 74 |
sc_name = shortcut_for_key_event(ev, self.view.keyboard_shortcut_map) |
| 75 |
if not sc_name: |
| 76 |
return |
| 77 |
|
| 78 |
def container_clicked(self, ev): |
| 79 |
ev.stopPropagation(), ev.preventDefault() |
| 80 |
self.hide() |
| 81 |
|
| 82 |
def apply_prefix(self, accept_full_match): |
| 83 |
matches = [] |
| 84 |
if self.current_prefix: |
| 85 |
for k in Object.keys(self.hints_map): |
| 86 |
if k is '_length': |
| 87 |
continue |
| 88 |
q = encode(int(k)) |
| 89 |
if accept_full_match: |
| 90 |
if q is self.current_prefix: |
| 91 |
matches.push(k) |
| 92 |
break |
| 93 |
elif q.startswith(self.current_prefix): |
| 94 |
matches.push(k) |
| 95 |
if matches.length is 1: |
| 96 |
self.send_message('activate', hint=self.hints_map[matches[0]]) |
| 97 |
self.hide() |
| 98 |
else: |
| 99 |
self.send_message('apply_prefix', prefix=self.current_prefix) |
| 100 |
|
| 101 |
def send_message(self, type, **kw): |
| 102 |
self.view.iframe_wrapper.send_message('hints', type=type, **kw) |
| 103 |
|
| 104 |
def handle_message(self, msg): |
| 105 |
if msg.type is 'shown': |
| 106 |
self.reset() |
| 107 |
self.hints_map = msg.hints_map |
| 108 |
if not self.hints_map._length: |
| 109 |
self.no_hints_found() |
| 110 |
|
| 111 |
def no_hints_found(self): |
| 112 |
c = self.container |
| 113 |
c.appendChild(E.div( |
| 114 |
_('No links found. Press Esc to close'), |
| 115 |
style=f'position: absolute; margin: auto; top: 50%; left: 50%; background: {get_color("window-background")};' + |
| 116 |
' padding: 1rem; border: solid 1px currentColor; border-radius: 4px; transform: translate(-50%, -50%);' |
| 117 |
)) |
| 118 |
|
| 119 |
|
| 120 |
def is_visible(a): |
| 121 |
if not a.offsetParent: |
| 122 |
return False |
| 123 |
rect = a.getBoundingClientRect() |
| 124 |
return rect.left >= 0 and rect.top >= 0 and rect.left < window.innerWidth and rect.top < window.innerHeight |
| 125 |
|
| 126 |
|
| 127 |
def encode(i): |
| 128 |
return i.toString(36).toLowerCase() |
| 129 |
|
| 130 |
|
| 131 |
def hint_visible_links(): |
| 132 |
i = 0 |
| 133 |
hint_map = {} |
| 134 |
for a in document.body.querySelectorAll('a[href]'): |
| 135 |
if is_visible(a): |
| 136 |
i += 1 |
| 137 |
h = i + '' |
| 138 |
a.dataset.calibreHintRender = encode(i) |
| 139 |
a.dataset.calibreHintValue = h |
| 140 |
a.classList.add('calibre-hint-visible') |
| 141 |
hint_map[h] = {'type': 'link', 'value': i} |
| 142 |
hint_map._length = i |
| 143 |
return hint_map |
| 144 |
|
| 145 |
|
| 146 |
def unhint_links(): |
| 147 |
for a in document.body.querySelectorAll('a[href]'): |
| 148 |
a.classList.remove('calibre-hint-visible', 'calibre-hint-enter') |
| 149 |
v'delete a.dataset.calibreHintRender' |
| 150 |
v'delete a.dataset.calibreHintValue' |
| 151 |
|
| 152 |
|
| 153 |
def apply_prefix_to_hints(prefix): |
| 154 |
for a in document.body.querySelectorAll('[data-calibre-hint-value]'): |
| 155 |
val = int(a.dataset.calibreHintValue) |
| 156 |
r = encode(val) |
| 157 |
a.classList.remove('calibre-hint-enter') |
| 158 |
if not prefix or r.startsWith(prefix): |
| 159 |
a.classList.add('calibre-hint-visible') |
| 160 |
a.dataset.calibreHintRender = leftover = r[prefix.length:] or '\xa0' |
| 161 |
if leftover is '\xa0': |
| 162 |
a.classList.add('calibre-hint-enter') |
| 163 |
else: |
| 164 |
a.classList.remove('calibre-hint-visible') |