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