From f8e8d9caa09380e3e864fede1e6935570cbd0a4f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 25 May 2014 14:29:25 +0530 Subject: [PATCH] Preview panel: Allow syncing of position more accurately when there are multiple tags on the same source code line --- resources/compiled_coffeescript.zip | Bin 91255 -> 92080 bytes src/calibre/ebooks/oeb/polish/preview.coffee | 23 +++++++++++++------ src/calibre/gui2/tweak_book/boss.py | 6 ++--- src/calibre/gui2/tweak_book/preview.py | 20 +++++++++++----- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/resources/compiled_coffeescript.zip b/resources/compiled_coffeescript.zip index bd1e285820ae37d4f24d541bc62888ac1000150f..950086af8e723461a85a3fe3e45f263c956445f6 100644 GIT binary patch delta 659 zcmZWnOK1~O6wM9hLt7@HNhQ`cz7#u6Z48-7(`K-#^@G?Zwvkp_m6}d6FX=$cl$j|i zR&b*$(aJ;a)`dG4ekdLM>_l)USOm9jTm=+dWLHg^6tUjTyYHOCdH241YabmiJ~{4e zK(L?78m~{k+x$>d#%wla%!>3w7Cgi#=vuj)GqqerQz%=yzi55sp_g!bh*&XzztV7+ z-7`5>{n3HZ47l0jc8^o1=Bi#{xoi*IwyI~~iyha8p$ku*g-#?q_)(k$pY?VU766}c z;J|7(w4=q<>4%q~56`8+&+Zlm*-sCT-Vhu_avApFn>0u$odB-$-%`T$LfP>x0bO&@ zgRyyVHr@;3=LHBl)rvxIL~wfwX0Vum0Or!*a_^udMc6mbK*#Q7Xeg)Rqcj9A?jVcD z+DI$ZA_+HD<0)dwB_mpZfFbh6hO~)l^?;`mx{1 zfxf?1sTRBIb(9E%=Sis1XD^E;UB3U@q$p>=$Cj?Taor0pw({Q5Rni*6DytP5Hndf} zKpQF6W+7rd&%hD4S5_3A8b*ZB2wB!?5x>vDQ&$VcL*vj5)OvOSB-lSSJ2M?MOkJ&% z)Z)!>OT#9%4s6atf7_mw!MF?)u2(YHw(Ym@Wgh(20~uEA;e@EjiMU8dM@I7Tgdhpx au%aZSydWi$;;1MK$+#3#L;-mP&i?@qE!zM9 delta 253 zcmdmRp7r|))`l&Na;ki)jayv)E1wY5wq#(?w%o3x${4^nIk6{Z`wKNjS%vLu+!@a> zZ9n717{oYTUV)Kg`g>nSqv=ULj3SeFw%JT~h-H+W{=|pTa=L&&Bg=GscSeQDpROuT zKK)*JazcRo?rMc6&BN&%( zfLIE)3aQfzq8XL8--=)~U}OP_ZC8w9wB return ans.reverse() +scroll_to_node = (node) -> + if node is document.body + window.scrollTo(0, 0) + else + node.scrollIntoView() class PreviewIntegration @@ -244,11 +249,15 @@ class PreviewIntegration for node in document.querySelectorAll('[data-lnum="' + lnum + '"]') if is_hidden(node) continue - if node is document.body - window.scrollTo(0, 0) - else - node.scrollIntoView() - return + scroll_to_node(node) + + go_to_sourceline_address: (sourceline, tags) => + for node, index in document.querySelectorAll('[data-lnum="' + sourceline + '"]') + if index >= tags.length or node.tagName.toLowerCase() != tags[index] + break + if index == tags.length - 1 and not is_hidden(node) + return scroll_to_node(node) + this.go_to_line(sourceline) line_numbers: () => found_body = false @@ -300,14 +309,14 @@ class PreviewIntegration this.report_split(event.target) else e = event.target + address = get_sourceline_address(e) # Find the closest containing link, if any - lnum = e.getAttribute('data-lnum') href = tn = '' while e and e != document.body and e != document and (tn != 'a' or not href) tn = e.tagName?.toLowerCase() href = e.getAttribute('href') e = e.parentNode - window.py_bridge.request_sync(tn, href, lnum) + window.py_bridge.request_sync(tn, href, JSON.stringify(address)) return false go_to_anchor: (anchor, lnum) => diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 78885afd9f..b3ca169765 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -1014,11 +1014,11 @@ class Boss(QObject): mt = current_container().mime_map.get(name, guess_type(name)) self.edit_file_requested(name, None, mt) - def sync_editor_to_preview(self, name, lnum): + def sync_editor_to_preview(self, name, sourceline_address): editor = self.edit_file(name, 'html') self.ignore_preview_to_editor_sync = True try: - editor.current_line = lnum + editor.goto_sourceline(*sourceline_address) finally: self.ignore_preview_to_editor_sync = False @@ -1029,7 +1029,7 @@ class Boss(QObject): if ed is not None: name = editor_name(ed) if name is not None and getattr(ed, 'syntax', None) == 'html': - self.gui.preview.sync_to_editor(name, ed.current_line) + self.gui.preview.sync_to_editor(name, ed.current_tag()) def sync_live_css_to_editor(self): ed = self.gui.central.current_editor diff --git a/src/calibre/gui2/tweak_book/preview.py b/src/calibre/gui2/tweak_book/preview.py index 757347b73e..baf7abd927 100644 --- a/src/calibre/gui2/tweak_book/preview.py +++ b/src/calibre/gui2/tweak_book/preview.py @@ -330,9 +330,9 @@ class WebPage(QWebPage): mf.evaluateJavaScript(self.js) @pyqtSlot(str, str, str) - def request_sync(self, tag_name, href, lnum): + def request_sync(self, tag_name, href, sourceline_address): try: - self.sync_requested.emit(unicode(tag_name), unicode(href), int(unicode(lnum))) + self.sync_requested.emit(unicode(tag_name), unicode(href), json.loads(unicode(sourceline_address))) except (TypeError, ValueError, OverflowError, AttributeError): pass @@ -369,6 +369,14 @@ class WebPage(QWebPage): self.mainFrame().evaluateJavaScript( 'window.calibre_preview_integration.go_to_line(%d)' % lnum) + def go_to_sourceline_address(self, sourceline_address): + lnum, tags = sourceline_address + if lnum is None: + return + tags = [x.lower() for x in tags] + self.mainFrame().evaluateJavaScript( + 'window.calibre_preview_integration.go_to_sourceline_address(%d, %s)' % (lnum, json.dumps(tags))) + def split_mode(self, enabled): self.mainFrame().evaluateJavaScript( 'window.calibre_preview_integration.split_mode(%s)' % ( @@ -538,8 +546,8 @@ class Preview(QWidget): if self.current_name: self.split_requested.emit(self.current_name, loc, totals) - def sync_to_editor(self, name, lnum): - self.current_sync_request = (name, lnum) + def sync_to_editor(self, name, sourceline_address): + self.current_sync_request = (name, sourceline_address) QTimer.singleShot(100, self._sync_to_editor) def _sync_to_editor(self): @@ -550,9 +558,9 @@ class Preview(QWidget): return QTimer.singleShot(100, self._sync_to_editor) except TypeError: return # Happens if current_sync_request is None - lnum = self.current_sync_request[1] + sourceline_address = self.current_sync_request[1] self.current_sync_request = None - self.view.page().go_to_line(lnum) + self.view.page().go_to_sourceline_address(sourceline_address) def show(self, name): if name != self.current_name: