Skip to content

Commit

Permalink
MDL-49107 atto_link: Insert link even when selection is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
FMCorz committed Feb 25, 2015
1 parent 95751e8 commit 39c6f62
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 47 deletions.
Expand Up @@ -111,7 +111,7 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
_displayDialogue: function() {
// Store the current selection.
this._currentSelection = this.get('host').getSelection();
if (this._currentSelection === false || this._currentSelection.collapsed) {
if (this._currentSelection === false) {
return;
}

Expand Down Expand Up @@ -167,11 +167,11 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
},

/**
* Update the dialogue after an image was selected in the File Picker.
* Update the dialogue after a link was selected in the File Picker.
*
* @method _filepickerCallback
* @param {object} params The parameters provided by the filepicker
* containing information about the image.
* containing information about the link.
* @private
*/
_filepickerCallback: function(params) {
Expand All @@ -180,9 +180,9 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
.hide();

if (params.url !== '') {
this.get('host').setSelection(this._currentSelection);
document.execCommand('unlink', false, null);
document.execCommand('createLink', false, params.url);
// Add the link.
this._setLinkOnSelection(params.url);

// And mark the text area as updated.
this.markUpdated();
}
Expand All @@ -202,8 +202,6 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
anchornodes,
value;

var host = this.get('host');

e.preventDefault();
this.getDialogue({
focusAfterHide: null
Expand All @@ -213,8 +211,6 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit

value = input.get('value');
if (value !== '') {
this.editor.focus();
host.setSelection(this._currentSelection);

// We add a prefix if it is not already prefixed.
value = value.trim();
Expand All @@ -223,11 +219,8 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
value = 'http://' + value;
}

document.execCommand('unlink', false, null);
document.execCommand('createLink', false, value);

// Now set the target.
selectednode = host.getSelectionParentNode();
// Add the link.
selectednode = this._setLinkOnSelection(value);

// Note this is a document fragment and YUI doesn't like them.
if (!selectednode) {
Expand All @@ -248,6 +241,41 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
}
},

/**
* Final step setting the anchor on the selection.
*
* @private
* @method _setLinkOnSelection
* @param {String} url URL the link will point to.
* @return {Node} The added Node.
*/
_setLinkOnSelection: function(url) {
var host = this.get('host'),
link,
selectednode;

this.editor.focus();
host.setSelection(this._currentSelection);

if (this._currentSelection[0].collapsed) {
// Firefox cannot add links when the selection is empty so we will add it manually.
link = Y.Node.create('<a>' + url + '</a>');
link.setAttribute('href', url);

// Add the node and select it to replicate the behaviour of execCommand.
selectednode = host.insertContentAtFocusPoint(link.get('outerHTML'));
host.setSelection(host.getSelectionFromNode(selectednode));
} else {
document.execCommand('unlink', false, null);
document.execCommand('createLink', false, url);

// Now set the target.
selectednode = host.getSelectionParentNode();
}

return selectednode;
},

/**
* Look up and down for the nearest anchor tags that are least partly contained in the selection.
*
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -111,7 +111,7 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
_displayDialogue: function() {
// Store the current selection.
this._currentSelection = this.get('host').getSelection();
if (this._currentSelection === false || this._currentSelection.collapsed) {
if (this._currentSelection === false) {
return;
}

Expand Down Expand Up @@ -167,11 +167,11 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
},

/**
* Update the dialogue after an image was selected in the File Picker.
* Update the dialogue after a link was selected in the File Picker.
*
* @method _filepickerCallback
* @param {object} params The parameters provided by the filepicker
* containing information about the image.
* containing information about the link.
* @private
*/
_filepickerCallback: function(params) {
Expand All @@ -180,9 +180,9 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
.hide();

if (params.url !== '') {
this.get('host').setSelection(this._currentSelection);
document.execCommand('unlink', false, null);
document.execCommand('createLink', false, params.url);
// Add the link.
this._setLinkOnSelection(params.url);

// And mark the text area as updated.
this.markUpdated();
}
Expand All @@ -202,8 +202,6 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
anchornodes,
value;

var host = this.get('host');

e.preventDefault();
this.getDialogue({
focusAfterHide: null
Expand All @@ -213,8 +211,6 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit

value = input.get('value');
if (value !== '') {
this.editor.focus();
host.setSelection(this._currentSelection);

// We add a prefix if it is not already prefixed.
value = value.trim();
Expand All @@ -223,11 +219,8 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
value = 'http://' + value;
}

document.execCommand('unlink', false, null);
document.execCommand('createLink', false, value);

// Now set the target.
selectednode = host.getSelectionParentNode();
// Add the link.
selectednode = this._setLinkOnSelection(value);

// Note this is a document fragment and YUI doesn't like them.
if (!selectednode) {
Expand All @@ -248,6 +241,41 @@ Y.namespace('M.atto_link').Button = Y.Base.create('button', Y.M.editor_atto.Edit
}
},

/**
* Final step setting the anchor on the selection.
*
* @private
* @method _setLinkOnSelection
* @param {String} url URL the link will point to.
* @return {Node} The added Node.
*/
_setLinkOnSelection: function(url) {
var host = this.get('host'),
link,
selectednode;

this.editor.focus();
host.setSelection(this._currentSelection);

if (this._currentSelection[0].collapsed) {
// Firefox cannot add links when the selection is empty so we will add it manually.
link = Y.Node.create('<a>' + url + '</a>');
link.setAttribute('href', url);

// Add the node and select it to replicate the behaviour of execCommand.
selectednode = host.insertContentAtFocusPoint(link.get('outerHTML'));
host.setSelection(host.getSelectionFromNode(selectednode));
} else {
document.execCommand('unlink', false, null);
document.execCommand('createLink', false, url);

// Now set the target.
selectednode = host.getSelectionParentNode();
}

return selectednode;
},

/**
* Look up and down for the nearest anchor tags that are least partly contained in the selection.
*
Expand Down

0 comments on commit 39c6f62

Please sign in to comment.