forked from liferay/alloy-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautolink.js
More file actions
337 lines (271 loc) · 12.3 KB
/
autolink.js
File metadata and controls
337 lines (271 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
(function() {
'use strict';
if (CKEDITOR.plugins.get('ae_autolink')) {
return;
}
// Disables the auto URL detection feature in IE, their lacks functionality:
// They convert the links only on space. We do on space, comma, semicolon and Enter.
if (/MSIE ([^;]*)|Trident.*; rv:([0-9.]+)/.test(navigator.userAgent)) {
document.execCommand('AutoUrlDetect', false, false);
}
var KEY_BACK = 8;
var KEY_COMMA = 188;
var KEY_ENTER = 13;
var KEY_SEMICOLON = 186;
var KEY_SPACE = 32;
var DELIMITERS = [KEY_COMMA, KEY_ENTER, KEY_SEMICOLON, KEY_SPACE];
var REGEX_LAST_WORD = /[^\s]+/gim;
var REGEX_URL = /((([A - Za - z]{ 3, 9}: (?: \/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(https?\:\/\/|www.|[-;:&=.\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))((.*):(\d*)\/?(.*))?)/i;
var REGEX_EMAIL = /[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/i;
/**
* CKEditor plugin which automatically generates links when user types text which looks like URL.
*
* @class CKEDITOR.plugins.ae_autolink
* @constructor
*/
CKEDITOR.plugins.add(
'ae_autolink', {
/**
* Initialization of the plugin, part of CKEditor plugin lifecycle.
* The function registers the `keyup` event on the editing area.
*
* @instance
* @memberof CKEDITOR.plugins.ae_autolink
* @method init
* @param {Object} editor The current editor instance
*/
init: function(editor) {
editor.once('contentDom', function() {
var editable = editor.editable();
editable.attachListener(editable, 'keyup', this._onKeyUp, this, {
editor: editor
});
}.bind(this));
editor.on('paste', function (event) {
if (event.data.method === 'paste') {
if (event.data.dataValue.indexOf('<') > -1 || event.data.dataValue.indexOf('<') > -1) {
return;
}
var instance = this;
event.data.dataValue = event.data.dataValue.replace(RegExp(REGEX_URL, 'gim'), function (url) {
if (instance._isValidURL(url)) {
if (instance._isValidEmail(url)) {
return '<a href=\"mailto:' + url + '\">' + url + '</a>';
} else {
return '<a href=\"' + url + '\">' + url + '</a>';
}
}
});
}
}.bind(this));
},
/**
* Retrieves the last word introduced by the user. Reads from the current
* caret position backwards until it finds the first white space.
*
* @instance
* @memberof CKEDITOR.plugins.ae_autolink
* @method _getLastWord
* @protected
* @return {String} The last word introduced by user
*/
_getLastWord: function(editor) {
var range = editor.getSelection().getRanges()[0];
var offset = range.startOffset;
var previousText = '';
// The user pressed Enter, so we have to look on the previous node
if (this._currentKeyCode === KEY_ENTER) {
var previousNode = range.startContainer.getPrevious();
var lastChild;
if (previousNode) {
// If previous node is a SPACE, (it does not have 'getLast' method),
// ignore it and find the previous text node
while (!previousNode.getLast) {
previousNode = previousNode.getPrevious();
}
lastChild = previousNode.getLast();
// Depending on the browser, the last child node may be a <BR>
// (which does not have 'getText' method),
// so ignore it and find the previous text node
while (lastChild && !lastChild.getText()) {
lastChild = lastChild.getPrevious();
}
}
// Check if the lastChild is already a link
if (!(lastChild && lastChild.$.href)) {
this._startContainer = lastChild;
previousText = lastChild ? lastChild.getText() : '';
this._offset = previousText.length;
}
} else {
this._startContainer = range.startContainer;
// Last character is the delimiter, ignore it
previousText = this._startContainer.getText().substring(0, offset - 1);
this._offset = offset - 1;
}
var lastWord = '';
var match = previousText.match(REGEX_LAST_WORD);
if (match) {
lastWord = match.pop();
}
return lastWord;
},
/**
* Checks if the given link is a valid Email.
*
* @instance
* @memberof CKEDITOR.plugins.ae_autolink
* @method isValidEmail
* @param {String} link The email we want to know if it is a valid Email
* @protected
* @return {Boolean} Returns true if the email is a valid Email, false otherwise
*/
_isValidEmail: function(email) {
return REGEX_EMAIL.test(email);
},
/**
* Checks if the given link is a valid URL.
*
* @instance
* @memberof CKEDITOR.plugins.ae_autolink
* @method isValidURL
* @param {String} link The link we want to know if it is a valid URL
* @protected
* @return {Boolean} Returns true if the link is a valid URL, false otherwise
*/
_isValidURL: function(link) {
return REGEX_URL.test(link);
},
/**
* Listens to the `keydown` event and if the keycode is `Backspace`, removes the previously
* created link.
*
* @instance
* @memberof CKEDITOR.plugins.ae_autolink
* @method _onKeyDown
* @param {EventFacade} event EventFacade object
* @protected
*/
_onKeyDown: function(event) {
var nativeEvent = event.data.$;
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(editor);
}
this._ckLink = null;
},
/**
* Listens to the `Enter` and `Space` key events in order to check if the last word
* introduced by the user should be replaced by a link element.
*
* @instance
* @memberof CKEDITOR.plugins.ae_autolink
* @method _onKeyUp
* @param {EventFacade} event EventFacade object
* @protected
*/
_onKeyUp: function(event) {
var nativeEvent = event.data.$;
this._currentKeyCode = nativeEvent.keyCode;
if (DELIMITERS.indexOf(this._currentKeyCode) !== -1) {
var editor = event.listenerData.editor;
var lastWord = this._getLastWord(editor);
if (this._isValidURL(lastWord)) {
this._replaceContentByLink(editor, lastWord);
}
}
},
/**
* Replaces content by a link element.
*
* @fires CKEDITOR.plugins.ae_autolink#autolinkAdd
* @instance
* @memberof CKEDITOR.plugins.ae_autolink
* @method _replaceContentByLink
* @param {String} content The text that has to be replaced by an link element
* @protected
*/
_replaceContentByLink: function(editor, content) {
var range = editor.createRange();
var node = CKEDITOR.dom.element.get(this._startContainer);
var offset = this._offset;
// Select the content, so CKEDITOR.Link can properly replace it
range.setStart(node, offset - content.length);
range.setEnd(node, offset);
range.select();
var ckLink = new CKEDITOR.Link(editor);
ckLink.create(content);
this._ckLink = ckLink;
var linkNode = ckLink.getFromSelection();
editor.fire('autolinkAdd', linkNode);
this._subscribeToKeyEvent(editor);
// Now range is on the link and it is selected. We have to
// return focus to the caret position.
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)
if (this._currentKeyCode === KEY_ENTER) {
var nextEditableNode = range.getNextEditableNode();
range.setStart(nextEditableNode, 0);
range.setEnd(nextEditableNode, 0);
} else {
var nextNode = range.getNextNode();
range.setStart(nextNode, 1);
range.setEnd(nextNode, 1);
}
range.select();
},
/**
* Fired when a URL is detected in text and converted to a link.
*
* @event CKEDITOR.plugins.ae_autolink#autolinkAdd
* @memberof CKEDITOR.plugins.ae_autolink
* @param {CKEDITOR.dom.element} el Node of the created link.
*/
/**
* Removes the created link element, and replaces it by its text.
*
* @instance
* @memberof CKEDITOR.plugins.ae_autolink
* @method _removeLink
* @protected
*/
_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 = editor.createRange();
newRange.setStart(linkNode, 0);
newRange.setEndAfter(linkNode);
newRange.select();
this._ckLink.remove();
// Return focus to the caret position
range.setEnd(range.startContainer, caretOffset);
range.setStart(range.startContainer, caretOffset);
range.select();
},
/**
* Subscribe to a key event of the editable aria.
*
* @instance
* @memberof CKEDITOR.plugins.ae_autolink
* @method _subscribeToKeyEvent
* @protected
*/
_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, {
editor: editor
}, 1);
}
}
);
}());