-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
51 lines (40 loc) · 1.28 KB
/
plugin.js
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
(function() {
'use strict';
var REG_HREF = /^http(s?):\/\//;
CKEDITOR.plugins.add('openlink', {
modes: { 'wysiwyg': 1 },
init: function(editor) {
editor.on('contentDom', this.onContentDom);
},
onContentDom: function() {
this.editable().on('click', this.plugins.openlink.onClick, this, null, 0);
},
onClick: function(event) {
var selection = this.getSelection();
if (!selection) {
return;
}
var range = selection.getRanges()[ 0 ];
if (!range ||
!range.collapsed ||
range.checkEndOfBlock() ||
range.checkStartOfBlock()) {
return;
}
var nativeEvent = event.data.$;
var path = new CKEDITOR.dom.elementPath(new CKEDITOR.dom.element(nativeEvent.target), range.root);
var link = path.contains('a', true);
if (!link) {
return;
}
var href = link.getAttribute('href');
if (!href || !REG_HREF.test(href)) {
return;
}
event.stop();
setTimeout(function() {
window.open(href, '_blank');
}, 0);
}
});
}());