Handle more than 36 hints on screen

This commit is contained in:
Kovid Goyal 2020-12-15 22:26:26 +05:30
parent 4470b0aaaf
commit 99ddd5457d
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 43 additions and 6 deletions

View File

@ -56,12 +56,16 @@ class Hints:
if ev.key is 'Escape':
self.hide()
return
if ev.key is 'Enter':
if self.current_prefix:
self.apply_prefix(True)
return
if ev.key is 'Backspace':
if self.current_prefix:
self.current_prefix = self.current_prefix[:-1]
self.apply_prefix()
return
hint_keys = list('01234567890abcdefghijklmnopqrstuvwxyz')
hint_keys = list('1234567890abcdefghijklmnopqrstuvwxyz')
q = ev.key.toLowerCase()
if hint_keys.indexOf(q) > -1:
self.current_prefix += q
@ -75,15 +79,24 @@ class Hints:
ev.stopPropagation(), ev.preventDefault()
self.hide()
def apply_prefix(self):
def apply_prefix(self, accept_full_match):
matches = v'[]'
if self.current_prefix:
for k in Object.keys(self.hints_map):
if k.startswith(self.current_prefix):
if k is '_length':
continue
q = encode(int(k))
if accept_full_match:
if q is self.current_prefix:
matches.push(k)
break
elif q.startswith(self.current_prefix):
matches.push(k)
if matches.length is 1:
self.send_message('activate', hint=self.hints_map[matches[0]])
self.hide()
else:
self.send_message('apply_prefix', prefix=self.current_prefix)
def send_message(self, type, **kw):
self.view.iframe_wrapper.send_message('hints', type=type, **kw)
@ -111,6 +124,10 @@ def is_visible(a):
return rect.left >= 0 and rect.top >= 0 and rect.left < window.innerWidth and rect.top < window.innerHeight
def encode(i):
return i.toString(36).toLowerCase()
def hint_visible_links():
i = 0
hint_map = {}
@ -118,7 +135,7 @@ def hint_visible_links():
if is_visible(a):
i += 1
h = i + ''
a.dataset.calibreHintRender = i.toString(36)
a.dataset.calibreHintRender = encode(i)
a.dataset.calibreHintValue = h
a.classList.add('calibre-hint-visible')
hint_map[h] = {'type': 'link', 'value': i}
@ -128,6 +145,20 @@ def hint_visible_links():
def unhint_links():
for a in document.body.querySelectorAll('a[href]'):
a.classList.remove('calibre-hint-visible')
a.classList.remove('calibre-hint-visible', 'calibre-hint-enter')
v'delete a.dataset.calibreHintRender'
v'delete a.dataset.calibreHintValue'
def apply_prefix_to_hints(prefix):
for a in document.body.querySelectorAll('[data-calibre-hint-value]'):
val = int(a.dataset.calibreHintValue)
r = encode(val)
a.classList.remove('calibre-hint-enter')
if not prefix or r.startsWith(prefix):
a.classList.add('calibre-hint-visible')
a.dataset.calibreHintRender = leftover = r[prefix.length:] or '\xa0'
if leftover is '\xa0':
a.classList.add('calibre-hint-enter')
else:
a.classList.remove('calibre-hint-visible')

View File

@ -11,7 +11,7 @@ from range_utils import (
last_span_for_crw, reset_highlight_counter, select_crw, unwrap_all_crw,
unwrap_crw, wrap_text_in_range
)
from read_book.hints import hint_visible_links, unhint_links
from read_book.hints import hint_visible_links, unhint_links, apply_prefix_to_hints
from read_book.cfi import cfi_for_selection, range_from_cfi
from read_book.extract import get_elements
from read_book.find import (
@ -936,6 +936,8 @@ class IframeBoss:
a.removeEventListener('animationend', self.hint_animation_ended)
a.addEventListener('animationend', self.hint_animation_ended, False)
a.classList.add('calibre-animated-hint')
elif data.type is 'apply_prefix':
apply_prefix_to_hints(data.prefix)
def hint_animation_ended(self, ev):
a = ev.currentTarget

View File

@ -128,6 +128,10 @@ def apply_colors(is_content_popup):
}}
.calibre-hint-enter::before {{
background: #FF5733 !important; \
}}
.calibre-animated-hint {{
animation-name: calibre-animate-hint; \
animation-duration: 0.3s; \