From cc0e4f9dc79d4d5bcdc763e81f76d2f0c94d6b79 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 4 Apr 2016 10:41:16 +0530 Subject: [PATCH] Port cfi.coffee to cfi.pyj --- .../ebooks/oeb/display/test-cfi/cfi-test.pyj | 73 +++ .../ebooks/oeb/display/test-cfi/iframe.html | 1 + .../ebooks/oeb/display/test-cfi/index.html | 2 + .../oeb/display/test-cfi/run_rapydscript.py | 39 ++ src/pyj/read_book/cfi.pyj | 590 ++++++++++++++++++ 5 files changed, 705 insertions(+) create mode 100644 src/calibre/ebooks/oeb/display/test-cfi/cfi-test.pyj create mode 100644 src/calibre/ebooks/oeb/display/test-cfi/run_rapydscript.py create mode 100644 src/pyj/read_book/cfi.pyj diff --git a/src/calibre/ebooks/oeb/display/test-cfi/cfi-test.pyj b/src/calibre/ebooks/oeb/display/test-cfi/cfi-test.pyj new file mode 100644 index 0000000000..74108dc120 --- /dev/null +++ b/src/calibre/ebooks/oeb/display/test-cfi/cfi-test.pyj @@ -0,0 +1,73 @@ +# vim:fileencoding=utf-8 +# License: GPL v3 Copyright: 2016, Kovid Goyal + +from cfi import scroll_to, at_current, at + +def show_cfi(): + if window.current_cfi: + scroll_to(window.current_cfi, def(x, y): + ms = document.getElementById("marker").style + ms.display = 'block' + ms.top = y - 30 + 'px' + ms.left = x - 1 + 'px' + ) + +def mark_and_reload(evt): + x = evt.clientX + y = evt.clientY + if evt.button is 2: + return # Right mouse click, generated only in firefox + + elem = document.elementFromPoint(x, y) + if elem and elem.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") + ms.style.display = 'none' + + if document.getElementById('viewport_mode').checked: + cfi = at_current() + scroll_to(cfi) + return + + def fn(): + try: + window.current_cfi = at(x, y) + except Exception as err: + alert(str.format("Failed to calculate cfi: {}", err.message)) + return + if window.current_cfi: + epubcfi = 'epubcfi(' + window.current_cfi + ')' + ypos = window.pageYOffset + newloc = window.location.href.replace(/#.*$/, '') + "#" + ypos + epubcfi + window.location.replace(newloc) + window.location.reload() + + setTimeout(fn, 1) + +def frame_clicked(evt): + iframe = evt.target.ownerDocument.defaultView.frameElement + # We know that the offset parent of the iframe is body + # So we can easily calculate the event co-ords w.r.t. the browser window + rect = iframe.getBoundingClientRect() + x = evt.clientX + rect.left + y = evt.clientY + rect.top + mark_and_reload({'clientX':x, 'clientY':y, 'button':evt.button}) + +window.onload = def(): + document.onclick = mark_and_reload + for iframe in document.getElementsByTagName("iframe"): + iframe.contentWindow.document.onclick = frame_clicked + + r = window.location.hash.match(/#(\d*)epubcfi\((.+)\)$/) + if r: + window.current_cfi = r[2] + ypos = 0+r[1] if r[1] else 0 + base = document.getElementById('first-h1').innerHTML + document.title = base + ": " + window.current_cfi + setTimeout(def(): + show_cfi() + window.scrollTo(0, ypos) + , 100) diff --git a/src/calibre/ebooks/oeb/display/test-cfi/iframe.html b/src/calibre/ebooks/oeb/display/test-cfi/iframe.html index 94761560fb..f2c4527aeb 100644 --- a/src/calibre/ebooks/oeb/display/test-cfi/iframe.html +++ b/src/calibre/ebooks/oeb/display/test-cfi/iframe.html @@ -1,6 +1,7 @@ + Testing EPUB CFI diff --git a/src/calibre/ebooks/oeb/display/test-cfi/index.html b/src/calibre/ebooks/oeb/display/test-cfi/index.html index 42bcf626b1..057ce47500 100644 --- a/src/calibre/ebooks/oeb/display/test-cfi/index.html +++ b/src/calibre/ebooks/oeb/display/test-cfi/index.html @@ -2,6 +2,8 @@ Testing cfi.coffee + +