2
* @license Copyright (c) 2003-2014, CKSource - Frederico Knabben. All rights reserved.
3
* For licensing, see LICENSE.md or http://ckeditor.com/license
7
* @fileOverview The "colorbutton" plugin that makes it possible to assign
8
* text and background colors to editor contents.
11
CKEDITOR.plugins.add( 'colorbutton', {
12
requires: 'panelbutton,floatpanel',
13
lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,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,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%
14
icons: 'bgcolor,textcolor', // %REMOVE_LINE_CORE%
15
hidpi: true, // %REMOVE_LINE_CORE%
16
init: function( editor ) {
17
var config = editor.config,
18
lang = editor.lang.colorbutton;
22
if ( !CKEDITOR.env.hc ) {
23
addButton( 'TextColor', 'fore', lang.textColorTitle, 10 );
24
addButton( 'BGColor', 'back', lang.bgColorTitle, 20 );
27
function addButton( name, type, title, order ) {
28
var style = new CKEDITOR.style( config[ 'colorButton_' + type + 'Style' ] ),
29
colorBoxId = CKEDITOR.tools.getNextId() + '_colorBox';
31
editor.ui.add( name, CKEDITOR.UI_PANELBUTTON, {
34
modes: { wysiwyg: 1 },
36
toolbar: 'colors,' + order,
37
allowedContent: style,
38
requiredContent: style,
41
css: CKEDITOR.skin.getPath( 'editor' ),
42
attributes: { role: 'listbox', 'aria-label': lang.panelTitle }
45
onBlock: function( panel, block ) {
46
block.autoSize = true;
47
block.element.addClass( 'cke_colorblock' );
48
block.element.setHtml( renderColors( panel, type, colorBoxId ) );
49
// The block should not have scrollbars (#5933, #6056)
50
block.element.getDocument().getBody().setStyle( 'overflow', 'hidden' );
52
CKEDITOR.ui.fire( 'ready', this );
54
var keys = block.keys;
55
var rtl = editor.lang.dir == 'rtl';
56
keys[ rtl ? 37 : 39 ] = 'next'; // ARROW-RIGHT
57
keys[ 40 ] = 'next'; // ARROW-DOWN
58
keys[ 9 ] = 'next'; // TAB
59
keys[ rtl ? 39 : 37 ] = 'prev'; // ARROW-LEFT
60
keys[ 38 ] = 'prev'; // ARROW-UP
61
keys[ CKEDITOR.SHIFT + 9 ] = 'prev'; // SHIFT + TAB
62
keys[ 32 ] = 'click'; // SPACE
66
if ( !editor.activeFilter.check( style ) )
67
this.setState( CKEDITOR.TRISTATE_DISABLED );
70
// The automatic colorbox should represent the real color (#6010)
73
var selection = editor.getSelection(),
74
block = selection && selection.getStartElement(),
75
path = editor.elementPath( block ),
81
// Find the closest block element.
82
block = path.block || path.blockLimit || editor.document.getBody();
84
// The background color might be transparent. In that case, look up the color in the DOM tree.
86
color = block && block.getComputedStyle( type == 'back' ? 'background-color' : 'color' ) || 'transparent';
88
while ( type == 'back' && color == 'transparent' && block && ( block = block.getParent() ) );
90
// The box should never be transparent.
91
if ( !color || color == 'transparent' )
94
this._.panel._.iframe.getFrameDocument().getById( colorBoxId ).setStyle( 'background-color', color );
102
function renderColors( panel, type, colorBoxId ) {
104
colors = config.colorButton_colors.split( ',' );
106
var clickFn = CKEDITOR.tools.addFunction( function( color, type ) {
107
if ( color == '?' ) {
108
var applyColorStyle = arguments.callee;
110
function onColorDialogClose( evt ) {
111
this.removeListener( 'ok', onColorDialogClose );
112
this.removeListener( 'cancel', onColorDialogClose );
114
evt.name == 'ok' && applyColorStyle( this.getContentElement( 'picker', 'selectedColor' ).getValue(), type );
117
editor.openDialog( 'colordialog', function() {
118
this.on( 'ok', onColorDialogClose );
119
this.on( 'cancel', onColorDialogClose );
129
editor.fire( 'saveSnapshot' );
131
// Clean up any conflicting style within the range.
132
editor.removeStyle( new CKEDITOR.style( config[ 'colorButton_' + type + 'Style' ], { color: 'inherit' } ) );
135
var colorStyle = config[ 'colorButton_' + type + 'Style' ];
137
colorStyle.childRule = type == 'back' ?
138
function( element ) {
139
// It's better to apply background color as the innermost style. (#3599)
140
// Except for "unstylable elements". (#6103)
141
return isUnstylable( element );
142
} : function( element ) {
143
// Fore color style must be applied inside links instead of around it. (#4772,#6908)
144
return !( element.is( 'a' ) || element.getElementsByTag( 'a' ).count() ) || isUnstylable( element );
147
editor.applyStyle( new CKEDITOR.style( colorStyle, { color: color } ) );
150
editor.fire( 'saveSnapshot' );
153
// Render the "Automatic" button.
154
output.push( '<a class="cke_colorauto" _cke_focus=1 hidefocus=true' +
155
' title="', lang.auto, '"' +
156
' onclick="CKEDITOR.tools.callFunction(', clickFn, ',null,\'', type, '\');return false;"' +
157
' href="javascript:void(\'', lang.auto, '\')"' +
159
'<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' +
162
'<span class="cke_colorbox" id="', colorBoxId, '"></span>' +
164
'<td colspan=7 align=center>', lang.auto, '</td>' +
168
'<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' );
170
// Render the color boxes.
171
for ( var i = 0; i < colors.length; i++ ) {
172
if ( ( i % 8 ) === 0 )
173
output.push( '</tr><tr>' );
175
var parts = colors[ i ].split( '/' ),
176
colorName = parts[ 0 ],
177
colorCode = parts[ 1 ] || colorName;
179
// The data can be only a color code (without #) or colorName + color code
180
// If only a color code is provided, then the colorName is the color with the hash
181
// Convert the color from RGB to RRGGBB for better compatibility with IE and <font>. See #5676
183
colorName = '#' + colorName.replace( /^(.)(.)(.)$/, '$1$1$2$2$3$3' );
185
var colorLabel = editor.lang.colorbutton.colors[ colorCode ] || colorCode;
186
output.push( '<td>' +
187
'<a class="cke_colorbox" _cke_focus=1 hidefocus=true' +
188
' title="', colorLabel, '"' +
189
' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'', colorName, '\',\'', type, '\'); return false;"' +
190
' href="javascript:void(\'', colorLabel, '\')"' +
192
'<span class="cke_colorbox" style="background-color:#', colorCode, '"></span>' +
197
// Render the "More Colors" button.
198
if ( editor.plugins.colordialog && config.colorButton_enableMore === undefined || config.colorButton_enableMore ) {
199
output.push( '</tr>' +
201
'<td colspan=8 align=center>' +
202
'<a class="cke_colormore" _cke_focus=1 hidefocus=true' +
203
' title="', lang.more, '"' +
204
' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'?\',\'', type, '\');return false;"' +
205
' href="javascript:void(\'', lang.more, '\')"', ' role="option">', lang.more, '</a>' +
206
'</td>' ); // tr is later in the code.
209
output.push( '</tr></table>' );
211
return output.join( '' );
214
function isUnstylable( ele ) {
215
return ( ele.getAttribute( 'contentEditable' ) == 'false' ) || ele.getAttribute( 'data-nostyle' );
221
* Whether to enable the **More Colors*** button in the color selectors.
223
* config.colorButton_enableMore = false;
225
* @cfg {Boolean} [colorButton_enableMore=true]
226
* @member CKEDITOR.config
230
* Defines the colors to be displayed in the color selectors. This is a string
231
* containing hexadecimal notation for HTML colors, without the `'#'` prefix.
233
* **Since 3.3:** A color name may optionally be defined by prefixing the entries with
234
* a name and the slash character. For example, `'FontColor1/FF9900'` will be
235
* displayed as the color `#FF9900` in the selector, but will be output as `'FontColor1'`.
237
* // Brazil colors only.
238
* config.colorButton_colors = '00923E,F8C100,28166F';
240
* config.colorButton_colors = 'FontColor1/FF9900,FontColor2/0066CC,FontColor3/F00';
242
* @cfg {String} [colorButton_colors=see source]
243
* @member CKEDITOR.config
245
CKEDITOR.config.colorButton_colors = '000,800000,8B4513,2F4F4F,008080,000080,4B0082,696969,' +
246
'B22222,A52A2A,DAA520,006400,40E0D0,0000CD,800080,808080,' +
247
'F00,FF8C00,FFD700,008000,0FF,00F,EE82EE,A9A9A9,' +
248
'FFA07A,FFA500,FFFF00,00FF00,AFEEEE,ADD8E6,DDA0DD,D3D3D3,' +
249
'FFF0F5,FAEBD7,FFFFE0,F0FFF0,F0FFFF,F0F8FF,E6E6FA,FFF';
252
* Stores the style definition that applies the text foreground color.
254
* // This is actually the default value.
255
* config.colorButton_foreStyle = {
257
* styles: { color: '#(color)' }
260
* @cfg [colorButton_foreStyle=see source]
261
* @member CKEDITOR.config
263
CKEDITOR.config.colorButton_foreStyle = {
265
styles: { 'color': '#(color)' },
267
element: 'font', attributes: { 'color': null }
272
* Stores the style definition that applies the text background color.
274
* // This is actually the default value.
275
* config.colorButton_backStyle = {
277
* styles: { 'background-color': '#(color)' }
280
* @cfg [colorButton_backStyle=see source]
281
* @member CKEDITOR.config
283
CKEDITOR.config.colorButton_backStyle = {
285
styles: { 'background-color': '#(color)' }