2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
8 CKEDITOR.plugins.add( 'enterkey',
\r
10 requires : [ 'keystrokes', 'indent' ],
\r
12 init : function( editor )
\r
14 var specialKeys = editor.specialKeys;
\r
15 specialKeys[ 13 ] = enter;
\r
16 specialKeys[ CKEDITOR.SHIFT + 13 ] = shiftEnter;
\r
20 CKEDITOR.plugins.enterkey =
\r
22 enterBlock : function( editor, mode, range, forceMode )
\r
24 // Get the range for the current selection.
\r
25 range = range || getRange( editor );
\r
27 var doc = range.document;
\r
29 // Exit the list when we're inside an empty list item block. (#5376)
\r
30 if ( range.checkStartOfBlock() && range.checkEndOfBlock() )
\r
32 var path = new CKEDITOR.dom.elementPath( range.startContainer ),
\r
35 if ( block && ( block.is( 'li' ) || block.getParent().is( 'li' ) ) )
\r
37 editor.execCommand( 'outdent' );
\r
42 // Determine the block element to be used.
\r
43 var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
46 var splitInfo = range.splitBlock( blockTag );
\r
51 // Get the current blocks.
\r
52 var previousBlock = splitInfo.previousBlock,
\r
53 nextBlock = splitInfo.nextBlock;
\r
55 var isStartOfBlock = splitInfo.wasStartOfBlock,
\r
56 isEndOfBlock = splitInfo.wasEndOfBlock;
\r
60 // If this is a block under a list item, split it as well. (#1647)
\r
63 node = nextBlock.getParent();
\r
64 if ( node.is( 'li' ) )
\r
66 nextBlock.breakParent( node );
\r
67 nextBlock.move( nextBlock.getNext(), true );
\r
70 else if ( previousBlock && ( node = previousBlock.getParent() ) && node.is( 'li' ) )
\r
72 previousBlock.breakParent( node );
\r
73 range.moveToElementEditStart( previousBlock.getNext() );
\r
74 previousBlock.move( previousBlock.getPrevious() );
\r
77 // If we have both the previous and next blocks, it means that the
\r
78 // boundaries were on separated blocks, or none of them where on the
\r
79 // block limits (start/end).
\r
80 if ( !isStartOfBlock && !isEndOfBlock )
\r
82 // If the next block is an <li> with another list tree as the first
\r
83 // child, we'll need to append a filler (<br>/NBSP) or the list item
\r
84 // wouldn't be editable. (#1420)
\r
85 if ( nextBlock.is( 'li' )
\r
86 && ( node = nextBlock.getFirst( CKEDITOR.dom.walker.invisible( true ) ) )
\r
87 && node.is && node.is( 'ul', 'ol' ) )
\r
88 ( CKEDITOR.env.ie ? doc.createText( '\xa0' ) : doc.createElement( 'br' ) ).insertBefore( node );
\r
90 // Move the selection to the end block.
\r
92 range.moveToElementEditStart( nextBlock );
\r
98 if ( previousBlock )
\r
100 // Do not enter this block if it's a header tag, or we are in
\r
101 // a Shift+Enter (#77). Create a new block element instead
\r
102 // (later in the code).
\r
103 if ( previousBlock.is( 'li' ) || !headerTagRegex.test( previousBlock.getName() ) )
\r
105 // Otherwise, duplicate the previous block.
\r
106 newBlock = previousBlock.clone();
\r
109 else if ( nextBlock )
\r
110 newBlock = nextBlock.clone();
\r
113 newBlock = doc.createElement( blockTag );
\r
114 // Force the enter block unless we're talking of a list item.
\r
115 else if ( forceMode && !newBlock.is( 'li' ) )
\r
116 newBlock.renameNode( blockTag );
\r
118 // Recreate the inline elements tree, which was available
\r
119 // before hitting enter, so the same styles will be available in
\r
121 var elementPath = splitInfo.elementPath;
\r
124 for ( var i = 0, len = elementPath.elements.length ; i < len ; i++ )
\r
126 var element = elementPath.elements[ i ];
\r
128 if ( element.equals( elementPath.block ) || element.equals( elementPath.blockLimit ) )
\r
131 if ( CKEDITOR.dtd.$removeEmpty[ element.getName() ] )
\r
133 element = element.clone();
\r
134 newBlock.moveChildren( element );
\r
135 newBlock.append( element );
\r
140 if ( !CKEDITOR.env.ie )
\r
141 newBlock.appendBogus();
\r
143 range.insertNode( newBlock );
\r
145 // This is tricky, but to make the new block visible correctly
\r
146 // we must select it.
\r
147 // The previousBlock check has been included because it may be
\r
148 // empty if we have fixed a block-less space (like ENTER into an
\r
149 // empty table cell).
\r
150 if ( CKEDITOR.env.ie && isStartOfBlock && ( !isEndOfBlock || !previousBlock.getChildCount() ) )
\r
152 // Move the selection to the new block.
\r
153 range.moveToElementEditStart( isEndOfBlock ? previousBlock : newBlock );
\r
157 // Move the selection to the new block.
\r
158 range.moveToElementEditStart( isStartOfBlock && !isEndOfBlock ? nextBlock : newBlock );
\r
161 if ( !CKEDITOR.env.ie )
\r
165 // If we have split the block, adds a temporary span at the
\r
166 // range position and scroll relatively to it.
\r
167 var tmpNode = doc.createElement( 'span' );
\r
169 // We need some content for Safari.
\r
170 tmpNode.setHtml( ' ' );
\r
172 range.insertNode( tmpNode );
\r
173 tmpNode.scrollIntoView();
\r
174 range.deleteContents();
\r
178 // We may use the above scroll logic for the new block case
\r
179 // too, but it gives some weird result with Opera.
\r
180 newBlock.scrollIntoView();
\r
187 enterBr : function( editor, mode, range, forceMode )
\r
189 // Get the range for the current selection.
\r
190 range = range || getRange( editor );
\r
192 var doc = range.document;
\r
194 // Determine the block element to be used.
\r
195 var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
197 var isEndOfBlock = range.checkEndOfBlock();
\r
199 var elementPath = new CKEDITOR.dom.elementPath( editor.getSelection().getStartElement() );
\r
201 var startBlock = elementPath.block,
\r
202 startBlockTag = startBlock && elementPath.block.getName();
\r
206 if ( !forceMode && startBlockTag == 'li' )
\r
208 enterBlock( editor, mode, range, forceMode );
\r
212 // If we are at the end of a header block.
\r
213 if ( !forceMode && isEndOfBlock && headerTagRegex.test( startBlockTag ) )
\r
215 // Insert a <br> after the current paragraph.
\r
216 doc.createElement( 'br' ).insertAfter( startBlock );
\r
218 // A text node is required by Gecko only to make the cursor blink.
\r
219 if ( CKEDITOR.env.gecko )
\r
220 doc.createText( '' ).insertAfter( startBlock );
\r
222 // IE has different behaviors regarding position.
\r
223 range.setStartAt( startBlock.getNext(), CKEDITOR.env.ie ? CKEDITOR.POSITION_BEFORE_START : CKEDITOR.POSITION_AFTER_START );
\r
229 isPre = ( startBlockTag == 'pre' );
\r
231 // Gecko prefers <br> as line-break inside <pre> (#4711).
\r
232 if ( isPre && !CKEDITOR.env.gecko )
\r
233 lineBreak = doc.createText( CKEDITOR.env.ie ? '\r' : '\n' );
\r
235 lineBreak = doc.createElement( 'br' );
\r
237 range.deleteContents();
\r
238 range.insertNode( lineBreak );
\r
240 // A text node is required by Gecko only to make the cursor blink.
\r
241 // We need some text inside of it, so the bogus <br> is properly
\r
243 if ( !CKEDITOR.env.ie )
\r
244 doc.createText( '\ufeff' ).insertAfter( lineBreak );
\r
246 // If we are at the end of a block, we must be sure the bogus node is available in that block.
\r
247 if ( isEndOfBlock && !CKEDITOR.env.ie )
\r
248 lineBreak.getParent().appendBogus();
\r
250 // Now we can remove the text node contents, so the caret doesn't
\r
252 if ( !CKEDITOR.env.ie )
\r
253 lineBreak.getNext().$.nodeValue = '';
\r
254 // IE has different behavior regarding position.
\r
255 if ( CKEDITOR.env.ie )
\r
256 range.setStartAt( lineBreak, CKEDITOR.POSITION_AFTER_END );
\r
258 range.setStartAt( lineBreak.getNext(), CKEDITOR.POSITION_AFTER_START );
\r
260 // Scroll into view, for non IE.
\r
261 if ( !CKEDITOR.env.ie )
\r
265 // BR is not positioned in Opera and Webkit.
\r
266 if ( !CKEDITOR.env.gecko )
\r
268 dummy = doc.createElement( 'span' );
\r
269 // We need have some contents for Webkit to position it
\r
270 // under parent node. ( #3681)
\r
271 dummy.setHtml(' ');
\r
274 dummy = doc.createElement( 'br' );
\r
276 dummy.insertBefore( lineBreak.getNext() );
\r
277 dummy.scrollIntoView();
\r
282 // This collapse guarantees the cursor will be blinking.
\r
283 range.collapse( true );
\r
285 range.select( isPre );
\r
289 var plugin = CKEDITOR.plugins.enterkey,
\r
290 enterBr = plugin.enterBr,
\r
291 enterBlock = plugin.enterBlock,
\r
292 headerTagRegex = /^h[1-6]$/;
\r
294 function shiftEnter( editor )
\r
296 // Only effective within document.
\r
297 if ( editor.mode != 'wysiwyg' )
\r
301 // 1. We want to enforce the mode to be respected, instead
\r
302 // of cloning the current block. (#77)
\r
303 // 2. Always perform a block break when inside <pre> (#5402).
\r
304 if ( editor.getSelection().getStartElement().hasAscendant( 'pre', true ) )
\r
306 setTimeout( function() { enterBlock( editor, editor.config.enterMode, null, true ); }, 0 );
\r
310 return enter( editor, editor.config.shiftEnterMode, true );
\r
313 function enter( editor, mode, forceMode )
\r
315 forceMode = editor.config.forceEnterMode || forceMode;
\r
317 // Only effective within document.
\r
318 if ( editor.mode != 'wysiwyg' )
\r
322 mode = editor.config.enterMode;
\r
324 // Use setTimout so the keys get cancelled immediatelly.
\r
325 setTimeout( function()
\r
327 editor.fire( 'saveSnapshot' ); // Save undo step.
\r
328 if ( mode == CKEDITOR.ENTER_BR || editor.getSelection().getStartElement().hasAscendant( 'pre', true ) )
\r
329 enterBr( editor, mode, null, forceMode );
\r
331 enterBlock( editor, mode, null, forceMode );
\r
339 function getRange( editor )
\r
341 // Get the selection ranges.
\r
342 var ranges = editor.getSelection().getRanges();
\r
344 // Delete the contents of all ranges except the first one.
\r
345 for ( var i = ranges.length - 1 ; i > 0 ; i-- )
\r
347 ranges[ i ].deleteContents();
\r
350 // Return the first range.
\r
351 return ranges[ 0 ];
\r