2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @file Increse and decrease indent commands.
\r
12 var listNodeNames = { ol : 1, ul : 1 },
\r
13 isNotWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),
\r
14 isNotBookmark = CKEDITOR.dom.walker.bookmark( false, true );
\r
16 function onSelectionChange( evt )
\r
18 var editor = evt.editor,
\r
19 elementPath = evt.data.path,
\r
20 list = elementPath && elementPath.contains( listNodeNames ),
\r
21 firstBlock = elementPath.block || elementPath.blockLimit;
\r
24 return this.setState( CKEDITOR.TRISTATE_OFF );
\r
26 if ( !this.useIndentClasses && this.name == 'indent' )
\r
27 return this.setState( CKEDITOR.TRISTATE_OFF );
\r
30 return this.setState( CKEDITOR.TRISTATE_DISABLED );
\r
32 if ( this.useIndentClasses )
\r
34 var indentClass = firstBlock.$.className.match( this.classNameRegex ),
\r
38 indentClass = indentClass[1];
\r
39 indentStep = this.indentClassMap[ indentClass ];
\r
41 if ( ( this.name == 'outdent' && !indentStep ) ||
\r
42 ( this.name == 'indent' && indentStep == editor.config.indentClasses.length ) )
\r
43 return this.setState( CKEDITOR.TRISTATE_DISABLED );
\r
44 return this.setState( CKEDITOR.TRISTATE_OFF );
\r
48 var indent = parseInt( firstBlock.getStyle( getIndentCssProperty( firstBlock ) ), 10 );
\r
49 if ( isNaN( indent ) )
\r
52 return this.setState( CKEDITOR.TRISTATE_DISABLED );
\r
53 return this.setState( CKEDITOR.TRISTATE_OFF );
\r
57 function indentCommand( editor, name )
\r
60 this.useIndentClasses = editor.config.indentClasses && editor.config.indentClasses.length > 0;
\r
61 if ( this.useIndentClasses )
\r
63 this.classNameRegex = new RegExp( '(?:^|\\s+)(' + editor.config.indentClasses.join( '|' ) + ')(?=$|\\s)' );
\r
64 this.indentClassMap = {};
\r
65 for ( var i = 0 ; i < editor.config.indentClasses.length ; i++ )
\r
66 this.indentClassMap[ editor.config.indentClasses[i] ] = i + 1;
\r
69 this.startDisabled = name == 'outdent';
\r
72 // Returns the CSS property to be used for identing a given element.
\r
73 function getIndentCssProperty( element, dir )
\r
75 return ( dir || element.getComputedStyle( 'direction' ) ) == 'ltr' ? 'margin-left' : 'margin-right';
\r
78 function isListItem( node )
\r
80 return node.type = CKEDITOR.NODE_ELEMENT && node.is( 'li' );
\r
83 indentCommand.prototype = {
\r
84 exec : function( editor )
\r
86 var self = this, database = {};
\r
88 function indentList( listNode )
\r
90 // Our starting and ending points of the range might be inside some blocks under a list item...
\r
91 // So before playing with the iterator, we need to expand the block to include the list items.
\r
92 var startContainer = range.startContainer,
\r
93 endContainer = range.endContainer;
\r
94 while ( startContainer && !startContainer.getParent().equals( listNode ) )
\r
95 startContainer = startContainer.getParent();
\r
96 while ( endContainer && !endContainer.getParent().equals( listNode ) )
\r
97 endContainer = endContainer.getParent();
\r
99 if ( !startContainer || !endContainer )
\r
102 // Now we can iterate over the individual items on the same tree depth.
\r
103 var block = startContainer,
\r
106 while ( !stopFlag )
\r
108 if ( block.equals( endContainer ) )
\r
110 itemsToMove.push( block );
\r
111 block = block.getNext();
\r
113 if ( itemsToMove.length < 1 )
\r
116 // Do indent or outdent operations on the array model of the list, not the
\r
117 // list's DOM tree itself. The array model demands that it knows as much as
\r
118 // possible about the surrounding lists, we need to feed it the further
\r
119 // ancestor node that is still a list.
\r
120 var listParents = listNode.getParents( true );
\r
121 for ( var i = 0 ; i < listParents.length ; i++ )
\r
123 if ( listParents[i].getName && listNodeNames[ listParents[i].getName() ] )
\r
125 listNode = listParents[i];
\r
129 var indentOffset = self.name == 'indent' ? 1 : -1,
\r
130 startItem = itemsToMove[0],
\r
131 lastItem = itemsToMove[ itemsToMove.length - 1 ];
\r
133 // Convert the list DOM tree into a one dimensional array.
\r
134 var listArray = CKEDITOR.plugins.list.listToArray( listNode, database );
\r
136 // Apply indenting or outdenting on the array.
\r
137 var baseIndent = listArray[ lastItem.getCustomData( 'listarray_index' ) ].indent;
\r
138 for ( i = startItem.getCustomData( 'listarray_index' ); i <= lastItem.getCustomData( 'listarray_index' ); i++ )
\r
140 listArray[ i ].indent += indentOffset;
\r
141 // Make sure the newly created sublist get a brand-new element of the same type. (#5372)
\r
142 var listRoot = listArray[ i ].parent;
\r
143 listArray[ i ].parent = new CKEDITOR.dom.element( listRoot.getName(), listRoot.getDocument() );
\r
146 for ( i = lastItem.getCustomData( 'listarray_index' ) + 1 ;
\r
147 i < listArray.length && listArray[i].indent > baseIndent ; i++ )
\r
148 listArray[i].indent += indentOffset;
\r
150 // Convert the array back to a DOM forest (yes we might have a few subtrees now).
\r
151 // And replace the old list with the new forest.
\r
152 var newList = CKEDITOR.plugins.list.arrayToList( listArray, database, null, editor.config.enterMode, listNode.getDirection() );
\r
154 // Avoid nested <li> after outdent even they're visually same,
\r
155 // recording them for later refactoring.(#3982)
\r
156 if ( self.name == 'outdent' )
\r
158 var parentLiElement;
\r
159 if ( ( parentLiElement = listNode.getParent() ) && parentLiElement.is( 'li' ) )
\r
161 var children = newList.listNode.getChildren(),
\r
163 count = children.count(),
\r
166 for ( i = count - 1 ; i >= 0 ; i-- )
\r
168 if ( ( child = children.getItem( i ) ) && child.is && child.is( 'li' ) )
\r
169 pendingLis.push( child );
\r
175 newList.listNode.replace( listNode );
\r
177 // Move the nested <li> to be appeared after the parent.
\r
178 if ( pendingLis && pendingLis.length )
\r
180 for ( i = 0; i < pendingLis.length ; i++ )
\r
182 var li = pendingLis[ i ],
\r
183 followingList = li;
\r
185 // Nest preceding <ul>/<ol> inside current <li> if any.
\r
186 while ( ( followingList = followingList.getNext() ) &&
\r
187 followingList.is &&
\r
188 followingList.getName() in listNodeNames )
\r
190 // IE requires a filler NBSP for nested list inside empty list item,
\r
191 // otherwise the list item will be inaccessiable. (#4476)
\r
192 if ( CKEDITOR.env.ie && !li.getFirst( function( node ){ return isNotWhitespaces( node ) && isNotBookmark( node ); } ) )
\r
193 li.append( range.document.createText( '\u00a0' ) );
\r
195 li.append( followingList );
\r
198 li.insertAfter( parentLiElement );
\r
203 function indentBlock()
\r
205 var iterator = range.createIterator(),
\r
206 enterMode = editor.config.enterMode;
\r
207 iterator.enforceRealBlocks = true;
\r
208 iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;
\r
210 while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) )
\r
211 indentElement( block );
\r
214 function indentElement( element, dir )
\r
216 if ( element.getCustomData( 'indent_processed' ) )
\r
219 if ( self.useIndentClasses )
\r
221 // Transform current class name to indent step index.
\r
222 var indentClass = element.$.className.match( self.classNameRegex ),
\r
226 indentClass = indentClass[1];
\r
227 indentStep = self.indentClassMap[ indentClass ];
\r
230 // Operate on indent step index, transform indent step index back to class
\r
232 if ( self.name == 'outdent' )
\r
237 if ( indentStep < 0 )
\r
240 indentStep = Math.min( indentStep, editor.config.indentClasses.length );
\r
241 indentStep = Math.max( indentStep, 0 );
\r
242 element.$.className = CKEDITOR.tools.ltrim( element.$.className.replace( self.classNameRegex, '' ) );
\r
243 if ( indentStep > 0 )
\r
244 element.addClass( editor.config.indentClasses[ indentStep - 1 ] );
\r
248 var indentCssProperty = getIndentCssProperty( element, dir ),
\r
249 currentOffset = parseInt( element.getStyle( indentCssProperty ), 10 );
\r
250 if ( isNaN( currentOffset ) )
\r
252 var indentOffset = editor.config.indentOffset || 40;
\r
253 currentOffset += ( self.name == 'indent' ? 1 : -1 ) * indentOffset;
\r
255 if ( currentOffset < 0 )
\r
258 currentOffset = Math.max( currentOffset, 0 );
\r
259 currentOffset = Math.ceil( currentOffset / indentOffset ) * indentOffset;
\r
260 element.setStyle( indentCssProperty, currentOffset ? currentOffset + ( editor.config.indentUnit || 'px' ) : '' );
\r
261 if ( element.getAttribute( 'style' ) === '' )
\r
262 element.removeAttribute( 'style' );
\r
265 CKEDITOR.dom.element.setMarker( database, element, 'indent_processed', 1 );
\r
269 var selection = editor.getSelection(),
\r
270 bookmarks = selection.createBookmarks( 1 ),
\r
271 ranges = selection && selection.getRanges( 1 ),
\r
275 var iterator = ranges.createIterator();
\r
276 while ( ( range = iterator.getNextRange() ) )
\r
278 var rangeRoot = range.getCommonAncestor(),
\r
279 nearestListBlock = rangeRoot;
\r
281 while ( nearestListBlock && !( nearestListBlock.type == CKEDITOR.NODE_ELEMENT &&
\r
282 listNodeNames[ nearestListBlock.getName() ] ) )
\r
283 nearestListBlock = nearestListBlock.getParent();
\r
285 // Avoid having selection enclose the entire list. (#6138)
\r
286 // [<ul><li>...</li></ul>] =><ul><li>[...]</li></ul>
\r
287 if ( !nearestListBlock )
\r
289 var selectedNode = range.getEnclosedNode();
\r
291 && selectedNode.type == CKEDITOR.NODE_ELEMENT
\r
292 && selectedNode.getName() in listNodeNames)
\r
294 range.setStartAt( selectedNode, CKEDITOR.POSITION_AFTER_START );
\r
295 range.setEndAt( selectedNode, CKEDITOR.POSITION_BEFORE_END );
\r
296 nearestListBlock = selectedNode;
\r
300 // Avoid selection anchors under list root.
\r
301 // <ul>[<li>...</li>]</ul> => <ul><li>[...]</li></ul>
\r
302 if ( nearestListBlock && range.startContainer.type == CKEDITOR.NODE_ELEMENT
\r
303 && range.startContainer.getName() in listNodeNames )
\r
305 var walker = new CKEDITOR.dom.walker( range );
\r
306 walker.evaluator = isListItem;
\r
307 range.startContainer = walker.next();
\r
310 if ( nearestListBlock && range.endContainer.type == CKEDITOR.NODE_ELEMENT
\r
311 && range.endContainer.getName() in listNodeNames )
\r
313 walker = new CKEDITOR.dom.walker( range );
\r
314 walker.evaluator = isListItem;
\r
315 range.endContainer = walker.previous();
\r
318 if ( nearestListBlock )
\r
320 var firstListItem = nearestListBlock.getFirst( isListItem ),
\r
321 hasMultipleItems = !!firstListItem.getNext( isListItem ),
\r
322 rangeStart = range.startContainer,
\r
323 indentWholeList = firstListItem.equals( rangeStart ) || firstListItem.contains( rangeStart );
\r
325 // Indent the entire list if cursor is inside the first list item. (#3893)
\r
326 // Only do that for indenting or when using indent classes or when there is something to outdent. (#6141)
\r
327 if ( !( indentWholeList &&
\r
328 ( self.name == 'indent' || self.useIndentClasses || parseInt( nearestListBlock.getStyle( getIndentCssProperty( nearestListBlock ) ), 10 ) ) &&
\r
329 indentElement( nearestListBlock, !hasMultipleItems && firstListItem.getDirection() ) ) )
\r
330 indentList( nearestListBlock );
\r
336 // Clean up the markers.
\r
337 CKEDITOR.dom.element.clearAllMarkers( database );
\r
339 editor.forceNextSelectionCheck();
\r
340 selection.selectBookmarks( bookmarks );
\r
344 CKEDITOR.plugins.add( 'indent',
\r
346 init : function( editor )
\r
348 // Register commands.
\r
349 var indent = editor.addCommand( 'indent', new indentCommand( editor, 'indent' ) ),
\r
350 outdent = editor.addCommand( 'outdent', new indentCommand( editor, 'outdent' ) );
\r
352 // Register the toolbar buttons.
\r
353 editor.ui.addButton( 'Indent',
\r
355 label : editor.lang.indent,
\r
358 editor.ui.addButton( 'Outdent',
\r
360 label : editor.lang.outdent,
\r
361 command : 'outdent'
\r
364 // Register the state changing handlers.
\r
365 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, indent ) );
\r
366 editor.on( 'selectionChange', CKEDITOR.tools.bind( onSelectionChange, outdent ) );
\r
368 // [IE6/7] Raw lists are using margin instead of padding for visual indentation in wysiwyg mode. (#3893)
\r
369 if ( CKEDITOR.env.ie6Compat || CKEDITOR.env.ie7Compat )
\r
374 " margin-left: 0px;" +
\r
375 " padding-left: 40px;" +
\r
379 // Register dirChanged listener.
\r
380 editor.on( 'dirChanged', function( e )
\r
382 var range = new CKEDITOR.dom.range( editor.document );
\r
383 range.setStartBefore( e.data.node );
\r
384 range.setEndAfter( e.data.node );
\r
386 var walker = new CKEDITOR.dom.walker( range ),
\r
389 while ( ( node = walker.next() ) )
\r
391 if ( node.type == CKEDITOR.NODE_ELEMENT )
\r
393 // A child with the defined dir is to be ignored.
\r
394 if ( !node.equals( e.data.node ) && node.getDirection() )
\r
396 range.setStartAfter( node );
\r
397 walker = new CKEDITOR.dom.walker( range );
\r
401 // Switch alignment classes.
\r
402 var classes = editor.config.indentClasses;
\r
405 var suffix = ( e.data.dir == 'ltr' ) ? [ '_rtl', '' ] : [ '', '_rtl' ];
\r
406 for ( var i = 0; i < classes.length; i++ )
\r
408 if ( node.hasClass( classes[ i ] + suffix[ 0 ] ) )
\r
410 node.removeClass( classes[ i ] + suffix[ 0 ] );
\r
411 node.addClass( classes[ i ] + suffix[ 1 ] );
\r
416 // Switch the margins.
\r
417 var marginLeft = node.getStyle( 'margin-right' ),
\r
418 marginRight = node.getStyle( 'margin-left' );
\r
420 marginLeft ? node.setStyle( 'margin-left', marginLeft ) : node.removeStyle( 'margin-left' );
\r
421 marginRight ? node.setStyle( 'margin-right', marginRight ) : node.removeStyle( 'margin-right' );
\r
427 requires : [ 'domiterator', 'list' ]
\r
432 * Size of each indentation step
\r
433 * @name CKEDITOR.config.indentOffset
\r
437 * config.indentOffset = 4;
\r
441 * Unit for the indentation style
\r
442 * @name CKEDITOR.config.indentUnit
\r
446 * config.indentUnit = 'em';
\r
450 * List of classes to use for indenting the contents. If it's null, no classes will be used
\r
451 * and instead the {@link #indentUnit} and {@link #indentOffset} properties will be used.
\r
452 * @name CKEDITOR.config.indentClasses
\r
456 * // Use the classes 'Indent1', 'Indent2', 'Indent3'
\r
457 * config.indentClasses = ['Indent1', 'Indent2', 'Indent3'];
\r