plugin.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
  3. For licensing, see LICENSE.html or http://ckeditor.com/license
  4. */
  5. /**
  6. * @fileOverview The "colorbutton" plugin that makes it possible to assign
  7. * text and background colors to editor contents.
  8. *
  9. */
  10. CKEDITOR.plugins.add( 'colorbutton',
  11. {
  12. requires : [ 'panelbutton', 'floatpanel', 'styles' ],
  13. init : function( editor )
  14. {
  15. var config = editor.config,
  16. lang = editor.lang.colorButton;
  17. var clickFn;
  18. if ( !CKEDITOR.env.hc )
  19. {
  20. addButton( 'TextColor', 'fore', lang.textColorTitle );
  21. addButton( 'BGColor', 'back', lang.bgColorTitle );
  22. }
  23. function addButton( name, type, title )
  24. {
  25. var colorBoxId = CKEDITOR.tools.getNextId() + '_colorBox';
  26. editor.ui.add( name, CKEDITOR.UI_PANELBUTTON,
  27. {
  28. label : title,
  29. title : title,
  30. className : 'cke_button_' + name.toLowerCase(),
  31. modes : { wysiwyg : 1 },
  32. panel :
  33. {
  34. css : editor.skin.editor.css,
  35. attributes : { role : 'listbox', 'aria-label' : lang.panelTitle }
  36. },
  37. onBlock : function( panel, block )
  38. {
  39. block.autoSize = true;
  40. block.element.addClass( 'cke_colorblock' );
  41. block.element.setHtml( renderColors( panel, type, colorBoxId ) );
  42. // The block should not have scrollbars (#5933, #6056)
  43. block.element.getDocument().getBody().setStyle( 'overflow', 'hidden' );
  44. CKEDITOR.ui.fire( 'ready', this );
  45. var keys = block.keys;
  46. var rtl = editor.lang.dir == 'rtl';
  47. keys[ rtl ? 37 : 39 ] = 'next'; // ARROW-RIGHT
  48. keys[ 40 ] = 'next'; // ARROW-DOWN
  49. keys[ 9 ] = 'next'; // TAB
  50. keys[ rtl ? 39 : 37 ] = 'prev'; // ARROW-LEFT
  51. keys[ 38 ] = 'prev'; // ARROW-UP
  52. keys[ CKEDITOR.SHIFT + 9 ] = 'prev'; // SHIFT + TAB
  53. keys[ 32 ] = 'click'; // SPACE
  54. },
  55. // The automatic colorbox should represent the real color (#6010)
  56. onOpen : function()
  57. {
  58. var selection = editor.getSelection(),
  59. block = selection && selection.getStartElement(),
  60. path = new CKEDITOR.dom.elementPath( block ),
  61. color;
  62. // Find the closest block element.
  63. block = path.block || path.blockLimit || editor.document.getBody();
  64. // The background color might be transparent. In that case, look up the color in the DOM tree.
  65. do
  66. {
  67. color = block && block.getComputedStyle( type == 'back' ? 'background-color' : 'color' ) || 'transparent';
  68. }
  69. while ( type == 'back' && color == 'transparent' && block && ( block = block.getParent() ) );
  70. // The box should never be transparent.
  71. if ( !color || color == 'transparent' )
  72. color = '#ffffff';
  73. this._.panel._.iframe.getFrameDocument().getById( colorBoxId ).setStyle( 'background-color', color );
  74. }
  75. });
  76. }
  77. function renderColors( panel, type, colorBoxId )
  78. {
  79. var output = [],
  80. colors = config.colorButton_colors.split( ',' ),
  81. total = colors.length + ( config.colorButton_enableMore ? 2 : 1 );
  82. var clickFn = CKEDITOR.tools.addFunction( function( color, type )
  83. {
  84. if ( color == '?' )
  85. {
  86. var applyColorStyle = arguments.callee;
  87. function onColorDialogClose( evt )
  88. {
  89. this.removeListener( 'ok', onColorDialogClose );
  90. this.removeListener( 'cancel', onColorDialogClose );
  91. evt.name == 'ok' && applyColorStyle( this.getContentElement( 'picker', 'selectedColor' ).getValue(), type );
  92. }
  93. editor.openDialog( 'colordialog', function()
  94. {
  95. this.on( 'ok', onColorDialogClose );
  96. this.on( 'cancel', onColorDialogClose );
  97. } );
  98. return;
  99. }
  100. editor.focus();
  101. panel.hide( false );
  102. editor.fire( 'saveSnapshot' );
  103. // Clean up any conflicting style within the range.
  104. new CKEDITOR.style( config['colorButton_' + type + 'Style'], { color : 'inherit' } ).remove( editor.document );
  105. if ( color )
  106. {
  107. var colorStyle = config['colorButton_' + type + 'Style'];
  108. colorStyle.childRule = type == 'back' ?
  109. function( element )
  110. {
  111. // It's better to apply background color as the innermost style. (#3599)
  112. // Except for "unstylable elements". (#6103)
  113. return isUnstylable( element );
  114. }
  115. :
  116. function( element )
  117. {
  118. // Fore color style must be applied inside links instead of around it. (#4772,#6908)
  119. return !( element.is( 'a' ) || element.getElementsByTag( 'a' ).count() ) || isUnstylable( element );
  120. };
  121. new CKEDITOR.style( colorStyle, { color : color } ).apply( editor.document );
  122. }
  123. editor.fire( 'saveSnapshot' );
  124. });
  125. // Render the "Automatic" button.
  126. output.push(
  127. '<a class="cke_colorauto" _cke_focus=1 hidefocus=true' +
  128. ' title="', lang.auto, '"' +
  129. ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',null,\'', type, '\');return false;"' +
  130. ' href="javascript:void(\'', lang.auto, '\')"' +
  131. ' role="option" aria-posinset="1" aria-setsize="', total, '">' +
  132. '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' +
  133. '<tr>' +
  134. '<td>' +
  135. '<span class="cke_colorbox" id="', colorBoxId, '"></span>' +
  136. '</td>' +
  137. '<td colspan=7 align=center>',
  138. lang.auto,
  139. '</td>' +
  140. '</tr>' +
  141. '</table>' +
  142. '</a>' +
  143. '<table role="presentation" cellspacing=0 cellpadding=0 width="100%">' );
  144. // Render the color boxes.
  145. for ( var i = 0 ; i < colors.length ; i++ )
  146. {
  147. if ( ( i % 8 ) === 0 )
  148. output.push( '</tr><tr>' );
  149. var parts = colors[ i ].split( '/' ),
  150. colorName = parts[ 0 ],
  151. colorCode = parts[ 1 ] || colorName;
  152. // The data can be only a color code (without #) or colorName + color code
  153. // If only a color code is provided, then the colorName is the color with the hash
  154. // Convert the color from RGB to RRGGBB for better compatibility with IE and <font>. See #5676
  155. if (!parts[1])
  156. colorName = '#' + colorName.replace( /^(.)(.)(.)$/, '$1$1$2$2$3$3' );
  157. var colorLabel = editor.lang.colors[ colorCode ] || colorCode;
  158. output.push(
  159. '<td>' +
  160. '<a class="cke_colorbox" _cke_focus=1 hidefocus=true' +
  161. ' title="', colorLabel, '"' +
  162. ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'', colorName, '\',\'', type, '\'); return false;"' +
  163. ' href="javascript:void(\'', colorLabel, '\')"' +
  164. ' role="option" aria-posinset="', ( i + 2 ), '" aria-setsize="', total, '">' +
  165. '<span class="cke_colorbox" style="background-color:#', colorCode, '"></span>' +
  166. '</a>' +
  167. '</td>' );
  168. }
  169. // Render the "More Colors" button.
  170. if ( config.colorButton_enableMore === undefined || config.colorButton_enableMore )
  171. {
  172. output.push(
  173. '</tr>' +
  174. '<tr>' +
  175. '<td colspan=8 align=center>' +
  176. '<a class="cke_colormore" _cke_focus=1 hidefocus=true' +
  177. ' title="', lang.more, '"' +
  178. ' onclick="CKEDITOR.tools.callFunction(', clickFn, ',\'?\',\'', type, '\');return false;"' +
  179. ' href="javascript:void(\'', lang.more, '\')"',
  180. ' role="option" aria-posinset="', total, '" aria-setsize="', total, '">',
  181. lang.more,
  182. '</a>' +
  183. '</td>' ); // tr is later in the code.
  184. }
  185. output.push( '</tr></table>' );
  186. return output.join( '' );
  187. }
  188. function isUnstylable( ele )
  189. {
  190. return ( ele.getAttribute( 'contentEditable' ) == 'false' ) || ele.getAttribute( 'data-nostyle' );
  191. }
  192. }
  193. });
  194. /**
  195. * Whether to enable the <strong>More Colors</strong> button in the color selectors.
  196. * @name CKEDITOR.config.colorButton_enableMore
  197. * @default <code>true</code>
  198. * @type Boolean
  199. * @example
  200. * config.colorButton_enableMore = false;
  201. */
  202. /**
  203. * Defines the colors to be displayed in the color selectors. This is a string
  204. * containing hexadecimal notation for HTML colors, without the "#" prefix.
  205. * <br /><br />
  206. * Since 3.3: A color name may optionally be defined by prefixing the entries with
  207. * a name and the slash character. For example, "FontColor1/FF9900" will be
  208. * displayed as the color #FF9900 in the selector, but will be output as "FontColor1".
  209. * @name CKEDITOR.config.colorButton_colors
  210. * @type String
  211. * @default <code>'000,800000,8B4513,2F4F4F,008080,000080,4B0082,696969,B22222,A52A2A,DAA520,006400,40E0D0,0000CD,800080,808080,F00,FF8C00,FFD700,008000,0FF,00F,EE82EE,A9A9A9,FFA07A,FFA500,FFFF00,00FF00,AFEEEE,ADD8E6,DDA0DD,D3D3D3,FFF0F5,FAEBD7,FFFFE0,F0FFF0,F0FFFF,F0F8FF,E6E6FA,FFF'</code>
  212. * @example
  213. * // Brazil colors only.
  214. * config.colorButton_colors = '00923E,F8C100,28166F';
  215. * @example
  216. * config.colorButton_colors = 'FontColor1/FF9900,FontColor2/0066CC,FontColor3/F00'
  217. */
  218. CKEDITOR.config.colorButton_colors =
  219. '000,800000,8B4513,2F4F4F,008080,000080,4B0082,696969,' +
  220. 'B22222,A52A2A,DAA520,006400,40E0D0,0000CD,800080,808080,' +
  221. 'F00,FF8C00,FFD700,008000,0FF,00F,EE82EE,A9A9A9,' +
  222. 'FFA07A,FFA500,FFFF00,00FF00,AFEEEE,ADD8E6,DDA0DD,D3D3D3,' +
  223. 'FFF0F5,FAEBD7,FFFFE0,F0FFF0,F0FFFF,F0F8FF,E6E6FA,FFF';
  224. /**
  225. * Stores the style definition that applies the text foreground color.
  226. * @name CKEDITOR.config.colorButton_foreStyle
  227. * @type Object
  228. * @default (see example)
  229. * @example
  230. * // This is actually the default value.
  231. * config.colorButton_foreStyle =
  232. * {
  233. * element : 'span',
  234. * styles : { 'color' : '#(color)' }
  235. * };
  236. */
  237. CKEDITOR.config.colorButton_foreStyle =
  238. {
  239. element : 'span',
  240. styles : { 'color' : '#(color)' },
  241. overrides : [ { element : 'font', attributes : { 'color' : null } } ]
  242. };
  243. /**
  244. * Stores the style definition that applies the text background color.
  245. * @name CKEDITOR.config.colorButton_backStyle
  246. * @type Object
  247. * @default (see example)
  248. * @example
  249. * // This is actually the default value.
  250. * config.colorButton_backStyle =
  251. * {
  252. * element : 'span',
  253. * styles : { 'background-color' : '#(color)' }
  254. * };
  255. */
  256. CKEDITOR.config.colorButton_backStyle =
  257. {
  258. element : 'span',
  259. styles : { 'background-color' : '#(color)' }
  260. };