mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Handle more than 36 hints on screen
This commit is contained in:
parent
4470b0aaaf
commit
99ddd5457d
@ -56,12 +56,16 @@ class Hints:
|
|||||||
if ev.key is 'Escape':
|
if ev.key is 'Escape':
|
||||||
self.hide()
|
self.hide()
|
||||||
return
|
return
|
||||||
|
if ev.key is 'Enter':
|
||||||
|
if self.current_prefix:
|
||||||
|
self.apply_prefix(True)
|
||||||
|
return
|
||||||
if ev.key is 'Backspace':
|
if ev.key is 'Backspace':
|
||||||
if self.current_prefix:
|
if self.current_prefix:
|
||||||
self.current_prefix = self.current_prefix[:-1]
|
self.current_prefix = self.current_prefix[:-1]
|
||||||
self.apply_prefix()
|
self.apply_prefix()
|
||||||
return
|
return
|
||||||
hint_keys = list('01234567890abcdefghijklmnopqrstuvwxyz')
|
hint_keys = list('1234567890abcdefghijklmnopqrstuvwxyz')
|
||||||
q = ev.key.toLowerCase()
|
q = ev.key.toLowerCase()
|
||||||
if hint_keys.indexOf(q) > -1:
|
if hint_keys.indexOf(q) > -1:
|
||||||
self.current_prefix += q
|
self.current_prefix += q
|
||||||
@ -75,15 +79,24 @@ class Hints:
|
|||||||
ev.stopPropagation(), ev.preventDefault()
|
ev.stopPropagation(), ev.preventDefault()
|
||||||
self.hide()
|
self.hide()
|
||||||
|
|
||||||
def apply_prefix(self):
|
def apply_prefix(self, accept_full_match):
|
||||||
matches = v'[]'
|
matches = v'[]'
|
||||||
if self.current_prefix:
|
if self.current_prefix:
|
||||||
for k in Object.keys(self.hints_map):
|
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)
|
matches.push(k)
|
||||||
if matches.length is 1:
|
if matches.length is 1:
|
||||||
self.send_message('activate', hint=self.hints_map[matches[0]])
|
self.send_message('activate', hint=self.hints_map[matches[0]])
|
||||||
self.hide()
|
self.hide()
|
||||||
|
else:
|
||||||
|
self.send_message('apply_prefix', prefix=self.current_prefix)
|
||||||
|
|
||||||
def send_message(self, type, **kw):
|
def send_message(self, type, **kw):
|
||||||
self.view.iframe_wrapper.send_message('hints', type=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
|
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():
|
def hint_visible_links():
|
||||||
i = 0
|
i = 0
|
||||||
hint_map = {}
|
hint_map = {}
|
||||||
@ -118,7 +135,7 @@ def hint_visible_links():
|
|||||||
if is_visible(a):
|
if is_visible(a):
|
||||||
i += 1
|
i += 1
|
||||||
h = i + ''
|
h = i + ''
|
||||||
a.dataset.calibreHintRender = i.toString(36)
|
a.dataset.calibreHintRender = encode(i)
|
||||||
a.dataset.calibreHintValue = h
|
a.dataset.calibreHintValue = h
|
||||||
a.classList.add('calibre-hint-visible')
|
a.classList.add('calibre-hint-visible')
|
||||||
hint_map[h] = {'type': 'link', 'value': i}
|
hint_map[h] = {'type': 'link', 'value': i}
|
||||||
@ -128,6 +145,20 @@ def hint_visible_links():
|
|||||||
|
|
||||||
def unhint_links():
|
def unhint_links():
|
||||||
for a in document.body.querySelectorAll('a[href]'):
|
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.calibreHintRender'
|
||||||
v'delete a.dataset.calibreHintValue'
|
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')
|
||||||
|
@ -11,7 +11,7 @@ from range_utils import (
|
|||||||
last_span_for_crw, reset_highlight_counter, select_crw, unwrap_all_crw,
|
last_span_for_crw, reset_highlight_counter, select_crw, unwrap_all_crw,
|
||||||
unwrap_crw, wrap_text_in_range
|
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.cfi import cfi_for_selection, range_from_cfi
|
||||||
from read_book.extract import get_elements
|
from read_book.extract import get_elements
|
||||||
from read_book.find import (
|
from read_book.find import (
|
||||||
@ -936,6 +936,8 @@ class IframeBoss:
|
|||||||
a.removeEventListener('animationend', self.hint_animation_ended)
|
a.removeEventListener('animationend', self.hint_animation_ended)
|
||||||
a.addEventListener('animationend', self.hint_animation_ended, False)
|
a.addEventListener('animationend', self.hint_animation_ended, False)
|
||||||
a.classList.add('calibre-animated-hint')
|
a.classList.add('calibre-animated-hint')
|
||||||
|
elif data.type is 'apply_prefix':
|
||||||
|
apply_prefix_to_hints(data.prefix)
|
||||||
|
|
||||||
def hint_animation_ended(self, ev):
|
def hint_animation_ended(self, ev):
|
||||||
a = ev.currentTarget
|
a = ev.currentTarget
|
||||||
|
@ -128,6 +128,10 @@ def apply_colors(is_content_popup):
|
|||||||
|
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
.calibre-hint-enter::before {{
|
||||||
|
background: #FF5733 !important; \
|
||||||
|
}}
|
||||||
|
|
||||||
.calibre-animated-hint {{
|
.calibre-animated-hint {{
|
||||||
animation-name: calibre-animate-hint; \
|
animation-name: calibre-animate-hint; \
|
||||||
animation-duration: 0.3s; \
|
animation-duration: 0.3s; \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user