-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathplugin.js
169 lines (135 loc) · 4.82 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
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
/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* CKEditor 4 LTS ("Long Term Support") is available under the terms of the Extended Support Model.
*/
( function() {
'use strict';
CKEDITOR.plugins.add( 'autolink', {
requires: 'clipboard,textmatch,link',
isSupportedEnvironment: function() {
return !CKEDITOR.env.ie || CKEDITOR.env.edge;
},
init: function( editor ) {
// (#2208)
if ( !this.isSupportedEnvironment() ) {
return;
}
editor.on( 'paste', function( evt ) {
if ( evt.data.dataTransfer.getTransferType( editor ) == CKEDITOR.DATA_TRANSFER_INTERNAL ) {
return;
}
var data = evt.data.dataValue;
// If we found "<" it means that most likely there's some tag and we don't want to touch it.
if ( data.indexOf( '<' ) > -1 ) {
return;
}
if ( matchLink( data ) ) {
evt.data.dataValue = getHtmlToInsert( data );
evt.data.type = 'html';
}
} );
// (#3156)
editor.on( 'key', function( evt ) {
if ( editor.mode !== 'wysiwyg' || CKEDITOR.tools.indexOf( editor.config.autolink_commitKeystrokes, evt.data.keyCode ) == -1 ) {
return;
}
var matched = CKEDITOR.plugins.textMatch.match( editor.getSelection().getRanges()[ 0 ], matchCallback );
if ( matched ) {
insertLink( matched );
}
} );
function insertLink( match ) {
var selection = editor.getSelection();
// We don't want to insert a link if selection is already inside another link.
if ( selection.getRanges()[ 0 ].startContainer.getAscendant( 'a', true ) ) {
return;
}
selection.selectRanges( [ match.range ] );
editor.insertHtml( getHtmlToInsert( match.text ), 'text' );
if ( !CKEDITOR.env.webkit ) {
// Make sure that link cannot be modified right after insertion
// by moving selection at the end of inserted node.
var insertionRange = selection.getRanges()[ 0 ],
newRange = editor.createRange();
newRange.setStartAfter( insertionRange.startContainer );
selection.selectRanges( [ newRange ] );
}
}
function getHtmlToInsert( text ) {
// URL will be encoded later on with link.setAttribute method. Avoid
// double encoding of special characters (#4858).
text = CKEDITOR.tools.htmlDecodeAttr( text );
var link = new CKEDITOR.dom.element( 'a' ),
value = text.replace( /"/g, '%22' );
value = value.match( editor.config.autolink_urlRegex ) ? value : 'mailto:' + value;
link.setText( text );
link.setAttribute( 'href', value );
// (#1824)
var linkData = CKEDITOR.plugins.link.parseLinkAttributes( editor, link ),
attributes = CKEDITOR.plugins.link.getLinkAttributes( editor, linkData );
if ( !CKEDITOR.tools.isEmpty( attributes.set ) ) {
link.setAttributes( attributes.set );
}
if ( attributes.removed.length ) {
link.removeAttributes( attributes.removed );
}
link.removeAttribute( 'data-cke-saved-href' );
return link.getOuterHtml();
}
function matchCallback( text, offset ) {
var parts = text.slice( 0, offset )
.split( /\s+/ ),
query = parts[ parts.length - 1 ];
if ( !query ) {
return null;
}
if ( !matchLink( query ) ) {
return null;
}
return { start: text.lastIndexOf( query ), end: offset };
}
function matchLink( query ) {
return query.match( editor.config.autolink_urlRegex ) ||
query.match( editor.config.autolink_emailRegex );
}
}
} );
/**
* The [Auto Link](https://ckeditor.com/cke4/addon/autolink) plugin keystrokes used to finish link completion.
*
* ```javascript
* // Default configuration (13 = Enter, 32 = space).
* config.autolink_commitKeystrokes = [ 9, 13 ];
* ```
*
* Commit keystrokes can be also disabled by setting it to an empty array.
*
* ```javascript
* // Disable autolink commit keystrokes.
* config.autolink_commitKeystrokes = [];
* ```
*
* @since 4.11.0
* @cfg {Number/Number[]} [autolink_commitKeystrokes=[ 13, 32 ]]
* @member CKEDITOR.config
*/
CKEDITOR.config.autolink_commitKeystrokes = [ 13, 32 ];
/**
* Regex used by the [Auto Link](https://ckeditor.com/cke4/addon/autolink) plugin to match URL adresses.
*
* @cfg {RegExp} [autolink_urlRegex]
* @since 4.11.0
* @member CKEDITOR.config
*/
CKEDITOR.config.autolink_urlRegex = /^(https?|ftp):\/\/(-\.)?([^\s\/?\.#]\.?)+(\/[^\s]*)?[^\s\.,]$/i;
// Regex by Imme Emosol.
/**
* Regex used by the [Auto Link](https://ckeditor.com/cke4/addon/autolink) plugin to match email adresses.
*
* @cfg {RegExp} [autolink_emailRegex]
* @since 4.11.0
* @member CKEDITOR.config
*/
CKEDITOR.config.autolink_emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
// Regex by (https://html.spec.whatwg.org/#e-mail-state-(type=email)).
} )();