From c6bf118b3847e8574111c1772bbde79ecf349636 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 19 Jun 2012 13:58:58 +0530 Subject: [PATCH] Fix searching in paged mode --- resources/compiled_coffeescript.zip | Bin 36336 -> 36843 bytes src/calibre/ebooks/oeb/display/paged.coffee | 16 ++++++++++++++++ src/calibre/gui2/viewer/documentview.py | 8 +++++--- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/resources/compiled_coffeescript.zip b/resources/compiled_coffeescript.zip index bacbcf8aaf11d325c9b2430b2ae6345b1dcc2112..dc290df6e3bbf87534acc106d57772b73a22df44 100644 GIT binary patch delta 363 zcmew`o9Xp@Cg}igW)=|!5b(^u?7-JPWA-v71_lt8p8Q`yY_feX$41KpF~Q6<1&xx* zg4Fypg^GgwVg<#?j%DJ;Ir-@tAg+#rdSH#Fym97pLZ=CQtq=DHooSpRA*hlbTkd z12irrRYw765>VEfO92dkOa)to^31%H{BphY)RJJ3#*)nZJPl2lk|Ll2pfZqbP-0$s zs$+?Uf#&4KbOl9_fl$fByyT4hB0r!2OdH6~$pO_8lk+(_Ccmv==K{I}s2HSYavi7G zes6k7#LP7Y?Npe+bk@;j&rk<(%~FFRLRN0 e{gF&h6ej2Odof+tnfy*qdh*qN0k(fVAaekuNg%WU diff --git a/src/calibre/ebooks/oeb/display/paged.coffee b/src/calibre/ebooks/oeb/display/paged.coffee index a65a452483..f4dcf4309e 100644 --- a/src/calibre/ebooks/oeb/display/paged.coffee +++ b/src/calibre/ebooks/oeb/display/paged.coffee @@ -140,6 +140,9 @@ class PagedDisplay scroll_to_xpos: (xpos) -> # Scroll so that the column containing xpos is the left most column in # the viewport + if typeof(xpos) != 'number' + log(xpos, 'is not a number, cannot scroll to it!') + return pos = 0 until (pos <= xpos < pos + this.page_width) pos += this.page_width @@ -206,6 +209,9 @@ class PagedDisplay return ans jump_to_anchor: (name) -> + # Jump to the element identified by anchor name. Ensures that the left + # most column in the viewport is the column containing the start of the + # element and that the scroll position is at the start of the column. elem = document.getElementById(name) if !elem elems = document.getElementsByName(name) @@ -218,6 +224,16 @@ class PagedDisplay # Ensure we are scrolled to the column containing elem this.scroll_to_xpos(absleft(elem) + 5) + snap_to_selection: () -> + sel = window.getSelection() + r = sel.getRangeAt(0).getBoundingClientRect() + node = sel.anchorNode + left = viewport_to_document(r.left, r.top, doc=node.ownerDocument)[0] + if this.in_paged_mode + # Ensure we are scrolled to the column containing the start of the + # selection + this.scroll_to_xpos(left+5) + if window? window.paged_display = new PagedDisplay() diff --git a/src/calibre/gui2/viewer/documentview.py b/src/calibre/gui2/viewer/documentview.py index 602f024de3..f0b6668c1d 100644 --- a/src/calibre/gui2/viewer/documentview.py +++ b/src/calibre/gui2/viewer/documentview.py @@ -588,9 +588,11 @@ class DocumentView(QWebView): # {{{ return property(fget=fget, fset=fset) def search(self, text, backwards=False): - if backwards: - return self.findText(text, self.document.FindBackward) - return self.findText(text) + flags = self.document.FindBackward if backwards else self.document.FindFlags(0) + found = self.findText(text, flags) + if found and self.document.in_paged_mode: + self.document.javascript('paged_display.snap_to_selection()') + return found def path(self): return os.path.abspath(unicode(self.url().toLocalFile()))