]> git.immae.eu Git - perso/Immae/Projets/packagist/ludivine-ckeditor-component.git/blame - sources/plugins/colorbutton/plugin.js
Add audio, color and fonts
[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
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,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
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
88 colorBoxId = CKEDITOR.tools.getNextId() + '_colorBox';\r
89\r
90 options = options || {};\r
91\r
92 editor.ui.add( name, CKEDITOR.UI_PANELBUTTON, {\r
93 label: title,\r
94 title: title,\r
95 modes: { wysiwyg: 1 },\r
96 editorFocus: 0,\r
97 toolbar: 'colors,' + order,\r
98 allowedContent: style,\r
99 requiredContent: style,\r
100 contentTransformations: options.contentTransformations,\r
101\r
102 panel: {\r
103 css: CKEDITOR.skin.getPath( 'editor' ),\r
104 attributes: { role: 'listbox', 'aria-label': lang.panelTitle }\r
105 },\r
106\r
107 onBlock: function( panel, block ) {\r
108 block.autoSize = true;\r
109 block.element.addClass( 'cke_colorblock' );\r
110 block.element.setHtml( renderColors( panel, type, colorBoxId ) );\r
111 // The block should not have scrollbars (#5933, #6056)\r
112 block.element.getDocument().getBody().setStyle( 'overflow', 'hidden' );\r
113\r
114 CKEDITOR.ui.fire( 'ready', this );\r
115\r
116 var keys = block.keys;\r
117 var rtl = editor.lang.dir == 'rtl';\r
118 keys[ rtl ? 37 : 39 ] = 'next'; // ARROW-RIGHT\r
119 keys[ 40 ] = 'next'; // ARROW-DOWN\r
120 keys[ 9 ] = 'next'; // TAB\r
121 keys[ rtl ? 39 : 37 ] = 'prev'; // ARROW-LEFT\r
122 keys[ 38 ] = 'prev'; // ARROW-UP\r
123 keys[ CKEDITOR.SHIFT + 9 ] = 'prev'; // SHIFT + TAB\r
124 keys[ 32 ] = 'click'; // SPACE\r
125 },\r
126\r
127 refresh: function() {\r
128 if ( !editor.activeFilter.check( style ) )\r
129 this.setState( CKEDITOR.TRISTATE_DISABLED );\r
130 },\r
131\r
132 // The automatic colorbox should represent the real color (#6010)\r
133 onOpen: function() {\r
134\r
135 var selection = editor.getSelection(),\r
136 block = selection && selection.getStartElement(),\r
137 path = editor.elementPath( block ),\r
138 color;\r
139\r
140 if ( !path )\r
141 return;\r
142\r
143 // Find the closest block element.\r
144 block = path.block || path.blockLimit || editor.document.getBody();\r
145\r
146 // The background color might be transparent. In that case, look up the color in the DOM tree.\r
147 do {\r
148 color = block && block.getComputedStyle( type == 'back' ? 'background-color' : 'color' ) || 'transparent';\r
149 }\r
150 while ( type == 'back' && color == 'transparent' && block && ( block = block.getParent() ) );\r
151\r
152 // The box should never be transparent.\r
153 if ( !color || color == 'transparent' )\r
154 color = '#ffffff';\r
155\r
156 if ( config.colorButton_enableAutomatic !== false ) {\r
157 this._.panel._.iframe.getFrameDocument().getById( colorBoxId ).setStyle( 'background-color', color );\r
158 }\r
159\r
160 return color;\r
161 }\r
162 } );\r
163 }\r
164\r
165 function renderColors( panel, type, colorBoxId ) {\r
166 var output = [],\r
167 colors = config.colorButton_colors.split( ',' ),\r
168 colorsPerRow = config.colorButton_colorsPerRow || 6,\r
169 // Tells if we should include "More Colors..." button.\r
170 moreColorsEnabled = editor.plugins.colordialog && config.colorButton_enableMore !== false,\r
171 // aria-setsize and aria-posinset attributes are used to indicate size of options, because\r
172 // screen readers doesn't play nice with table, based layouts (#12097).\r
173 total = colors.length + ( moreColorsEnabled ? 2 : 1 );\r
174\r
175 var clickFn = CKEDITOR.tools.addFunction( function( color, type ) {\r
176 var applyColorStyle = arguments.callee;\r
177 function onColorDialogClose( evt ) {\r
178 this.removeListener( 'ok', onColorDialogClose );\r
179 this.removeListener( 'cancel', onColorDialogClose );\r
180\r
181 evt.name == 'ok' && applyColorStyle( this.getContentElement( 'picker', 'selectedColor' ).getValue(), type );\r
182 }\r
183\r
184 if ( color == '?' ) {\r
185 editor.openDialog( 'colordialog', function() {\r
186 this.on( 'ok', onColorDialogClose );\r
187 this.on( 'cancel', onColorDialogClose );\r
188 } );\r
189\r
190 return;\r
191 }\r
192\r
193 editor.focus();\r
194\r
195 panel.hide();\r
196\r
197 editor.fire( 'saveSnapshot' );\r
198\r
199 // Clean up any conflicting style within the range.\r
200 editor.removeStyle( new CKEDITOR.style( config[ 'colorButton_' + type + 'Style' ], { color: 'inherit' } ) );\r
201\r
202 if ( color ) {\r
203 var colorStyle = config[ 'colorButton_' + type + 'Style' ];\r
204\r
205 colorStyle.childRule = type == 'back' ?\r
206 function( element ) {\r
207 // It's better to apply background color as the innermost style. (#3599)\r
208 // Except for "unstylable elements". (#6103)\r
209 return isUnstylable( element );\r
210 } : function( element ) {\r
211 // Fore color style must be applied inside links instead of around it. (#4772,#6908)\r
212 return !( element.is( 'a' ) || element.getElementsByTag( 'a' ).count() ) || isUnstylable( element );\r
213 };\r
214\r
215 editor.applyStyle( new CKEDITOR.style( colorStyle, { color: color } ) );\r
216 }\r
217\r
218 editor.fire( 'saveSnapshot' );\r
219 } );\r
220\r
221 if ( config.colorButton_enableAutomatic !== false ) {\r
222 // Render the "Automatic" button.\r
223 output.push( '<a class="cke_colorauto" _cke_focus=1 hidefocus=true' +\r
224 ' title="', lang.auto, '"' +\r
225 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',null,\'', type, '\');return false;"' +\r
226 ' href="javascript:void(\'', lang.auto, '\')"' +\r
227 ' role="option" aria-posinset="1" aria-setsize="', total, '">' +\r
228 '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' +\r
229 '<tr>' +\r
230 '<td colspan="' + colorsPerRow + '" align="center"><span class="cke_colorbox" id="', colorBoxId, '"></span>', lang.auto, '</td>' +\r
231 '</tr>' +\r
232 '</table>' +\r
233 '</a>' );\r
234 }\r
235 output.push( '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' );\r
236\r
237 // Render the color boxes.\r
238 for ( var i = 0; i < colors.length; i++ ) {\r
239 if ( ( i % colorsPerRow ) === 0 )\r
240 output.push( '</tr><tr>' );\r
241\r
242 var parts = colors[ i ].split( '/' ),\r
243 colorName = parts[ 0 ],\r
244 colorCode = parts[ 1 ] || colorName;\r
245\r
246 // The data can be only a color code (without #) or colorName + color code\r
247 // If only a color code is provided, then the colorName is the color with the hash\r
248 // Convert the color from RGB to RRGGBB for better compatibility with IE and <font>. See #5676\r
249 if ( !parts[ 1 ] )\r
250 colorName = '#' + colorName.replace( /^(.)(.)(.)$/, '$1$1$2$2$3$3' );\r
251\r
252 var colorLabel = editor.lang.colorbutton.colors[ colorCode ] || colorCode;\r
253 output.push( '<td>' +\r
254 '<a class="cke_colorbox" _cke_focus=1 hidefocus=true' +\r
255 ' title="', colorLabel, '"' +\r
256 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'', colorName, '\',\'', type, '\'); return false;"' +\r
257 ' href="javascript:void(\'', colorLabel, '\')"' +\r
258 ' role="option" aria-posinset="', ( i + 2 ), '" aria-setsize="', total, '">' +\r
259 '<span class="cke_colorbox" style="background-color:#', colorCode, '"></span>' +\r
260 '</a>' +\r
261 '</td>' );\r
262 }\r
263\r
264 // Render the "More Colors" button.\r
265 if ( moreColorsEnabled ) {\r
266 output.push( '</tr>' +\r
267 '<tr>' +\r
268 '<td colspan="' + colorsPerRow + '" align="center">' +\r
269 '<a class="cke_colormore" _cke_focus=1 hidefocus=true' +\r
270 ' title="', lang.more, '"' +\r
271 ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'?\',\'', type, '\');return false;"' +\r
272 ' href="javascript:void(\'', lang.more, '\')"', ' role="option" aria-posinset="', total, '" aria-setsize="', total, '">', lang.more, '</a>' +\r
273 '</td>' ); // tr is later in the code.\r
274 }\r
275\r
276 output.push( '</tr></table>' );\r
277\r
278 return output.join( '' );\r
279 }\r
280\r
281 function isUnstylable( ele ) {\r
282 return ( ele.getAttribute( 'contentEditable' ) == 'false' ) || ele.getAttribute( 'data-nostyle' );\r
283 }\r
284 }\r
285} );\r
286\r
287/**\r
288 * Whether to enable the **More Colors** button in the color selectors.\r
289 *\r
290 * Read more in the [documentation](#!/guide/dev_colorbutton)\r
291 * and see the [SDK sample](http://sdk.ckeditor.com/samples/colorbutton.html).\r
292 *\r
293 * config.colorButton_enableMore = false;\r
294 *\r
295 * @cfg {Boolean} [colorButton_enableMore=true]\r
296 * @member CKEDITOR.config\r
297 */\r
298\r
299/**\r
300 * Defines the colors to be displayed in the color selectors. This is a string\r
301 * containing hexadecimal notation for HTML colors, without the `'#'` prefix.\r
302 *\r
303 * **Since 3.3:** A color name may optionally be defined by prefixing the entries with\r
304 * a name and the slash character. For example, `'FontColor1/FF9900'` will be\r
305 * displayed as the color `#FF9900` in the selector, but will be output as `'FontColor1'`.\r
306 *\r
307 * **Since 4.6.2:** The default color palette has changed. It contains fewer colors in more\r
308 * pastel shades than the previous one.\r
309 *\r
310 * Read more in the [documentation](#!/guide/dev_colorbutton)\r
311 * and see the [SDK sample](http://sdk.ckeditor.com/samples/colorbutton.html).\r
312 *\r
313 * // Brazil colors only.\r
314 * config.colorButton_colors = '00923E,F8C100,28166F';\r
315 *\r
316 * config.colorButton_colors = 'FontColor1/FF9900,FontColor2/0066CC,FontColor3/F00';\r
317 *\r
318 * // CKEditor color palette available before version 4.6.2.\r
319 * config.colorButton_colors =\r
320 * '000,800000,8B4513,2F4F4F,008080,000080,4B0082,696969,' +\r
321 * 'B22222,A52A2A,DAA520,006400,40E0D0,0000CD,800080,808080,' +\r
322 * 'F00,FF8C00,FFD700,008000,0FF,00F,EE82EE,A9A9A9,' +\r
323 * 'FFA07A,FFA500,FFFF00,00FF00,AFEEEE,ADD8E6,DDA0DD,D3D3D3,' +\r
324 * 'FFF0F5,FAEBD7,FFFFE0,F0FFF0,F0FFFF,F0F8FF,E6E6FA,FFF';\r
325 *\r
326 * @cfg {String} [colorButton_colors=see source]\r
327 * @member CKEDITOR.config\r
328 */\r
329CKEDITOR.config.colorButton_colors = '1ABC9C,2ECC71,3498DB,9B59B6,4E5F70,F1C40F,' +\r
330 '16A085,27AE60,2980B9,8E44AD,2C3E50,F39C12,' +\r
331 'E67E22,E74C3C,ECF0F1,95A5A6,DDD,FFF,' +\r
332 'D35400,C0392B,BDC3C7,7F8C8D,999,000';\r
333\r
334/**\r
335 * Stores the style definition that applies the text foreground color.\r
336 *\r
337 * Read more in the [documentation](#!/guide/dev_colorbutton)\r
338 * and see the [SDK sample](http://sdk.ckeditor.com/samples/colorbutton.html).\r
339 *\r
340 * // This is actually the default value.\r
341 * config.colorButton_foreStyle = {\r
342 * element: 'span',\r
343 * styles: { color: '#(color)' }\r
344 * };\r
345 *\r
346 * @cfg [colorButton_foreStyle=see source]\r
347 * @member CKEDITOR.config\r
348 */\r
349CKEDITOR.config.colorButton_foreStyle = {\r
350 element: 'span',\r
351 styles: { 'color': '#(color)' },\r
352 overrides: [ {\r
353 element: 'font', attributes: { 'color': null }\r
354 } ]\r
355};\r
356\r
357/**\r
358 * Stores the style definition that applies the text background color.\r
359 *\r
360 * Read more in the [documentation](#!/guide/dev_colorbutton)\r
361 * and see the [SDK sample](http://sdk.ckeditor.com/samples/colorbutton.html).\r
362 *\r
363 * // This is actually the default value.\r
364 * config.colorButton_backStyle = {\r
365 * element: 'span',\r
366 * styles: { 'background-color': '#(color)' }\r
367 * };\r
368 *\r
369 * @cfg [colorButton_backStyle=see source]\r
370 * @member CKEDITOR.config\r
371 */\r
372CKEDITOR.config.colorButton_backStyle = {\r
373 element: 'span',\r
374 styles: { 'background-color': '#(color)' }\r
375};\r
376\r
377/**\r
378 * Whether to enable the **Automatic** button in the color selectors.\r
379 *\r
380 * Read more in the [documentation](#!/guide/dev_colorbutton)\r
381 * and see the [SDK sample](http://sdk.ckeditor.com/samples/colorbutton.html).\r
382 *\r
383 * config.colorButton_enableAutomatic = false;\r
384 *\r
385 * @cfg {Boolean} [colorButton_enableAutomatic=true]\r
386 * @member CKEDITOR.config\r
387 */\r
388\r
389/**\r
390 * Defines how many colors will be shown per row in the color selectors.\r
391 *\r
392 * Read more in the [documentation](#!/guide/dev_colorbutton)\r
393 * and see the [SDK sample](http://sdk.ckeditor.com/samples/colorbutton.html).\r
394 *\r
395 * config.colorButton_colorsPerRow = 8;\r
396 *\r
397 * @since 4.6.2\r
398 * @cfg {Number} [colorButton_colorsPerRow=6]\r
399 * @member CKEDITOR.config\r
400 */\r
401\r
402/**\r
403 * Whether the plugin should convert `background` CSS properties with color only, to a `background-color` property,\r
404 * allowing the [Color Button](http://ckeditor.com/addon/colorbutton) plugin to edit these styles.\r
405 *\r
406 * config.colorButton_normalizeBackground = false;\r
407 *\r
408 * @since 4.6.1\r
409 * @cfg {Boolean} [colorButton_normalizeBackground=true]\r
410 * @member CKEDITOR.config\r
411 */\r