2016-11-30 22:56:01 +11:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright 2016 Software Freedom Conservancy Inc.
|
|
|
|
|
|
* Copyright 2016 Michael Gratton <mike@vee.net>
|
|
|
|
|
|
*
|
|
|
|
|
|
* This software is licensed under the GNU Lesser General Public License
|
|
|
|
|
|
* (version 2.1 or later). See the COPYING file in this distribution.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Application logic for ComposerWebView.
|
|
|
|
|
|
*/
|
2017-01-04 01:15:08 +11:00
|
|
|
|
let ComposerPageState = function() {
|
2016-11-30 22:56:01 +11:00
|
|
|
|
this.init.apply(this, arguments);
|
|
|
|
|
|
};
|
2016-12-02 01:08:07 +11:00
|
|
|
|
ComposerPageState.BODY_ID = "message-body";
|
2017-01-26 13:31:08 +11:00
|
|
|
|
ComposerPageState.KEYWORD_SPLIT_REGEX = /[\s]+/g;
|
2017-01-26 16:31:03 +11:00
|
|
|
|
ComposerPageState.QUOTE_START = "\x91"; // private use one
|
|
|
|
|
|
ComposerPageState.QUOTE_END = "\x92"; // private use two
|
|
|
|
|
|
ComposerPageState.QUOTE_MARKER = "\x7f"; // delete
|
|
|
|
|
|
ComposerPageState.PROTOCOL_REGEX = /^(aim|apt|bitcoin|cvs|ed2k|ftp|file|finger|git|gtalk|http|https|irc|ircs|irc6|lastfm|ldap|ldaps|magnet|news|nntp|rsync|sftp|skype|smb|sms|svn|telnet|tftp|ssh|webcal|xmpp):/i;
|
2017-01-26 13:31:08 +11:00
|
|
|
|
// Taken from Geary.HTML.URL_REGEX, without the inline modifier (?x)
|
|
|
|
|
|
// at the start, which is unsupported in JS
|
2017-01-26 16:31:03 +11:00
|
|
|
|
ComposerPageState.URL_REGEX = new RegExp("\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))", "gi");
|
2016-12-02 01:08:07 +11:00
|
|
|
|
|
2016-11-30 22:56:01 +11:00
|
|
|
|
ComposerPageState.prototype = {
|
|
|
|
|
|
__proto__: PageState.prototype,
|
|
|
|
|
|
init: function() {
|
|
|
|
|
|
PageState.prototype.init.apply(this, []);
|
2016-12-03 11:20:00 +11:00
|
|
|
|
|
2017-01-04 18:53:28 +11:00
|
|
|
|
this.messageBody = null;
|
|
|
|
|
|
|
|
|
|
|
|
this.undoEnabled = false;
|
|
|
|
|
|
this.redoEnabled = false;
|
|
|
|
|
|
|
2017-01-19 01:48:03 +11:00
|
|
|
|
this.cursorContext = null;
|
2017-01-04 20:11:01 +11:00
|
|
|
|
|
2017-01-02 10:51:26 +11:00
|
|
|
|
let state = this;
|
2017-01-04 18:53:28 +11:00
|
|
|
|
|
2016-12-03 11:20:00 +11:00
|
|
|
|
document.addEventListener("click", function(e) {
|
|
|
|
|
|
if (e.target.tagName == "A") {
|
2017-01-19 13:16:13 +11:00
|
|
|
|
e.preventDefault();
|
2016-12-03 11:20:00 +11:00
|
|
|
|
}
|
|
|
|
|
|
}, true);
|
2017-01-04 18:53:28 +11:00
|
|
|
|
|
2017-01-06 10:48:40 +11:00
|
|
|
|
let modifiedId = null;
|
2017-01-04 18:53:28 +11:00
|
|
|
|
this.bodyObserver = new MutationObserver(function() {
|
2017-01-06 10:48:40 +11:00
|
|
|
|
if (modifiedId == null) {
|
|
|
|
|
|
modifiedId = window.setTimeout(function() {
|
|
|
|
|
|
state.documentModified();
|
|
|
|
|
|
state.checkCommandStack();
|
|
|
|
|
|
modifiedId = null;
|
|
|
|
|
|
}, 1000);
|
|
|
|
|
|
}
|
2017-01-04 18:53:28 +11:00
|
|
|
|
});
|
2016-11-30 22:56:01 +11:00
|
|
|
|
},
|
|
|
|
|
|
loaded: function() {
|
2017-01-05 13:12:14 +11:00
|
|
|
|
let state = this;
|
|
|
|
|
|
|
2017-01-04 18:53:28 +11:00
|
|
|
|
this.messageBody = document.getElementById(ComposerPageState.BODY_ID);
|
2017-01-05 13:12:14 +11:00
|
|
|
|
this.messageBody.addEventListener("keydown", function(e) {
|
2017-01-25 17:14:13 +11:00
|
|
|
|
// Should be using 'e.key == "Tab"' here, but that was
|
|
|
|
|
|
// only fixed in WK in Oct 2016 (WK Bug 36267). Migrate to
|
|
|
|
|
|
// that when we can rely on it being in WebKitGTK.
|
|
|
|
|
|
if (e.keyIdentifier == "U+0009"
|
|
|
|
|
|
&& !e.ctrlKey
|
|
|
|
|
|
&& !e.altKey
|
|
|
|
|
|
&& !e.metaKey) {
|
2017-01-05 13:12:14 +11:00
|
|
|
|
if (!e.shiftKey) {
|
|
|
|
|
|
state.tabOut();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
state.tabIn();
|
|
|
|
|
|
}
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2017-01-04 18:53:28 +11:00
|
|
|
|
|
2016-11-30 22:56:01 +11:00
|
|
|
|
// Search for and remove a particular styling when we quote
|
|
|
|
|
|
// text. If that style exists in the quoted text, we alter it
|
|
|
|
|
|
// slightly so we don't mess with it later.
|
2017-01-02 10:51:26 +11:00
|
|
|
|
let nodeList = document.querySelectorAll(
|
2016-11-30 22:56:01 +11:00
|
|
|
|
"blockquote[style=\"margin: 0 0 0 40px; border: none; padding: 0px;\"]");
|
2017-01-02 10:51:26 +11:00
|
|
|
|
for (let i = 0; i < nodeList.length; ++i) {
|
2016-11-30 22:56:01 +11:00
|
|
|
|
nodeList.item(i).setAttribute(
|
|
|
|
|
|
"style",
|
|
|
|
|
|
"margin: 0 0 0 40px; padding: 0px; border:none;"
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Focus within the HTML document
|
|
|
|
|
|
document.body.focus();
|
|
|
|
|
|
|
2017-01-19 01:48:03 +11:00
|
|
|
|
// Set text cursor at appropriate position
|
2017-01-02 10:51:26 +11:00
|
|
|
|
let cursor = document.getElementById("cursormarker");
|
2016-11-30 22:56:01 +11:00
|
|
|
|
if (cursor != null) {
|
2017-01-02 10:51:26 +11:00
|
|
|
|
let range = document.createRange();
|
2016-11-30 22:56:01 +11:00
|
|
|
|
range.selectNodeContents(cursor);
|
|
|
|
|
|
range.collapse(false);
|
|
|
|
|
|
|
2017-01-02 10:51:26 +11:00
|
|
|
|
let selection = window.getSelection();
|
2016-11-30 22:56:01 +11:00
|
|
|
|
selection.removeAllRanges();
|
|
|
|
|
|
selection.addRange(range);
|
|
|
|
|
|
cursor.parentNode.removeChild(cursor);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-04 18:53:28 +11:00
|
|
|
|
// Enable editing and observation machinery only after
|
|
|
|
|
|
// modifying the body above.
|
|
|
|
|
|
this.messageBody.contentEditable = true;
|
2017-01-06 10:48:40 +11:00
|
|
|
|
let config = {
|
|
|
|
|
|
attributes: true,
|
|
|
|
|
|
childList: true,
|
|
|
|
|
|
characterData: true,
|
|
|
|
|
|
subtree: true
|
|
|
|
|
|
};
|
|
|
|
|
|
this.bodyObserver.observe(this.messageBody, config);
|
2017-01-04 18:53:28 +11:00
|
|
|
|
|
2017-01-24 19:30:12 +11:00
|
|
|
|
// Chain up
|
2016-11-30 22:56:01 +11:00
|
|
|
|
PageState.prototype.loaded.apply(this, []);
|
2016-12-02 01:08:07 +11:00
|
|
|
|
},
|
2017-01-04 18:53:28 +11:00
|
|
|
|
undo: function() {
|
|
|
|
|
|
document.execCommand("undo", false, null);
|
|
|
|
|
|
this.checkCommandStack();
|
|
|
|
|
|
},
|
|
|
|
|
|
redo: function() {
|
|
|
|
|
|
document.execCommand("redo", false, null);
|
|
|
|
|
|
this.checkCommandStack();
|
|
|
|
|
|
},
|
2017-01-19 02:26:42 +11:00
|
|
|
|
insertLink: function(href) {
|
|
|
|
|
|
if (!window.getSelection().isCollapsed) {
|
|
|
|
|
|
// There is currently a selection, so assume the user
|
|
|
|
|
|
// knows what they are doing and just linkify it.
|
|
|
|
|
|
document.execCommand("createLink", false, href);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
let selected = SelectionUtil.getCursorElement();
|
|
|
|
|
|
if (selected != null && selected.tagName == "A") {
|
|
|
|
|
|
// The current cursor element is an A, so select it
|
|
|
|
|
|
// since createLink requires a range
|
|
|
|
|
|
let selection = SelectionUtil.save();
|
|
|
|
|
|
SelectionUtil.selectNode(selected);
|
|
|
|
|
|
document.execCommand("createLink", false, href);
|
|
|
|
|
|
SelectionUtil.restore(selection);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
deleteLink: function() {
|
|
|
|
|
|
if (!window.getSelection().isCollapsed) {
|
|
|
|
|
|
// There is currently a selection, so assume the user
|
|
|
|
|
|
// knows what they are doing and just unlink it.
|
|
|
|
|
|
document.execCommand("unlink", false, null);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
let selected = SelectionUtil.getCursorElement();
|
|
|
|
|
|
if (selected != null && selected.tagName == "A") {
|
|
|
|
|
|
// The current cursor element is an A, so select it
|
|
|
|
|
|
// since unlink requires a range
|
|
|
|
|
|
let selection = SelectionUtil.save();
|
|
|
|
|
|
SelectionUtil.selectNode(selected);
|
|
|
|
|
|
document.execCommand("unlink", false, null);
|
|
|
|
|
|
SelectionUtil.restore(selection);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2017-01-26 14:06:36 +11:00
|
|
|
|
indentLine: function() {
|
|
|
|
|
|
document.execCommand("indent", false, null);
|
|
|
|
|
|
let nodeList = document.querySelectorAll(
|
|
|
|
|
|
"blockquote[style=\"margin: 0 0 0 40px; border: none; padding: 0px;\"]"
|
|
|
|
|
|
);
|
|
|
|
|
|
for (let i = 0; i < nodeList.length; ++i) {
|
|
|
|
|
|
let element = nodeList.item(i);
|
|
|
|
|
|
element.removeAttribute("style");
|
|
|
|
|
|
element.setAttribute("type", "cite");
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
Clean up how composer loads content into its web view.
The main gist of this is to ensure that the composer's widgets are
constructed seperately to loading its content, and that we only ever call
ComposerWebView::load_html precisely once per composer instance.
* src/client/composer/composer-widget.vala: Remove referred message,
quote text and draft flag param from constructor signature, move any
calls that loaded data from them to new load method. Don't load
anything into the editor here. Make loading the signature file async,
and call new ComposerWebView::updateSignature method on the editor to
update it.
(ComposerWidget::load): New async message for loading content into the
composer. Move related code from the constructor and GearyController
here, make methods that were previously public for that private
again. Tidy up calls a bit now that we have a single place from which
to do it all, and can understand the process a bit better.
(ComposerWidget::on_editor_key_press_event): Don't reload the editor to
remove the quoted text, call new ComposerWebView::delete_quoted_message
method on it instead.
* src/client/composer/composer-web-view.vala
(ComposerWebView): Add ::delete_quoted_message ::update_signature
methods, thunk to JS.
(ComposerWebView::load_html): Add quote and is_draft parameters,
construct HTML for the composer using apporporate spacing here, instead
of relying on all the disparate parts from doing the right thing.
* src/client/application/geary-controller.vala
(GearyController::create_compose_widget_async): Load composer content
after adding it to the widget hierarchy, set focus only after
everything is set up.
* src/engine/rfc822/rfc822-utils.vala (quote_email_for_reply,
quote_email_for_forward): Don't add extra padding around quoted parts -
let callers manage their own whitespace.
* test/client/components/client-web-view-test-case.vala
(TestCase:load_body_fixture): Make HTML param non-nullable, update
subclasses.
* ui/composer-web-view.js (ComposerPageState): Add ::updateSignature and
::deleteQuotedMessage method stubs.
2017-01-25 11:16:20 +11:00
|
|
|
|
updateSignature: function(signature) {
|
|
|
|
|
|
// XXX need mark the sig somehow so we can find it, select
|
|
|
|
|
|
// it and replace it using execCommand
|
|
|
|
|
|
},
|
|
|
|
|
|
deleteQuotedMessage: function() {
|
|
|
|
|
|
// XXX need mark the quote somehow so we can find it, select
|
|
|
|
|
|
// it and delete it using execCommand
|
|
|
|
|
|
},
|
2017-01-26 13:31:08 +11:00
|
|
|
|
/**
|
|
|
|
|
|
* Determines if subject or body content refers to attachments.
|
|
|
|
|
|
*/
|
|
|
|
|
|
containsAttachmentKeyword: function(keywordSpec, subject) {
|
|
|
|
|
|
// XXX this could also use a structured representation of the
|
|
|
|
|
|
// message body so we don't need to text to check
|
|
|
|
|
|
|
|
|
|
|
|
let ATTACHMENT_KEYWORDS_SUFFIX = "doc|pdf|xls|ppt|rtf|pps";
|
|
|
|
|
|
|
|
|
|
|
|
let completeKeys = new Set(keywordSpec.toLocaleLowerCase().split("|"));
|
|
|
|
|
|
let suffixKeys = new Set(ATTACHMENT_KEYWORDS_SUFFIX.split("|"));
|
|
|
|
|
|
|
|
|
|
|
|
// Check the subject line
|
|
|
|
|
|
if (ComposerPageState.containsKeywords(subject, completeKeys, suffixKeys)) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check interesting body text
|
|
|
|
|
|
let breakingElements = new Set([
|
|
|
|
|
|
"BR", "P", "DIV", "BLOCKQUOTE", "TABLE", "OL", "UL", "HR"
|
|
|
|
|
|
]);
|
|
|
|
|
|
let content = this.messageBody.firstChild;
|
|
|
|
|
|
let found = false;
|
|
|
|
|
|
let done = false;
|
|
|
|
|
|
let textContent = [];
|
|
|
|
|
|
while (content != null && !done) {
|
|
|
|
|
|
if (content.nodeType == Node.TEXT_NODE) {
|
|
|
|
|
|
textContent.push(content.textContent);
|
|
|
|
|
|
} else if (content.nodeType == Node.ELEMENT_NODE) {
|
|
|
|
|
|
let isBreaking = breakingElements.has(content.nodeName);
|
|
|
|
|
|
if (isBreaking) {
|
|
|
|
|
|
textContent.push("\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Always exclude quoted text
|
|
|
|
|
|
if (content.nodeName != "BLOCKQUOTE") {
|
|
|
|
|
|
textContent.push(content.innerText);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (isBreaking || content.nextSibling == null) {
|
|
|
|
|
|
for (let line of textContent.join("").split("\n")) {
|
|
|
|
|
|
// Ignore everything after a sig or a
|
|
|
|
|
|
// forwarded message.
|
|
|
|
|
|
// XXX This breaks if the user types this at
|
|
|
|
|
|
// the start of a line, also, WK's innerText
|
|
|
|
|
|
// impl strips trailing whitespace, so can't
|
|
|
|
|
|
// test for 'line == "-- "' :(
|
|
|
|
|
|
if (line.startsWith("--")) {
|
|
|
|
|
|
done = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
line = line.trim();
|
|
|
|
|
|
if (line != "") {
|
|
|
|
|
|
if (ComposerPageState.containsKeywords(line, completeKeys, suffixKeys)) {
|
|
|
|
|
|
found = true;
|
|
|
|
|
|
done = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
textContent = [];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
content = content.nextSibling;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return found;
|
|
|
|
|
|
},
|
2017-01-05 13:12:14 +11:00
|
|
|
|
tabOut: function() {
|
|
|
|
|
|
document.execCommand(
|
|
|
|
|
|
"inserthtml", false, "<span style='white-space: pre-wrap'>\t</span>"
|
|
|
|
|
|
);
|
|
|
|
|
|
},
|
|
|
|
|
|
tabIn: function() {
|
|
|
|
|
|
// If there is no selection and the character before the
|
2017-01-19 01:48:03 +11:00
|
|
|
|
// text cursor is tab, delete it.
|
2017-01-05 13:12:14 +11:00
|
|
|
|
let selection = window.getSelection();
|
|
|
|
|
|
if (selection.isCollapsed) {
|
|
|
|
|
|
selection.modify("extend", "backward", "character");
|
|
|
|
|
|
if (selection.getRangeAt(0).toString() == "\t") {
|
|
|
|
|
|
document.execCommand("delete", false, null);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
selection.collapseToEnd();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2017-01-26 16:31:03 +11:00
|
|
|
|
linkifyContent: function() {
|
|
|
|
|
|
ComposerPageState.linkify(this.messageBody);
|
|
|
|
|
|
},
|
2016-12-02 01:08:07 +11:00
|
|
|
|
getHtml: function() {
|
2017-01-04 18:53:28 +11:00
|
|
|
|
return this.messageBody.innerHTML;
|
2016-12-02 01:08:07 +11:00
|
|
|
|
},
|
|
|
|
|
|
getText: function() {
|
2017-01-04 18:53:28 +11:00
|
|
|
|
return ComposerPageState.htmlToQuotedText(this.messageBody);
|
2016-12-02 19:49:35 +11:00
|
|
|
|
},
|
|
|
|
|
|
setRichText: function(enabled) {
|
|
|
|
|
|
if (enabled) {
|
|
|
|
|
|
document.body.classList.remove("plain");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
document.body.classList.add("plain");
|
|
|
|
|
|
}
|
2016-12-03 11:20:00 +11:00
|
|
|
|
},
|
2017-01-04 18:53:28 +11:00
|
|
|
|
checkCommandStack: function() {
|
|
|
|
|
|
let canUndo = document.queryCommandEnabled("undo");
|
|
|
|
|
|
let canRedo = document.queryCommandEnabled("redo");
|
|
|
|
|
|
|
|
|
|
|
|
if (canUndo != this.undoEnabled || canRedo != this.redoEnabled) {
|
|
|
|
|
|
this.undoEnabled = canUndo;
|
|
|
|
|
|
this.redoEnabled = canRedo;
|
|
|
|
|
|
window.webkit.messageHandlers.commandStackChanged.postMessage(
|
|
|
|
|
|
this.undoEnabled + "," + this.redoEnabled
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
2017-01-06 10:48:40 +11:00
|
|
|
|
documentModified: function(element) {
|
|
|
|
|
|
window.webkit.messageHandlers.documentModified.postMessage(null);
|
|
|
|
|
|
},
|
2017-01-04 20:11:01 +11:00
|
|
|
|
selectionChanged: function() {
|
|
|
|
|
|
PageState.prototype.selectionChanged.apply(this, []);
|
|
|
|
|
|
|
2017-01-19 01:48:03 +11:00
|
|
|
|
let cursor = SelectionUtil.getCursorElement();
|
|
|
|
|
|
if (cursor != null) {
|
|
|
|
|
|
let newContext = new EditContext(cursor);
|
|
|
|
|
|
if (!newContext.equals(this.cursorContext)) {
|
|
|
|
|
|
this.cursorContext = newContext;
|
|
|
|
|
|
window.webkit.messageHandlers.cursorContextChanged.postMessage(
|
|
|
|
|
|
newContext.encode()
|
2017-01-04 20:11:01 +11:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-11-30 22:56:01 +11:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2017-01-26 13:31:08 +11:00
|
|
|
|
/**
|
|
|
|
|
|
* Determines if any keywords are present in a string.
|
|
|
|
|
|
*/
|
|
|
|
|
|
ComposerPageState.containsKeywords = function(line, completeKeys, suffixKeys) {
|
|
|
|
|
|
let tokens = new Set(
|
|
|
|
|
|
line.toLocaleLowerCase().split(ComposerPageState.KEYWORD_SPLIT_REGEX)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
for (let key of completeKeys) {
|
|
|
|
|
|
if (tokens.has(key)) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let urlRegex = ComposerPageState.URL_REGEX;
|
|
|
|
|
|
// XXX assuming all suffixes have length = 3 here.
|
|
|
|
|
|
let extLen = 3;
|
|
|
|
|
|
for (let token of tokens) {
|
|
|
|
|
|
let extDelim = token.length - (extLen + 1);
|
|
|
|
|
|
// We do care about "a.pdf", but not ".pdf"
|
|
|
|
|
|
if (token.length >= extLen + 2 && token.charAt(extDelim) == ".") {
|
|
|
|
|
|
let suffix = token.substring(extDelim + 1);
|
|
|
|
|
|
if (suffixKeys.has(suffix)) {
|
|
|
|
|
|
if (token.match(urlRegex) == null) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-12-05 10:07:23 +11:00
|
|
|
|
/**
|
2017-01-02 10:23:35 +11:00
|
|
|
|
* Convert a HTML DOM tree to plain text with delineated quotes.
|
2016-12-05 10:07:23 +11:00
|
|
|
|
*
|
2017-01-02 10:23:35 +11:00
|
|
|
|
* Lines are delinated using LF. Quoted lines are prefixed with
|
|
|
|
|
|
* `ComposerPageState.QUOTE_MARKER`, where the number of markers
|
|
|
|
|
|
* indicates the depth of nesting of the quote.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This will modify/reset the DOM, since it ultimately requires
|
|
|
|
|
|
* stuffing `QUOTE_MARKER` into existing paragraphs and getting it
|
|
|
|
|
|
* back out in a way that preserves the visual presentation.
|
2016-12-05 10:07:23 +11:00
|
|
|
|
*/
|
2017-01-02 10:23:35 +11:00
|
|
|
|
ComposerPageState.htmlToQuotedText = function(root) {
|
|
|
|
|
|
// XXX It would be nice to just clone the root and modify that, or
|
|
|
|
|
|
// see if we can implement this some other way so as to not modify
|
|
|
|
|
|
// the DOM at all, but currently unit test show that the results
|
|
|
|
|
|
// are not the same if we work on a clone, likely because of the
|
|
|
|
|
|
// use of HTMLElement::innerText. Need to look into it more.
|
|
|
|
|
|
|
|
|
|
|
|
let savedDoc = root.innerHTML;
|
|
|
|
|
|
let blockquotes = root.querySelectorAll("blockquote");
|
|
|
|
|
|
let nbq = blockquotes.length;
|
|
|
|
|
|
let bqtexts = new Array(nbq);
|
2016-12-05 10:07:23 +11:00
|
|
|
|
|
|
|
|
|
|
// Get text of blockquotes and pull them out of DOM. They are
|
|
|
|
|
|
// replaced with tokens deliminated with the characters
|
|
|
|
|
|
// QUOTE_START and QUOTE_END (from a unicode private use block).
|
|
|
|
|
|
// We need to get the text while they're still in the DOM to get
|
|
|
|
|
|
// newlines at appropriate places. We go through the list of
|
|
|
|
|
|
// blockquotes from the end so that we get the innermost ones
|
|
|
|
|
|
// first.
|
|
|
|
|
|
for (let i = nbq - 1; i >= 0; i--) {
|
|
|
|
|
|
let bq = blockquotes.item(i);
|
|
|
|
|
|
let text = bq.innerText;
|
|
|
|
|
|
if (text.substr(-1, 1) == "\n") {
|
|
|
|
|
|
text = text.slice(0, -1);
|
|
|
|
|
|
} else {
|
2017-01-01 15:19:58 +11:00
|
|
|
|
console.debug(
|
2016-12-05 10:07:23 +11:00
|
|
|
|
" no newline at end of quote: " +
|
|
|
|
|
|
text.length > 0
|
|
|
|
|
|
? "0x" + text.codePointAt(text.length - 1).toString(16)
|
|
|
|
|
|
: "empty line"
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
bqtexts[i] = text;
|
|
|
|
|
|
|
|
|
|
|
|
bq.innerText = (
|
|
|
|
|
|
ComposerPageState.QUOTE_START
|
|
|
|
|
|
+ i.toString()
|
|
|
|
|
|
+ ComposerPageState.QUOTE_END
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-02 10:23:35 +11:00
|
|
|
|
// Reassemble plain text out of parts, and replace non-breaking
|
|
|
|
|
|
// space with regular space.
|
2017-01-04 01:15:08 +11:00
|
|
|
|
let text = ComposerPageState.resolveNesting(root.innerText, bqtexts);
|
2016-12-05 10:07:23 +11:00
|
|
|
|
|
2017-01-02 10:23:35 +11:00
|
|
|
|
// Reassemble DOM now we have the plain text
|
2016-12-05 10:07:23 +11:00
|
|
|
|
root.innerHTML = savedDoc;
|
|
|
|
|
|
|
2017-01-02 10:23:35 +11:00
|
|
|
|
return ComposerPageState.replaceNonBreakingSpace(text);
|
2016-12-05 10:07:23 +11:00
|
|
|
|
};
|
|
|
|
|
|
|
2017-01-26 16:31:03 +11:00
|
|
|
|
// Linkifies "plain text" link
|
|
|
|
|
|
ComposerPageState.linkify = function(node) {
|
|
|
|
|
|
if (node.nodeType == Node.TEXT_NODE) {
|
|
|
|
|
|
// Examine text node for something that looks like a URL
|
|
|
|
|
|
let input = node.nodeValue;
|
|
|
|
|
|
if (input != null) {
|
|
|
|
|
|
let output = input.replace(ComposerPageState.URL_REGEX, function(url) {
|
|
|
|
|
|
if (url.match(ComposerPageState.PROTOCOL_REGEX) != null) {
|
|
|
|
|
|
url = "\x01" + url + "\x01";
|
|
|
|
|
|
}
|
|
|
|
|
|
return url;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (input != output) {
|
|
|
|
|
|
// We got one! Now split the text and swap in a new anchor.
|
|
|
|
|
|
let parent = node.parentNode;
|
|
|
|
|
|
let sibling = node.nextSibling;
|
|
|
|
|
|
for (let part of output.split("\x01")) {
|
|
|
|
|
|
let newNode = null;
|
|
|
|
|
|
if (part.match(ComposerPageState.URL_REGEX) != null) {
|
|
|
|
|
|
newNode = document.createElement("A");
|
|
|
|
|
|
newNode.href = part;
|
|
|
|
|
|
newNode.innerText = part;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
newNode = document.createTextNode(part);
|
|
|
|
|
|
}
|
|
|
|
|
|
parent.insertBefore(newNode, sibling);
|
|
|
|
|
|
}
|
|
|
|
|
|
parent.removeChild(node);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Recurse
|
|
|
|
|
|
let child = node.firstChild;
|
|
|
|
|
|
while (child != null) {
|
|
|
|
|
|
// Save the child and get its next sibling early since if
|
|
|
|
|
|
// it does actually contain a URL, it will be removed from
|
|
|
|
|
|
// the tree
|
|
|
|
|
|
let target = child;
|
|
|
|
|
|
child = child.nextSibling;
|
|
|
|
|
|
// Don't attempt to linkify existing links
|
|
|
|
|
|
if (target.nodeName != "A") {
|
|
|
|
|
|
ComposerPageState.linkify(target);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-12-05 10:07:23 +11:00
|
|
|
|
ComposerPageState.resolveNesting = function(text, values) {
|
|
|
|
|
|
let tokenregex = new RegExp(
|
|
|
|
|
|
"(.?)" +
|
|
|
|
|
|
ComposerPageState.QUOTE_START +
|
|
|
|
|
|
"([0-9]*)" +
|
|
|
|
|
|
ComposerPageState.QUOTE_END +
|
2017-01-01 12:37:08 +11:00
|
|
|
|
"(?=(.?))", "g"
|
2016-12-05 10:07:23 +11:00
|
|
|
|
);
|
|
|
|
|
|
return text.replace(tokenregex, function(match, p1, p2, p3, offset, str) {
|
|
|
|
|
|
let key = new Number(p2);
|
2017-01-01 15:19:58 +11:00
|
|
|
|
let prevChars = p1;
|
|
|
|
|
|
let nextChars = p3;
|
2016-12-05 10:07:23 +11:00
|
|
|
|
let insertNext = "";
|
|
|
|
|
|
// Make sure there's a newline before and after the quote.
|
2017-01-01 15:19:58 +11:00
|
|
|
|
if (prevChars != "" && prevChars != "\n")
|
|
|
|
|
|
prevChars = prevChars + "\n";
|
|
|
|
|
|
if (nextChars != "" && nextChars != "\n")
|
2016-12-05 10:07:23 +11:00
|
|
|
|
insertNext = "\n";
|
|
|
|
|
|
|
|
|
|
|
|
let value = "";
|
|
|
|
|
|
if (key >= 0 && key < values.length) {
|
|
|
|
|
|
let nested = ComposerPageState.resolveNesting(values[key], values);
|
2017-01-01 15:19:58 +11:00
|
|
|
|
value = prevChars + ComposerPageState.quoteLines(nested) + insertNext;
|
2016-12-05 10:07:23 +11:00
|
|
|
|
} else {
|
2017-01-01 15:19:58 +11:00
|
|
|
|
console.error("Regex error in denesting blockquotes: Invalid key");
|
2016-12-05 10:07:23 +11:00
|
|
|
|
}
|
|
|
|
|
|
return value;
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2017-01-02 10:23:35 +11:00
|
|
|
|
/**
|
|
|
|
|
|
* Prefixes each NL-delineated line with `ComposerPageState.QUOTE_MARKER`.
|
|
|
|
|
|
*/
|
2016-12-05 10:07:23 +11:00
|
|
|
|
ComposerPageState.quoteLines = function(text) {
|
|
|
|
|
|
let lines = text.split("\n");
|
|
|
|
|
|
for (let i = 0; i < lines.length; i++)
|
|
|
|
|
|
lines[i] = ComposerPageState.QUOTE_MARKER + lines[i];
|
|
|
|
|
|
return lines.join("\n");
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2017-01-02 10:23:35 +11:00
|
|
|
|
/**
|
|
|
|
|
|
* Converts all non-breaking space chars to plain spaces.
|
|
|
|
|
|
*/
|
|
|
|
|
|
ComposerPageState.replaceNonBreakingSpace = function(text) {
|
|
|
|
|
|
// XXX this is a separate function for unit testing - since when
|
|
|
|
|
|
// running as a unit test, HTMLElement.innerText appears to not
|
|
|
|
|
|
// convert   into U+00A0.
|
|
|
|
|
|
return text.replace(new RegExp(" ", "g"), " ");
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-12-05 10:07:23 +11:00
|
|
|
|
|
2017-01-19 01:48:03 +11:00
|
|
|
|
/**
|
|
|
|
|
|
* Encapsulates editing-related state for a specific DOM node.
|
|
|
|
|
|
*
|
|
|
|
|
|
* This must be kept in sync with the vala object of the same name.
|
|
|
|
|
|
*/
|
|
|
|
|
|
let EditContext = function() {
|
|
|
|
|
|
this.init.apply(this, arguments);
|
|
|
|
|
|
};
|
|
|
|
|
|
EditContext.LINK_MASK = 1 << 0;
|
|
|
|
|
|
|
|
|
|
|
|
EditContext.prototype = {
|
|
|
|
|
|
init: function(node) {
|
Replace composer link dialog with a popover.
* src/client/composer/composer-link-popover.vala: New GtkPopover subclass
for creating/editing links.
* src/client/composer/composer-web-view.vala (EditContext): Add is_link
and link_uri properties, decode them from the message string, add
decoding tests.
(ComposerWebView): Add some as-yet un-implemented methods for
inserting/deleting links.
* src/client/composer/composer-widget.vala (ComposerWidget): Add
cursor_url for storing current text cursor link, update it from the
cursor_context_changed signal param, rename hover_url to pointer_url to
match. Add link_activated signal to let user's open links they are
adding, hook that up in the controller. Rename
::update_selection_actions to ::update_cursor_actions, since that's a
little more apt now, also enable insert link action if there is a
cursor_url set as well as a selection. Remove ::link_dialog, replace
with ::new_link_popover, hook up the new popover's signals there as
appropriate.
(ComposerWidget::on_insert_link): Create and show a lin popover instead
of a dialog.
* ui/composer-web-view.js: Take note of whther the context node is a link
and if so, also it's href. Include both when serialsing for the
cursorContextChanged message. Add serialisation tests.
* ui/composer-link-popover.ui: New UI for link popover.
2017-01-19 02:23:57 +11:00
|
|
|
|
this.context = 0;
|
|
|
|
|
|
this.linkUrl = "";
|
|
|
|
|
|
|
|
|
|
|
|
if (node.nodeName == "A") {
|
|
|
|
|
|
this.context |= EditContext.LINK_MASK;
|
|
|
|
|
|
this.linkUrl = node.href;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-19 01:48:03 +11:00
|
|
|
|
let styles = window.getComputedStyle(node);
|
|
|
|
|
|
let fontFamily = styles.getPropertyValue("font-family");
|
|
|
|
|
|
if (fontFamily.charAt() == "'") {
|
|
|
|
|
|
fontFamily = fontFamily.substr(1, fontFamily.length - 2);
|
|
|
|
|
|
}
|
|
|
|
|
|
this.fontFamily = fontFamily;
|
|
|
|
|
|
this.fontSize = styles.getPropertyValue("font-size").replace("px", "");
|
|
|
|
|
|
},
|
|
|
|
|
|
equals: function(other) {
|
|
|
|
|
|
return other != null
|
Replace composer link dialog with a popover.
* src/client/composer/composer-link-popover.vala: New GtkPopover subclass
for creating/editing links.
* src/client/composer/composer-web-view.vala (EditContext): Add is_link
and link_uri properties, decode them from the message string, add
decoding tests.
(ComposerWebView): Add some as-yet un-implemented methods for
inserting/deleting links.
* src/client/composer/composer-widget.vala (ComposerWidget): Add
cursor_url for storing current text cursor link, update it from the
cursor_context_changed signal param, rename hover_url to pointer_url to
match. Add link_activated signal to let user's open links they are
adding, hook that up in the controller. Rename
::update_selection_actions to ::update_cursor_actions, since that's a
little more apt now, also enable insert link action if there is a
cursor_url set as well as a selection. Remove ::link_dialog, replace
with ::new_link_popover, hook up the new popover's signals there as
appropriate.
(ComposerWidget::on_insert_link): Create and show a lin popover instead
of a dialog.
* ui/composer-web-view.js: Take note of whther the context node is a link
and if so, also it's href. Include both when serialsing for the
cursorContextChanged message. Add serialisation tests.
* ui/composer-link-popover.ui: New UI for link popover.
2017-01-19 02:23:57 +11:00
|
|
|
|
&& this.context == other.context
|
|
|
|
|
|
&& this.linkUrl == other.linkUrl
|
2017-01-19 01:48:03 +11:00
|
|
|
|
&& this.fontFamily == other.fontFamily
|
|
|
|
|
|
&& this.fontSize == other.fontSize;
|
|
|
|
|
|
},
|
|
|
|
|
|
encode: function() {
|
|
|
|
|
|
return [
|
Replace composer link dialog with a popover.
* src/client/composer/composer-link-popover.vala: New GtkPopover subclass
for creating/editing links.
* src/client/composer/composer-web-view.vala (EditContext): Add is_link
and link_uri properties, decode them from the message string, add
decoding tests.
(ComposerWebView): Add some as-yet un-implemented methods for
inserting/deleting links.
* src/client/composer/composer-widget.vala (ComposerWidget): Add
cursor_url for storing current text cursor link, update it from the
cursor_context_changed signal param, rename hover_url to pointer_url to
match. Add link_activated signal to let user's open links they are
adding, hook that up in the controller. Rename
::update_selection_actions to ::update_cursor_actions, since that's a
little more apt now, also enable insert link action if there is a
cursor_url set as well as a selection. Remove ::link_dialog, replace
with ::new_link_popover, hook up the new popover's signals there as
appropriate.
(ComposerWidget::on_insert_link): Create and show a lin popover instead
of a dialog.
* ui/composer-web-view.js: Take note of whther the context node is a link
and if so, also it's href. Include both when serialsing for the
cursorContextChanged message. Add serialisation tests.
* ui/composer-link-popover.ui: New UI for link popover.
2017-01-19 02:23:57 +11:00
|
|
|
|
this.context.toString(16),
|
|
|
|
|
|
this.linkUrl,
|
2017-01-19 01:48:03 +11:00
|
|
|
|
this.fontFamily,
|
|
|
|
|
|
this.fontSize
|
|
|
|
|
|
].join(",");
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Utility methods for managing the DOM Selection.
|
|
|
|
|
|
*/
|
|
|
|
|
|
let SelectionUtil = {
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Returns the element immediately under the text cursor.
|
|
|
|
|
|
*
|
|
|
|
|
|
* If there is a non-empty selection, the element at the end of the
|
|
|
|
|
|
* selection is returned.
|
|
|
|
|
|
*/
|
|
|
|
|
|
getCursorElement: function() {
|
|
|
|
|
|
let selection = window.getSelection();
|
|
|
|
|
|
let node = selection.focusNode;
|
2017-01-26 13:31:08 +11:00
|
|
|
|
if (node != null && node.nodeType != Node.ELEMENT_NODE) {
|
2017-01-19 01:48:03 +11:00
|
|
|
|
node = node.parentNode;
|
|
|
|
|
|
}
|
|
|
|
|
|
return node;
|
2017-01-19 02:26:42 +11:00
|
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Modifies the selection so that it contains the given target node.
|
|
|
|
|
|
*/
|
|
|
|
|
|
selectNode: function(target) {
|
|
|
|
|
|
let newRange = new Range();
|
|
|
|
|
|
newRange.selectNode(target);
|
|
|
|
|
|
|
|
|
|
|
|
let selection = window.getSelection();
|
|
|
|
|
|
selection.removeAllRanges();
|
|
|
|
|
|
selection.addRange(newRange);
|
|
|
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Saves the current selection so it can be restored with `restore`.
|
|
|
|
|
|
*/
|
|
|
|
|
|
save: function() {
|
|
|
|
|
|
let selection = window.getSelection();
|
|
|
|
|
|
var ranges = [];
|
|
|
|
|
|
let len = selection.rangeCount;
|
|
|
|
|
|
for (let i = 0; i < len; ++i) {
|
|
|
|
|
|
ranges.push(selection.getRangeAt(i));
|
|
|
|
|
|
}
|
|
|
|
|
|
return ranges;
|
|
|
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Restores the selection saved with `save`.
|
|
|
|
|
|
*/
|
|
|
|
|
|
restore: function(saved) {
|
|
|
|
|
|
let selection = window.getSelection();
|
|
|
|
|
|
selection.removeAllRanges();
|
|
|
|
|
|
for (let i = 0; i < saved.length; i++) {
|
|
|
|
|
|
selection.addRange(saved[i]);
|
|
|
|
|
|
}
|
2017-01-19 01:48:03 +11:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-11-30 22:56:01 +11:00
|
|
|
|
var geary = new ComposerPageState();
|