Clean up composer link insertion and deletion

Allow deleting only the selected part of a link, and modify the existing
link instead of inserting a new one if the user didn't select any text.
Also restructure the code to avoid always-true or always-false if
statements.

Fixes #591
This commit is contained in:
Alex Henrie 2019-10-05 10:25:25 -06:00
parent a3c8b02f43
commit 52d010bb22
3 changed files with 30 additions and 22 deletions

View file

@ -163,30 +163,31 @@ ComposerPageState.prototype = {
this.selections.delete(id);
},
insertLink: function(href, selectionId) {
if (!window.getSelection().isCollapsed) {
// There is currently a selection, so assume the user
// knows what they are doing and just linkify it.
SelectionUtil.restore(this.selections.get(selectionId));
if (window.getSelection().isCollapsed) {
// The saved selection was empty, which means that the user is
// modifying an existing link instead of inserting a new one.
let selection = SelectionUtil.save();
let selected = SelectionUtil.getCursorElement();
SelectionUtil.selectNode(selected);
document.execCommand("createLink", false, href);
SelectionUtil.restore(selection);
} else {
SelectionUtil.restore(this.selections.get(selectionId));
document.execCommand("createLink", false, href);
}
},
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 {
deleteLink: function(selectionId) {
SelectionUtil.restore(this.selections.get(selectionId));
if (window.getSelection().isCollapsed) {
// The saved selection was empty, which means that the user is
// deleting the entire existing link.
let selection = SelectionUtil.save();
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);
}
SelectionUtil.selectNode(selected);
document.execCommand("unlink", false, null);
SelectionUtil.restore(selection);
} else {
document.execCommand("unlink", false, null);
}
},
indentLine: function() {