plugin.js
5.33 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
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
( function() {
'use strict';
var doubleQuoteRegex = /"/g;
CKEDITOR.plugins.add( 'autolink', {
requires: 'clipboard,textmatch',
init: function( editor ) {
var urlTemplate = new CKEDITOR.template( '<a href="{link}" target="_blank">{text}</a>' ),
emailTemplate = new CKEDITOR.template( '<a href="mailto:{link}" target="_blank">{text}</a>' );
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';
}
} );
// IE has its own link completion and we don't want to interfere with it.
if ( CKEDITOR.env.ie && !CKEDITOR.env.edge ) {
return;
}
editor.on( 'contentDom', function() {
var commitKeystrokes = editor.config.autolink_commitKeystrokes || CKEDITOR.config.autolink_commitKeystrokes;
editor.on( 'key', function( evt ) {
if ( CKEDITOR.tools.indexOf( commitKeystrokes, evt.data.keyCode ) == -1 ) {
return;
}
// fix: 修复getRanges取不到的报错
if (editor.getSelection() && editor.getSelection().getRanges() && editor.getSelection().getRanges()[ 0 ]) {
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 tryToEncodeLink( data ) {
// If enabled use link plugin to encode email link.
if ( editor.plugins.link ) {
var link = CKEDITOR.dom.element.createFromHtml( data ),
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();
}
return data;
}
function getHtmlToInsert( text ) {
var opts = {
text: text,
link: text.replace( doubleQuoteRegex, '%22' )
},
template = opts.link.match( CKEDITOR.config.autolink_urlRegex ) ?
urlTemplate.output( opts )
: emailTemplate.output( opts );
return tryToEncodeLink( template );
}
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( CKEDITOR.config.autolink_urlRegex ) ||
query.match( CKEDITOR.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)).
} )();