Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get current "editor" from the event. #198

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions src/plugins/autolink.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
editor.once('contentDom', function() {
var editable = editor.editable();

editable.attachListener(editable, 'keyup', this._onKeyUp, this);
editable.attachListener(editable, 'keyup', this._onKeyUp, this, {
editor: editor
});
}.bind(this));

this._editor = editor;
},

/**
Expand All @@ -61,8 +61,8 @@
* @return {String} The last word introduced by user
* @protected
*/
_getLastWord: function() {
var range = this._editor.getSelection().getRanges()[0];
_getLastWord: function(editor) {
var range = editor.getSelection().getRanges()[0];

var offset = range.startOffset;

Expand Down Expand Up @@ -131,15 +131,17 @@
_onKeyDown: function(event) {
var nativeEvent = event.data.$;

var editable = this._editor.editable();
var editor = event.listenerData.editor;

var editable = editor.editable();

editable.removeListener('keydown', this._onKeyDown);

if (nativeEvent.keyCode === KEY_BACK) {
event.cancel();
event.data.preventDefault();

this._removeLink();
this._removeLink(editor);
}

this._ckLink = null;
Expand All @@ -159,10 +161,12 @@
this._currentKeyCode = nativeEvent.keyCode;

if (DELIMITERS.indexOf(this._currentKeyCode) !== -1) {
var lastWord = this._getLastWord();
var editor = event.listenerData.editor;

var lastWord = this._getLastWord(editor);

if (this._isValidURL(lastWord)) {
this._replaceContentByLink(lastWord);
this._replaceContentByLink(editor, lastWord);
}
}
},
Expand All @@ -174,8 +178,8 @@
* @param {String} content The text that has to be replaced by an link element
* @protected
*/
_replaceContentByLink: function(content) {
var range = this._editor.createRange();
_replaceContentByLink: function(editor, content) {
var range = editor.createRange();
var node = CKEDITOR.dom.element.get(this._startContainer);
var offset = this._offset;

Expand All @@ -184,15 +188,15 @@
range.setEnd(node, offset);
range.select();

var ckLink = new CKEDITOR.Link(this._editor);
var ckLink = new CKEDITOR.Link(editor);
ckLink.create(content);
this._ckLink = ckLink;

this._subscribeToKeyEvent();
this._subscribeToKeyEvent(editor);

// Now range is on the link and it is selected. We have to
// return focus to the caret position.
range = this._editor.getSelection().getRanges()[0];
range = editor.getSelection().getRanges()[0];

// If user pressed `Enter`, get the next editable node at position 0,
// otherwise set the cursor at the next character of the link (the white space)
Expand All @@ -217,14 +221,14 @@
* @method _removeLink
* @protected
*/
_removeLink: function() {
var range = this._editor.getSelection().getRanges()[0];
_removeLink: function(editor) {
var range = editor.getSelection().getRanges()[0];
var caretOffset = range.startOffset;

// Select the link, so CKEDITOR.Link can properly remove it
var linkNode = this._startContainer.getNext() || this._startContainer;

var newRange = this._editor.createRange();
var newRange = editor.createRange();
newRange.setStart(linkNode, 0);
newRange.setEndAfter(linkNode);
newRange.select();
Expand All @@ -244,13 +248,15 @@
* @method _subscribeToKeyEvent
* @protected
*/
_subscribeToKeyEvent: function() {
var editable = this._editor.editable();
_subscribeToKeyEvent: function(editor) {
var editable = editor.editable();

// Change the priority of keydown listener - 1 means the highest priority.
// In Chrome on pressing `Enter` the listener is not being invoked.
// See http://dev.ckeditor.com/ticket/11861 for more information.
editable.attachListener(editable, 'keydown', this._onKeyDown, this, {}, 1);
editable.attachListener(editable, 'keydown', this._onKeyDown, this, {
editor: editor
}, 1);
}
}
);
Expand Down