From 46cd8baed1a658b8bb7fa640acee9ef6d1fec0dd Mon Sep 17 00:00:00 2001 From: john Date: Sun, 10 Feb 2019 12:19:05 +0800 Subject: [PATCH] Avoiding add signal into the EmailRow, but translate seems hard to do since the webview is in the inside of message. --- .../conversation-email.vala | 2 +- .../conversation-list-box.vala | 19 +++++------------- .../conversation-message.vala | 20 ++++++++++++++----- .../conversation-web-view.vala | 6 +++--- ui/conversation-web-view.js | 18 ++++++++++++++--- 5 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/client/conversation-viewer/conversation-email.vala b/src/client/conversation-viewer/conversation-email.vala index 01a93e72..759a8e5b 100644 --- a/src/client/conversation-viewer/conversation-email.vala +++ b/src/client/conversation-viewer/conversation-email.vala @@ -421,7 +421,7 @@ public class ConversationEmail : Gtk.Box, Geary.BaseInterface { public signal void view_source(); /** Fired when a internal link is activated */ - public signal void internal_link_activated(uint y); + public signal void internal_link_activated(int y); /** Fired when the user selects text in a message. */ internal signal void body_selection_changed(bool has_selection); diff --git a/src/client/conversation-viewer/conversation-list-box.vala b/src/client/conversation-viewer/conversation-list-box.vala index 22015ea5..b6549fe2 100644 --- a/src/client/conversation-viewer/conversation-list-box.vala +++ b/src/client/conversation-viewer/conversation-list-box.vala @@ -327,15 +327,10 @@ public class ConversationListBox : Gtk.ListBox, Geary.BaseInterface { public ConversationEmail view { get; private set; } - /** Fired when a internal link is activated */ - public signal void internal_link_activated(EmailRow view, uint y); - - public EmailRow(ConversationEmail view) { base(view.email); this.view = view; add(view); - connect_email_signals(view); } public override async void expand() @@ -363,12 +358,6 @@ public class ConversationListBox : Gtk.ListBox, Geary.BaseInterface { } } - private void connect_email_signals(ConversationEmail email) { - email.internal_link_activated.connect((y) => { - internal_link_activated(this, y); - }); - } - } @@ -916,6 +905,10 @@ public class ConversationListBox : Gtk.ListBox, Geary.BaseInterface { ); view.mark_email.connect(on_mark_email); view.mark_email_from_here.connect(on_mark_email_from_here); + view.internal_link_activated.connect((y) => { + EmailRow row = get_email_row_by_id(view.email.id); + on_internal_link_activated(row, y); + }); view.body_selection_changed.connect((email, has_selection) => { this.body_selected_view = has_selection ? email : null; }); @@ -941,8 +934,6 @@ public class ConversationListBox : Gtk.ListBox, Geary.BaseInterface { } email_added(view); - row.internal_link_activated.connect(on_internal_link_activated); - return row; } @@ -1187,7 +1178,7 @@ public class ConversationListBox : Gtk.ListBox, Geary.BaseInterface { } } - private void on_internal_link_activated(EmailRow row, uint y) { + private void on_internal_link_activated(EmailRow row, int y) { scroll_to_anchor(row, y); } diff --git a/src/client/conversation-viewer/conversation-message.vala b/src/client/conversation-viewer/conversation-message.vala index 9ce020ea..865e6fe9 100644 --- a/src/client/conversation-viewer/conversation-message.vala +++ b/src/client/conversation-viewer/conversation-message.vala @@ -263,7 +263,7 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface { public signal void link_activated(string link); /** Fired when the user clicks a internal link in the email. */ - public signal void internal_link_activated(uint y); + public signal void internal_link_activated(int y); /** Fired when the user requests remote images be loaded. */ public signal void flag_remote_images(); @@ -1169,10 +1169,20 @@ public class ConversationMessage : Gtk.Grid, Geary.BaseInterface { if (link.contains(INTERNAL_ANCHOR_PREFIX)) { long start = INTERNAL_ANCHOR_PREFIX.length; long end = link.length; - this.web_view.get_anchor_target_y.begin(link.substring(start, end - start), (obj, res) => { - uint y = this.web_view.get_anchor_target_y.end(res); - internal_link_activated(y); - }); + string anchor_body = link.substring(start, end - start); + this.web_view.get_anchor_target_y.begin(anchor_body, (obj, res) => { + try { + int y = this.web_view.get_anchor_target_y.end(res); + //Workaround for JS.Error does not work well with primitive type. + if (y > 0) { + internal_link_activated(y); + } else { + debug("Failed to get anchor destination"); + } + } catch (Error err) { + debug("Failed to get anchor destination"); + } + }); } else { link_activated(link); } diff --git a/src/client/conversation-viewer/conversation-web-view.vala b/src/client/conversation-viewer/conversation-web-view.vala index e830d3b3..928ba479 100644 --- a/src/client/conversation-viewer/conversation-web-view.vala +++ b/src/client/conversation-viewer/conversation-web-view.vala @@ -99,11 +99,11 @@ public class ConversationWebView : ClientWebView { /** * Returns the y value for a element, by its id */ - public async uint? get_anchor_target_y(string id) throws Error { + public async int? get_anchor_target_y(string anchor_body) throws Error { WebKit.JavascriptResult result = yield call( - Geary.JS.callable("geary.getAnchorTargetY").string(id), null + Geary.JS.callable("geary.getAnchorTargetY").string(anchor_body), null ); - return (uint) WebKitUtil.to_number(result); + return (int) WebKitUtil.to_number(result); } /** diff --git a/ui/conversation-web-view.js b/ui/conversation-web-view.js index ec2624af..0415e723 100644 --- a/ui/conversation-web-view.js +++ b/ui/conversation-web-view.js @@ -244,9 +244,21 @@ ConversationPageState.prototype = { } return value; }, - getAnchorTargetY: function(id) { - let anchorTarget = document.getElementById(id); - return anchorTarget.getBoundingClientRect().top+document.documentElement.scrollTop; + getAnchorTargetY: function(anchor) { + let targetById = document.getElementById(anchor); + let targetByName = document.getElementsByName(anchor); + let finalTarget = null; + if (targetById != null) { + finalTarget = targetById; + } else if (targetByNmae.length > 0) { + finalTarget = targetByName[0]; + } + if (finalTarget != null) { + return finalTarget.getBoundingClientRect().top + + document.documentElement.scrollTop; + } else { + return -1; + } }, linkClicked: function(link) { let cancelClick = false;