plugin.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. * @file Justify commands.
  7. */
  8. (function()
  9. {
  10. function getState( editor, path )
  11. {
  12. var firstBlock = path.block || path.blockLimit;
  13. if ( !firstBlock || firstBlock.getName() == 'body' )
  14. return CKEDITOR.TRISTATE_OFF;
  15. return ( getAlignment( firstBlock, editor.config.useComputedState ) == this.value ) ?
  16. CKEDITOR.TRISTATE_ON :
  17. CKEDITOR.TRISTATE_OFF;
  18. }
  19. function getAlignment( element, useComputedState )
  20. {
  21. useComputedState = useComputedState === undefined || useComputedState;
  22. var align;
  23. if ( useComputedState )
  24. align = element.getComputedStyle( 'text-align' );
  25. else
  26. {
  27. while ( !element.hasAttribute || !( element.hasAttribute( 'align' ) || element.getStyle( 'text-align' ) ) )
  28. {
  29. var parent = element.getParent();
  30. if ( !parent )
  31. break;
  32. element = parent;
  33. }
  34. align = element.getStyle( 'text-align' ) || element.getAttribute( 'align' ) || '';
  35. }
  36. align && ( align = align.replace( /-moz-|-webkit-|start|auto/i, '' ) );
  37. !align && useComputedState && ( align = element.getComputedStyle( 'direction' ) == 'rtl' ? 'right' : 'left' );
  38. return align;
  39. }
  40. function onSelectionChange( evt )
  41. {
  42. if ( evt.editor.readOnly )
  43. return;
  44. var command = evt.editor.getCommand( this.name );
  45. command.state = getState.call( this, evt.editor, evt.data.path );
  46. command.fire( 'state' );
  47. }
  48. function justifyCommand( editor, name, value )
  49. {
  50. this.name = name;
  51. this.value = value;
  52. var classes = editor.config.justifyClasses;
  53. if ( classes )
  54. {
  55. switch ( value )
  56. {
  57. case 'left' :
  58. this.cssClassName = classes[0];
  59. break;
  60. case 'center' :
  61. this.cssClassName = classes[1];
  62. break;
  63. case 'right' :
  64. this.cssClassName = classes[2];
  65. break;
  66. case 'justify' :
  67. this.cssClassName = classes[3];
  68. break;
  69. }
  70. this.cssClassRegex = new RegExp( '(?:^|\\s+)(?:' + classes.join( '|' ) + ')(?=$|\\s)' );
  71. }
  72. }
  73. function onDirChanged( e )
  74. {
  75. var editor = e.editor;
  76. var range = new CKEDITOR.dom.range( editor.document );
  77. range.setStartBefore( e.data.node );
  78. range.setEndAfter( e.data.node );
  79. var walker = new CKEDITOR.dom.walker( range ),
  80. node;
  81. while ( ( node = walker.next() ) )
  82. {
  83. if ( node.type == CKEDITOR.NODE_ELEMENT )
  84. {
  85. // A child with the defined dir is to be ignored.
  86. if ( !node.equals( e.data.node ) && node.getDirection() )
  87. {
  88. range.setStartAfter( node );
  89. walker = new CKEDITOR.dom.walker( range );
  90. continue;
  91. }
  92. // Switch the alignment.
  93. var classes = editor.config.justifyClasses;
  94. if ( classes )
  95. {
  96. // The left align class.
  97. if ( node.hasClass( classes[ 0 ] ) )
  98. {
  99. node.removeClass( classes[ 0 ] );
  100. node.addClass( classes[ 2 ] );
  101. }
  102. // The right align class.
  103. else if ( node.hasClass( classes[ 2 ] ) )
  104. {
  105. node.removeClass( classes[ 2 ] );
  106. node.addClass( classes[ 0 ] );
  107. }
  108. }
  109. // Always switch CSS margins.
  110. var style = 'text-align';
  111. var align = node.getStyle( style );
  112. if ( align == 'left' )
  113. node.setStyle( style, 'right' );
  114. else if ( align == 'right' )
  115. node.setStyle( style, 'left' );
  116. }
  117. }
  118. }
  119. justifyCommand.prototype = {
  120. exec : function( editor )
  121. {
  122. var selection = editor.getSelection(),
  123. enterMode = editor.config.enterMode;
  124. if ( !selection )
  125. return;
  126. var bookmarks = selection.createBookmarks(),
  127. ranges = selection.getRanges( true );
  128. var cssClassName = this.cssClassName,
  129. iterator,
  130. block;
  131. var useComputedState = editor.config.useComputedState;
  132. useComputedState = useComputedState === undefined || useComputedState;
  133. for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
  134. {
  135. iterator = ranges[ i ].createIterator();
  136. iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;
  137. while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) )
  138. {
  139. block.removeAttribute( 'align' );
  140. block.removeStyle( 'text-align' );
  141. // Remove any of the alignment classes from the className.
  142. var className = cssClassName && ( block.$.className =
  143. CKEDITOR.tools.ltrim( block.$.className.replace( this.cssClassRegex, '' ) ) );
  144. var apply =
  145. ( this.state == CKEDITOR.TRISTATE_OFF ) &&
  146. ( !useComputedState || ( getAlignment( block, true ) != this.value ) );
  147. if ( cssClassName )
  148. {
  149. // Append the desired class name.
  150. if ( apply )
  151. block.addClass( cssClassName );
  152. else if ( !className )
  153. block.removeAttribute( 'class' );
  154. }
  155. else if ( apply )
  156. block.setStyle( 'text-align', this.value );
  157. }
  158. }
  159. editor.focus();
  160. editor.forceNextSelectionCheck();
  161. selection.selectBookmarks( bookmarks );
  162. }
  163. };
  164. CKEDITOR.plugins.add( 'justify',
  165. {
  166. init : function( editor )
  167. {
  168. var left = new justifyCommand( editor, 'justifyleft', 'left' ),
  169. center = new justifyCommand( editor, 'justifycenter', 'center' ),
  170. right = new justifyCommand( editor, 'justifyright', 'right' ),
  171. justify = new justifyCommand( editor, 'justifyblock', 'justify' );
  172. editor.addCommand( 'justifyleft', left );
  173. editor.addCommand( 'justifycenter', center );
  174. editor.addCommand( 'justifyright', right );
  175. editor.addCommand( 'justifyblock', justify );
  176. editor.ui.addButton( 'JustifyLeft',
  177. {
  178. label : editor.lang.justify.left,
  179. command : 'justifyleft'
  180. } );
  181. editor.ui.addButton( 'JustifyCenter',
  182. {
  183. label : editor.lang.justify.center,
  184. command : 'justifycenter'
  185. } );
  186. editor.ui.addButton( 'JustifyRight',
  187. {
  188. label : editor.lang.justify.right,
  189. command : 'justifyright'
  190. } );
  191. editor.ui.addButton( 'JustifyBlock',
  192. {
  193. label : editor.lang.justify.block,
  194. command : 'justifyblock'
  195. } );
  196. editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, left ) );
  197. editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, right ) );
  198. editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, center ) );
  199. editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, justify ) );
  200. editor.on( 'dirChanged', onDirChanged );
  201. },
  202. requires : [ 'domiterator' ]
  203. });
  204. })();
  205. /**
  206. * List of classes to use for aligning the contents. If it's null, no classes will be used
  207. * and instead the corresponding CSS values will be used. The array should contain 4 members, in the following order: left, center, right, justify.
  208. * @name CKEDITOR.config.justifyClasses
  209. * @type Array
  210. * @default null
  211. * @example
  212. * // Use the classes 'AlignLeft', 'AlignCenter', 'AlignRight', 'AlignJustify'
  213. * config.justifyClasses = [ 'AlignLeft', 'AlignCenter', 'AlignRight', 'AlignJustify' ];
  214. */