]> git.immae.eu Git - perso/Immae/Projets/packagist/ludivine-ckeditor-component.git/blame - sources/plugins/colorbutton/plugin.js
Update to 4.7.3
[perso/Immae/Projets/packagist/ludivine-ckeditor-component.git] / sources / plugins / colorbutton / plugin.js
CommitLineData
eaa92715
IB
1/**\r
2 * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.\r
3 * For licensing, see LICENSE.md or http://ckeditor.com/license\r
4 */\r
5\r
6/**\r
7 * @fileOverview The "colorbutton" plugin that makes it possible to assign\r
8 * text and background colors to editor contents.\r
9 *\r
10 */\r
11CKEDITOR.plugins.add( 'colorbutton', {\r
12 requires: 'panelbutton,floatpanel',\r
13 // jscs:disable maximumLineLength\r
1794320d 14 lang: 'af,ar,az,bg,bn,bs,ca,cs,cy,da,de,de-ch,el,en,en-au,en-ca,en-gb,eo,es,es-mx,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,oc,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%\r
eaa92715
IB
15 // jscs:enable maximumLineLength\r
16 icons: 'bgcolor,textcolor', // %REMOVE_LINE_CORE%\r
17 hidpi: true, // %REMOVE_LINE_CORE%\r
18 init: function( editor ) {\r
19 var config = editor.config,\r
20 lang = editor.lang.colorbutton;\r
21\r
22 if ( !CKEDITOR.env.hc ) {\r
23 addButton( 'TextColor', 'fore', lang.textColorTitle, 10, {\r
24 contentTransformations: [\r
25 [\r
26 {\r
27 element: 'font',\r
28 check: 'span{color}',\r
29 left: function( element ) {\r
30 return !!element.attributes.color;\r
31 },\r
32 right: function( element ) {\r
33 element.name = 'span';\r
34\r
35 element.attributes.color && ( element.styles.color = element.attributes.color );\r
36 delete element.attributes.color;\r
37 }\r
38 }\r
39 ]\r
40 ]\r
41 } );\r
42\r
43 var bgOptions = {},\r
44 normalizeBackground = editor.config.colorButton_normalizeBackground;\r
45\r
46 if ( normalizeBackground === undefined || normalizeBackground ) {\r
47 // If background contains only color, then we want to convert it into background-color so that it's\r
48 // correctly picked by colorbutton plugin.\r
49 bgOptions.contentTransformations = [\r
50 [\r
51 {\r
52 // Transform span that specify background with color only to background-color.\r
53 element: 'span',\r
54 left: function( element ) {\r
55 var tools = CKEDITOR.tools;\r
56 if ( element.name != 'span' || !element.styles || !element.styles.background ) {\r
57 return false;\r
58 }\r
59\r
60 var background = tools.style.parse.background( element.styles.background );\r
61\r
62 // We return true only if background specifies **only** color property, and there's only one background directive.\r
63 return background.color && tools.objectKeys( background ).length === 1;\r
64 },\r
65 right: function( element ) {\r
66 var style = new CKEDITOR.style( editor.config.colorButton_backStyle, {\r
67 color: element.styles.background\r
68 } ),\r
69 definition = style.getDefinition();\r
70\r
71 // Align the output object with the template used in config.\r
72 element.name = definition.element;\r
73 element.styles = definition.styles;\r
74 element.attributes = definition.attributes || {};\r
75\r
76 return element;\r
77 }\r
78 }\r
79 ]\r
80 ];\r
81 }\r
82\r
83 addButton( 'BGColor', 'back', lang.bgColorTitle, 20, bgOptions );\r
84 }\r
85\r
86 function addButton( name, type, title, order, options ) {\r
87 var style = new CKEDITOR.style( config[ 'colorButton_' + type + 'Style' ] ),\r
1794320d
IB
88 colorBoxId = CKEDITOR.tools.getNextId() + '_colorBox',\r
89 panelBlock;\r
eaa92715
IB
90\r
91 options = options || {};\r
92\r
93 editor.ui.add( name, CKEDITOR.UI_PANELBUTTON, {\r
94 label: title,\r
95 title: title,\r
96 modes: { wysiwyg: 1 },\r
97 editorFocus: 0,\r
98 toolbar: 'colors,' + order,\r
99 allowedContent: style,\r
100 requiredContent: style,\r
101 contentTransformations: options.contentTransformations,\r
102\r
103 panel: {\r
104 css: CKEDITOR.skin.getPath( 'editor' ),\r
105 attributes: { role: 'listbox', 'aria-label': lang.panelTitle }\r
106 },\r
107\r
108 onBlock: function( panel, block ) {\r
1794320d
IB
109 panelBlock = block;\r
110\r
eaa92715
IB
111 block.autoSize = true;\r
112 block.element.addClass( 'cke_colorblock' );\r
113 block.element.setHtml( renderColors( panel, type, colorBoxId ) );\r
1794320d 114 // The block should not have scrollbars (http://dev.ckeditor.com/ticket/5933, http://dev.ckeditor.com/ticket/6056)\r
eaa92715
IB
115 block.element.getDocument().getBody().setStyle( 'overflow', 'hidden' );\r
116\r
117 CKEDITOR.ui.fire( 'ready', this );\r
118\r
119 var keys = block.keys;\r
120 var rtl = editor.lang.dir == 'rtl';\r
121 keys[ rtl ? 37 : 39 ] = 'next'; // ARROW-RIGHT\r
122 keys[ 40 ] = 'next'; // ARROW-DOWN\r
123 keys[ 9 ] = 'next'; // TAB\r
124 keys[ rtl ? 39 : 37 ] = 'prev'; // ARROW-LEFT\r
125 keys[ 38 ] = 'prev'; // ARROW-UP\r
126 keys[ CKEDITOR.SHIFT + 9 ] = 'prev'; // SHIFT + TAB\r
127 keys[ 32 ] = 'click'; // SPACE\r
128 },\r
129\r
130 refresh: function() {\r
131 if ( !editor.activeFilter.check( style ) )\r
132 this.setState( CKEDITOR.TRISTATE_DISABLED );\r
133 },\r
134\r
1794320d 135 // The automatic colorbox should represent the real color (http://dev.ckeditor.com/ticket/6010)\r
eaa92715
IB
136 onOpen: function() {\r
137\r
138 var selection = editor.getSelection(),\r
139 block = selection && selection.getStartElement(),\r
140 path = editor.elementPath( block ),\r
1794320d 141 automaticColor;\r
eaa92715
IB
142\r
143 if ( !path )\r
144 return;\r
145\r
146 // Find the closest block element.\r
147 block = path.block || path.blockLimit || editor.document.getBody();\r
148\r
149 // The background color might be transparent. In that case, look up the color in the DOM tree.\r
150 do {\r
1794320d 151 automaticColor = block && block.getComputedStyle( type == 'back' ? 'background-color' : 'color' ) || 'transparent';\r
eaa92715 152 }\r
1794320d 153 while ( type == 'back' && automaticColor == 'transparent' && block && ( block = block.getParent() ) );\r
eaa92715
IB
154\r
155 // The box should never be transparent.\r
1794320d
IB
156 if ( !automaticColor || automaticColor == 'transparent' )\r
157 automaticColor = '#ffffff';\r
eaa92715
IB
158\r
159 if ( config.colorButton_enableAutomatic !== false ) {\r
1794320d 160 this._.panel._.iframe.getFrameDocument().getById( colorBoxId ).setStyle( 'background-color', automaticColor );\r
eaa92715
IB
161 }\r
162\r
1794320d
IB
163 var range = selection && selection.getRanges()[ 0 ];\r
164\r
165 if ( range ) {\r
166 var walker = new CKEDITOR.dom.walker( range ),\r
167 element = range.collapsed ? range.startContainer : walker.next(),\r
168 finalColor = '',\r
169 currentColor;\r
170\r
171 while ( element ) {\r
172 if ( element.type === CKEDITOR.NODE_TEXT ) {\r
173 element = element.getParent();\r
174 }\r
175\r
176 currentColor = normalizeColor( element.getComputedStyle( type == 'back' ? 'background-color' : 'color' ) );\r
177 finalColor = finalColor || currentColor;\r
178\r
179 if ( finalColor !== currentColor ) {\r
180 finalColor = '';\r
181 break;\r
182 }\r
183\r
184 element = walker.next();\r
185 }\r
186\r
187 selectColor( panelBlock, finalColor );\r
188 }\r
189\r
190 return automaticColor;\r
eaa92715
IB
191 }\r
192 } );\r
193 }\r
194\r
195 function renderColors( panel, type, colorBoxId ) {\r
196 var output = [],\r
197 colors = config.colorButton_colors.split( ',' ),\r
198 colorsPerRow = config.colorButton_colorsPerRow || 6,\r
199 // Tells if we should include "More Colors..." button.\r
200 moreColorsEnabled = editor.plugins.colordialog && config.colorButton_enableMore !== false,\r
201 // aria-setsize and aria-posinset attributes are used to indicate size of options, because\r
1794320d 202 // screen readers doesn't play nice with table, based layouts (http://dev.ckeditor.com/ticket/12097).\r
eaa92715
IB
203 total = colors.length + ( moreColorsEnabled ? 2 : 1 );\r
204\r
1794320d 205 var clickFn = CKEDITOR.tools.addFunction( function applyColorStyle( color, type ) {\r
eaa92715 206\r
1794320d
IB
207 editor.focus();\r
208 editor.fire( 'saveSnapshot' );\r
eaa92715
IB
209\r
210 if ( color == '?' ) {\r
1794320d
IB
211 editor.getColorFromDialog( function( color ) {\r
212 if ( color ) {\r
213 return applyColor( color );\r
214 }\r
eaa92715 215 } );\r
1794320d
IB
216 } else {\r
217 return applyColor( color );\r
eaa92715
IB
218 }\r
219\r
1794320d
IB
220 function applyColor( color ) {\r
221 // Clean up any conflicting style within the range.\r
222 editor.removeStyle( new CKEDITOR.style( config[ 'colorButton_' + type + 'Style' ], { color: 'inherit' } ) );\r
eaa92715
IB
223 var colorStyle = config[ 'colorButton_' + type + 'Style' ];\r
224\r
225 colorStyle.childRule = type == 'back' ?\r
226 function( element ) {\r
1794320d
IB
227 // It's better to apply background color as the innermost style. (http://dev.ckeditor.com/ticket/3599)\r
228 // Except for "unstylable elements". (http://dev.ckeditor.com/ticket/6103)\r
eaa92715
IB
229 return isUnstylable( element );\r
230 } : function( element ) {\r
1794320d 231 // Fore color style must be applied inside links instead of around it. (http://dev.ckeditor.com/ticket/4772,http://dev.ckeditor.com/ticket/6908)\r
eaa92715
IB
232 return !( element.is( 'a' ) || element.getElementsByTag( 'a' ).count() ) || isUnstylable( element );\r
233 };\r
234\r
1794320d 235 editor.focus();\r
eaa92715 236 editor.applyStyle( new CKEDITOR.style( colorStyle, { color: color } ) );\r
1794320d 237 editor.fire( 'saveSnapshot' );\r
eaa92715
IB
238 }\r
239\r
eaa92715
IB
240 } );\r
241\r
242 if ( config.colorButton_enableAutomatic !== false ) {\r
243 // Render the "Automatic" button.\r
244 output.push( '<a class="cke_colorauto" _cke_focus=1 hidefocus=true' +\r
245 ' title="', lang.auto, '"' +\r
246 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',null,\'', type, '\');return false;"' +\r
247 ' href="javascript:void(\'', lang.auto, '\')"' +\r
248 ' role="option" aria-posinset="1" aria-setsize="', total, '">' +\r
249 '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' +\r
250 '<tr>' +\r
251 '<td colspan="' + colorsPerRow + '" align="center"><span class="cke_colorbox" id="', colorBoxId, '"></span>', lang.auto, '</td>' +\r
252 '</tr>' +\r
253 '</table>' +\r
254 '</a>' );\r
255 }\r
256 output.push( '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' );\r
257\r
258 // Render the color boxes.\r
259 for ( var i = 0; i < colors.length; i++ ) {\r
260 if ( ( i % colorsPerRow ) === 0 )\r
261 output.push( '</tr><tr>' );\r
262\r
263 var parts = colors[ i ].split( '/' ),\r
264 colorName = parts[ 0 ],\r
265 colorCode = parts[ 1 ] || colorName;\r
266\r
267 // The data can be only a color code (without #) or colorName + color code\r
268 // If only a color code is provided, then the colorName is the color with the hash\r
1794320d 269 // Convert the color from RGB to RRGGBB for better compatibility with IE and <font>. See http://dev.ckeditor.com/ticket/5676\r
eaa92715
IB
270 if ( !parts[ 1 ] )\r
271 colorName = '#' + colorName.replace( /^(.)(.)(.)$/, '$1$1$2$2$3$3' );\r
272\r
273 var colorLabel = editor.lang.colorbutton.colors[ colorCode ] || colorCode;\r
274 output.push( '<td>' +\r
275 '<a class="cke_colorbox" _cke_focus=1 hidefocus=true' +\r
276 ' title="', colorLabel, '"' +\r
277 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'', colorName, '\',\'', type, '\'); return false;"' +\r
278 ' href="javascript:void(\'', colorLabel, '\')"' +\r
1794320d 279 ' data-value="' + colorCode + '"' +\r
eaa92715
IB
280 ' role="option" aria-posinset="', ( i + 2 ), '" aria-setsize="', total, '">' +\r
281 '<span class="cke_colorbox" style="background-color:#', colorCode, '"></span>' +\r
282 '</a>' +\r
283 '</td>' );\r
284 }\r
285\r
286 // Render the "More Colors" button.\r
287 if ( moreColorsEnabled ) {\r
288 output.push( '</tr>' +\r
289 '<tr>' +\r
290 '<td colspan="' + colorsPerRow + '" align="center">' +\r
291 '<a class="cke_colormore" _cke_focus=1 hidefocus=true' +\r
292 ' title="', lang.more, '"' +\r
293 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'?\',\'', type, '\');return false;"' +\r
294 ' href="javascript:void(\'', lang.more, '\')"', ' role="option" aria-posinset="', total, '" aria-setsize="', total, '">', lang.more, '</a>' +\r
295 '</td>' ); // tr is later in the code.\r
296 }\r
297\r
298 output.push( '</tr></table>' );\r
299\r
300 return output.join( '' );\r
301 }\r
302\r
303 function isUnstylable( ele ) {\r
304 return ( ele.getAttribute( 'contentEditable' ) == 'false' ) || ele.getAttribute( 'data-nostyle' );\r
305 }\r
1794320d
IB
306\r
307 /*\r
308 * Selects the specified color in the specified panel block.\r
309 *\r
310 * @private\r
311 * @member CKEDITOR.plugins.colorbutton\r
312 * @param {CKEDITOR.ui.panel.block} block\r
313 * @param {String} color\r
314 */\r
315 function selectColor( block, color ) {\r
316 var items = block._.getItems();\r
317\r
318 for ( var i = 0; i < items.count(); i++ ) {\r
319 var item = items.getItem( i );\r
320\r
321 item.removeAttribute( 'aria-selected' );\r
322\r
323 if ( color && color == normalizeColor( item.getAttribute( 'data-value' ) ) ) {\r
324 item.setAttribute( 'aria-selected', true );\r
325 }\r
326 }\r
327 }\r
328\r
329 /*\r
330 * Converts a CSS color value to an easily comparable form.\r
331 *\r
332 * @private\r
333 * @member CKEDITOR.plugins.colorbutton\r
334 * @param {String} color\r
335 * @returns {String}\r
336 */\r
337 function normalizeColor( color ) {\r
338 return CKEDITOR.tools.convertRgbToHex( color || '' ).replace( /#/, '' ).toLowerCase();\r
339 }\r
eaa92715
IB
340 }\r
341} );\r
342\r
343/**\r
344 * Whether to enable the **More Colors** button in the color selectors.\r
345 *\r
346 * Read more in the [documentation](#!/guide/dev_colorbutton)\r
347 * and see the [SDK sample](http://sdk.ckeditor.com/samples/colorbutton.html).\r
348 *\r
349 * config.colorButton_enableMore = false;\r
350 *\r
351 * @cfg {Boolean} [colorButton_enableMore=true]\r
352 * @member CKEDITOR.config\r
353 */\r
354\r
355/**\r
356 * Defines the colors to be displayed in the color selectors. This is a string\r
357 * containing hexadecimal notation for HTML colors, without the `'#'` prefix.\r
358 *\r
359 * **Since 3.3:** A color name may optionally be defined by prefixing the entries with\r
360 * a name and the slash character. For example, `'FontColor1/FF9900'` will be\r
361 * displayed as the color `#FF9900` in the selector, but will be output as `'FontColor1'`.\r
362 *\r
363 * **Since 4.6.2:** The default color palette has changed. It contains fewer colors in more\r
364 * pastel shades than the previous one.\r
365 *\r
366 * Read more in the [documentation](#!/guide/dev_colorbutton)\r
367 * and see the [SDK sample](http://sdk.ckeditor.com/samples/colorbutton.html).\r
368 *\r
369 * // Brazil colors only.\r
370 * config.colorButton_colors = '00923E,F8C100,28166F';\r
371 *\r
372 * config.colorButton_colors = 'FontColor1/FF9900,FontColor2/0066CC,FontColor3/F00';\r
373 *\r
374 * // CKEditor color palette available before version 4.6.2.\r
375 * config.colorButton_colors =\r
376 * '000,800000,8B4513,2F4F4F,008080,000080,4B0082,696969,' +\r
377 * 'B22222,A52A2A,DAA520,006400,40E0D0,0000CD,800080,808080,' +\r
378 * 'F00,FF8C00,FFD700,008000,0FF,00F,EE82EE,A9A9A9,' +\r
379 * 'FFA07A,FFA500,FFFF00,00FF00,AFEEEE,ADD8E6,DDA0DD,D3D3D3,' +\r
380 * 'FFF0F5,FAEBD7,FFFFE0,F0FFF0,F0FFFF,F0F8FF,E6E6FA,FFF';\r
381 *\r
382 * @cfg {String} [colorButton_colors=see source]\r
383 * @member CKEDITOR.config\r
384 */\r
385CKEDITOR.config.colorButton_colors = '1ABC9C,2ECC71,3498DB,9B59B6,4E5F70,F1C40F,' +\r
386 '16A085,27AE60,2980B9,8E44AD,2C3E50,F39C12,' +\r
387 'E67E22,E74C3C,ECF0F1,95A5A6,DDD,FFF,' +\r
388 'D35400,C0392B,BDC3C7,7F8C8D,999,000';\r
389\r
390/**\r
391 * Stores the style definition that applies the text foreground color.\r
392 *\r
393 * Read more in the [documentation](#!/guide/dev_colorbutton)\r
394 * and see the [SDK sample](http://sdk.ckeditor.com/samples/colorbutton.html).\r
395 *\r
396 * // This is actually the default value.\r
397 * config.colorButton_foreStyle = {\r
398 * element: 'span',\r
399 * styles: { color: '#(color)' }\r
400 * };\r
401 *\r
402 * @cfg [colorButton_foreStyle=see source]\r
403 * @member CKEDITOR.config\r
404 */\r
405CKEDITOR.config.colorButton_foreStyle = {\r
406 element: 'span',\r
407 styles: { 'color': '#(color)' },\r
408 overrides: [ {\r
409 element: 'font', attributes: { 'color': null }\r
410 } ]\r
411};\r
412\r
413/**\r
414 * Stores the style definition that applies the text background color.\r
415 *\r
416 * Read more in the [documentation](#!/guide/dev_colorbutton)\r
417 * and see the [SDK sample](http://sdk.ckeditor.com/samples/colorbutton.html).\r
418 *\r
419 * // This is actually the default value.\r
420 * config.colorButton_backStyle = {\r
421 * element: 'span',\r
422 * styles: { 'background-color': '#(color)' }\r
423 * };\r
424 *\r
425 * @cfg [colorButton_backStyle=see source]\r
426 * @member CKEDITOR.config\r
427 */\r
428CKEDITOR.config.colorButton_backStyle = {\r
429 element: 'span',\r
430 styles: { 'background-color': '#(color)' }\r
431};\r
432\r
433/**\r
434 * Whether to enable the **Automatic** button in the color selectors.\r
435 *\r
436 * Read more in the [documentation](#!/guide/dev_colorbutton)\r
437 * and see the [SDK sample](http://sdk.ckeditor.com/samples/colorbutton.html).\r
438 *\r
439 * config.colorButton_enableAutomatic = false;\r
440 *\r
441 * @cfg {Boolean} [colorButton_enableAutomatic=true]\r
442 * @member CKEDITOR.config\r
443 */\r
444\r
445/**\r
446 * Defines how many colors will be shown per row in the color selectors.\r
447 *\r
448 * Read more in the [documentation](#!/guide/dev_colorbutton)\r
449 * and see the [SDK sample](http://sdk.ckeditor.com/samples/colorbutton.html).\r
450 *\r
451 * config.colorButton_colorsPerRow = 8;\r
452 *\r
453 * @since 4.6.2\r
454 * @cfg {Number} [colorButton_colorsPerRow=6]\r
455 * @member CKEDITOR.config\r
456 */\r
457\r
458/**\r
459 * Whether the plugin should convert `background` CSS properties with color only, to a `background-color` property,\r
460 * allowing the [Color Button](http://ckeditor.com/addon/colorbutton) plugin to edit these styles.\r
461 *\r
462 * config.colorButton_normalizeBackground = false;\r
463 *\r
464 * @since 4.6.1\r
465 * @cfg {Boolean} [colorButton_normalizeBackground=true]\r
466 * @member CKEDITOR.config\r
467 */\r