From df246b09791b1485bb8d69a1649d72be85358429 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 25 Jan 2012 13:02:40 +0530 Subject: [PATCH] EPUB CFI: at_current() works --- src/calibre/ebooks/oeb/display/cfi.coffee | 37 ++++++++++--------- .../oeb/display/test-cfi/cfi-test.coffee | 17 ++++++--- .../ebooks/oeb/display/test-cfi/index.html | 9 ++++- 3 files changed, 38 insertions(+), 25 deletions(-) diff --git a/src/calibre/ebooks/oeb/display/cfi.coffee b/src/calibre/ebooks/oeb/display/cfi.coffee index 7e1649201a..6436191da3 100644 --- a/src/calibre/ebooks/oeb/display/cfi.coffee +++ b/src/calibre/ebooks/oeb/display/cfi.coffee @@ -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 diff --git a/src/calibre/ebooks/oeb/display/test-cfi/cfi-test.coffee b/src/calibre/ebooks/oeb/display/test-cfi/cfi-test.coffee index 663e830441..bed03d6ff7 100644 --- a/src/calibre/ebooks/oeb/display/test-cfi/cfi-test.coffee +++ b/src/calibre/ebooks/oeb/display/test-cfi/cfi-test.coffee @@ -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 diff --git a/src/calibre/ebooks/oeb/display/test-cfi/index.html b/src/calibre/ebooks/oeb/display/test-cfi/index.html index 8398d27791..42bcf626b1 100644 --- a/src/calibre/ebooks/oeb/display/test-cfi/index.html +++ b/src/calibre/ebooks/oeb/display/test-cfi/index.html @@ -8,6 +8,7 @@ body { font-family: sans-serif; background-color: white; + padding-bottom: 500px; } h1, h2 { color: #005a9c } @@ -48,7 +49,13 @@

Testing cfi.coffee

Click anywhere and the location will be marked with a marker, whose position is set via a CFI.

-

Reset CFI to None

+

+ Reset CFI to None +   + Test viewport location calculation: + +

A div with scrollbars

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