plugin.js
6.37 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
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @fileOverview This plugin allows to register new paste handlers.
*/
( function() {
var loadedFilters = [],
PasteTools = CKEDITOR.tools.createClass( {
$: function() {
this.handlers = [];
},
proto: {
register: function( definition ) {
if ( typeof definition.priority !== 'number' ) {
definition.priority = 10;
}
this.handlers.push( definition );
},
addPasteListener: function( editor ) {
editor.on( 'paste', function( evt ) {
var handlers = getMatchingHandlers( this.handlers, evt ),
filters,
isLoaded;
if ( handlers.length === 0 ) {
return;
}
filters = getFilters( handlers );
isLoaded = loadFilters( filters, function() {
return editor.fire( 'paste', evt.data );
} );
if ( !isLoaded ) {
return evt.cancel();
}
handlePaste( handlers, evt );
}, this, null, 3 );
}
}
} );
CKEDITOR.plugins.add( 'pastetools', {
requires: 'clipboard',
beforeInit: function( editor ) {
editor.pasteTools = new PasteTools();
editor.pasteTools.addPasteListener( editor );
}
} );
/**
* A set of paste tools helpers.
*
* @member CKEDITOR.plugins
* @since 4.13.0
*/
CKEDITOR.plugins.pastetools = {
/**
* A collection of available filters.
*
* @member CKEDITOR.plugins.pastetools
* @property {Object.<String,Object>}
*/
filters: {},
/**
* Loads external scripts containing filter definitions in given order.
*
* @param {String[]} filters An array of filter URLs.
* @param {Function} callback A callback that will be invoked after loading all scripts.
* @member CKEDITOR.plugins.pastetools
*/
loadFilters: loadFilters,
/**
* Creates a filter based on passed rules.
*
* @param {Object} options
* @param {Function} options.rules A function returning the filter's rules.
* @param {Function}options.additionalTransforms A function transforming HTML before passing it to the filter.
* @returns {Function} A function that wraps filter invocation.
* @member CKEDITOR.plugins.pastetools
*/
createFilter: function( options ) {
var rules = CKEDITOR.tools.array.isArray( options.rules ) ? options.rules : [ options.rules ],
additionalTransforms = options.additionalTransforms;
return function( html, editor ) {
var writer = new CKEDITOR.htmlParser.basicWriter(),
filter = new CKEDITOR.htmlParser.filter(),
fragment;
if ( additionalTransforms ) {
html = additionalTransforms( html, editor );
}
CKEDITOR.tools.array.forEach( rules, function( rule ) {
filter.addRules( rule( html, editor, filter ) );
} );
fragment = CKEDITOR.htmlParser.fragment.fromHtml( html );
filter.applyTo( fragment );
fragment.writeHtml( writer );
return writer.getHtml();
};
},
/**
* Gets clipboard data.
*
* @param {Object} data Paste event `data` property.
* @param {String} type MIME type of the requested data.
* @returns {String/Blob} Raw clipboard data.
* @member CKEDITOR.plugins.pastetools
*/
getClipboardData: function( data, type ) {
var dataTransfer;
if ( !CKEDITOR.plugins.clipboard.isCustomDataTypesSupported && type !== 'text/html' ) {
return null;
}
dataTransfer = data.dataTransfer.getData( type, true );
// Some commands fire paste event without setting dataTransfer property. In such case
// dataValue should be used for retrieving HTML.
if ( !dataTransfer && type === 'text/html' ) {
return data.dataValue;
}
return dataTransfer;
},
/**
* Gets the configuration value.
*
* This function allows to get the configuration value for Paste Tools from
* the legacy Paste from Word configuration.
*
* @param {CKEDITOR.editor} editor The editor instance.
* @param {String} configVariable The configuration variable name.
* @returns {String/Boolean/Number/Object/Array} The configuration variable value.
* @member CKEDITOR.plugins.pastetools
*/
getConfigValue: function( editor, configVariable ) {
if ( !editor || !editor.config ) {
return;
}
var tools = CKEDITOR.tools,
config = editor.config,
configVariables = tools.object.keys( config ),
names = [
'pasteTools_' + configVariable,
'pasteFromWord_' + configVariable,
'pasteFromWord' + tools.capitalize( configVariable, true )
],
found = tools.array.find( names, function( name ) {
return tools.array.indexOf( configVariables, name ) !== -1;
} );
return config[ found ];
}
};
function getMatchingHandlers( handlers, evt ) {
return CKEDITOR.tools.array.filter( handlers, function( handler ) {
return handler.canHandle( evt );
} ).sort( function( handler1, handler2 ) {
if ( handler1.priority === handler2.priority ) {
return 0;
}
return handler1.priority - handler2.priority;
} );
}
function handlePaste( handlers, evt ) {
var handler = handlers.shift();
if ( !handler ) {
return;
}
handler.handle( evt, function() {
handlePaste( handlers, evt );
} );
}
// Join all filters in one big array and then filter out duplicates.
function getFilters( handlers ) {
var filters = CKEDITOR.tools.array.reduce( handlers, function( filters, handler ) {
if ( !CKEDITOR.tools.array.isArray( handler.filters ) ) {
return filters;
}
return filters.concat( handler.filters );
}, [] );
return CKEDITOR.tools.array.filter( filters, function( filter, i ) {
return CKEDITOR.tools.array.indexOf( filters, filter ) === i;
} );
}
function loadFilters( filters, callback ) {
var loaded = 0,
toLoad,
i;
if ( !CKEDITOR.tools.array.isArray( filters ) || filters.length === 0 ) {
return true;
}
toLoad = CKEDITOR.tools.array.filter( filters, function( filter ) {
return CKEDITOR.tools.array.indexOf( loadedFilters, filter ) === -1;
} );
if ( toLoad.length > 0 ) {
for ( i = 0; i < toLoad.length; i++ ) {
( function( current ) {
CKEDITOR.scriptLoader.queue( current, function( isLoaded ) {
if ( isLoaded ) {
loadedFilters.push( current );
}
if ( ++loaded === toLoad.length ) {
callback();
}
} );
}( toLoad[ i ] ) );
}
}
return toLoad.length === 0;
}
CKEDITOR.pasteFilters = {};
} )();