mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
EPUB CFI: at_current() works
This commit is contained in:
parent
bc4ff3e011
commit
df246b0979
@ -579,29 +579,30 @@ class CanonicalFragmentIdentifier
|
||||
|
||||
get_cfi = (ox, oy) ->
|
||||
try
|
||||
cfi = this.at(ox, oy)
|
||||
point = this.point(cfi)
|
||||
cfi = window.cfi.at(ox, oy)
|
||||
point = window.cfi.point(cfi)
|
||||
catch err
|
||||
cfi = null
|
||||
|
||||
if point.range != null
|
||||
r = point.range
|
||||
rect = r.getClientRects()[0]
|
||||
if cfi
|
||||
if point.range != null
|
||||
r = point.range
|
||||
rect = r.getClientRects()[0]
|
||||
|
||||
x = (point.a*rect.left + (1-point.a)*rect.right)
|
||||
y = (rect.top + rect.bottom)/2
|
||||
[x, y] = viewport_to_document(x, y, r.startContainer.ownerDocument)
|
||||
else
|
||||
node = point.node
|
||||
r = node.getBoundingClientRect()
|
||||
[x, y] = viewport_to_document(r.left, r.top, node.ownerDocument)
|
||||
if typeof(point.x) == 'number' and node.offsetWidth
|
||||
x += (point.x*node.offsetWidth)/100
|
||||
if typeof(point.y) == 'number' and node.offsetHeight
|
||||
y += (point.y*node.offsetHeight)/100
|
||||
x = (point.a*rect.left + (1-point.a)*rect.right)
|
||||
y = (rect.top + rect.bottom)/2
|
||||
[x, y] = viewport_to_document(x, y, r.startContainer.ownerDocument)
|
||||
else
|
||||
node = point.node
|
||||
r = node.getBoundingClientRect()
|
||||
[x, y] = viewport_to_document(r.left, r.top, node.ownerDocument)
|
||||
if typeof(point.x) == 'number' and node.offsetWidth
|
||||
x += (point.x*node.offsetWidth)/100
|
||||
if typeof(point.y) == 'number' and node.offsetHeight
|
||||
y += (point.y*node.offsetHeight)/100
|
||||
|
||||
if dist(viewport_to_document(ox, oy), [x, y]) > 50
|
||||
cfi = null
|
||||
if dist(viewport_to_document(ox, oy), [x, y]) > 50
|
||||
cfi = null
|
||||
|
||||
return cfi
|
||||
|
||||
|
@ -30,18 +30,23 @@ window_ypos = (pos=null) ->
|
||||
window.scrollTo(0, pos)
|
||||
|
||||
mark_and_reload = (evt) ->
|
||||
# Remove image in case the click was on the image itself, we want the cfi to
|
||||
# be on the underlying element
|
||||
x = evt.clientX
|
||||
y = evt.clientY
|
||||
if evt.button == 2
|
||||
return # Right mouse click, generated only in firefox
|
||||
reset = document.getElementById('reset')
|
||||
if document.elementFromPoint(x, y) == reset
|
||||
|
||||
if document.elementFromPoint(x, y)?.getAttribute('id') in ['reset', 'viewport_mode']
|
||||
return
|
||||
|
||||
# Remove image in case the click was on the image itself, we want the cfi to
|
||||
# be on the underlying element
|
||||
ms = document.getElementById("marker")
|
||||
if ms
|
||||
ms.parentNode?.removeChild(ms)
|
||||
ms.style.display = 'none'
|
||||
|
||||
if document.getElementById('viewport_mode').checked
|
||||
cfi = window.cfi.at_current()
|
||||
window.cfi.scroll_to(cfi)
|
||||
return
|
||||
|
||||
fn = () ->
|
||||
try
|
||||
|
@ -8,6 +8,7 @@
|
||||
body {
|
||||
font-family: sans-serif;
|
||||
background-color: white;
|
||||
padding-bottom: 500px;
|
||||
}
|
||||
|
||||
h1, h2 { color: #005a9c }
|
||||
@ -48,7 +49,13 @@
|
||||
<div id="container">
|
||||
<h1 id="first-h1">Testing cfi.coffee</h1>
|
||||
<p>Click anywhere and the location will be marked with a marker, whose position is set via a CFI.</p>
|
||||
<p><a id="reset" href="/">Reset CFI to None</a></p>
|
||||
<p>
|
||||
<a id="reset" href="/">Reset CFI to None</a>
|
||||
|
||||
Test viewport location calculation:
|
||||
<input type="checkbox" id="viewport_mode" title=
|
||||
"Checking this will cause the window to scroll to a position based on a CFI calculated for the windows current position."/>
|
||||
</p>
|
||||
<h2>A div with scrollbars</h2>
|
||||
<p>Scroll down and click on some elements. Make sure to hit both
|
||||
bold and not bold text as well as different points on the image</p>
|
||||
|
Loading…
x
Reference in New Issue
Block a user