2 Copyright (c) 2003-2012, 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 editor.addCommand( 'enter', {
\r
15 modes : { wysiwyg:1 },
\r
16 editorFocus : false,
\r
17 exec : function( editor ){ enter( editor ); }
\r
20 editor.addCommand( 'shiftEnter', {
\r
21 modes : { wysiwyg:1 },
\r
22 editorFocus : false,
\r
23 exec : function( editor ){ shiftEnter( editor ); }
\r
26 var keystrokes = editor.keystrokeHandler.keystrokes;
\r
27 keystrokes[ 13 ] = 'enter';
\r
28 keystrokes[ CKEDITOR.SHIFT + 13 ] = 'shiftEnter';
\r
32 CKEDITOR.plugins.enterkey =
\r
34 enterBlock : function( editor, mode, range, forceMode )
\r
36 // Get the range for the current selection.
\r
37 range = range || getRange( editor );
\r
39 // We may not have valid ranges to work on, like when inside a
\r
40 // contenteditable=false element.
\r
44 var doc = range.document;
\r
46 var atBlockStart = range.checkStartOfBlock(),
\r
47 atBlockEnd = range.checkEndOfBlock(),
\r
48 path = new CKEDITOR.dom.elementPath( range.startContainer ),
\r
51 if ( atBlockStart && atBlockEnd )
\r
53 // Exit the list when we're inside an empty list item block. (#5376)
\r
54 if ( block && ( block.is( 'li' ) || block.getParent().is( 'li' ) ) )
\r
56 editor.execCommand( 'outdent' );
\r
60 if ( block && block.getParent().is( 'blockquote' ) )
\r
62 block.breakParent( block.getParent() );
\r
64 // If we were at the start of <blockquote>, there will be an empty element before it now.
\r
65 if ( !block.getPrevious().getFirst( CKEDITOR.dom.walker.invisible(1) ) )
\r
66 block.getPrevious().remove();
\r
68 // If we were at the end of <blockquote>, there will be an empty element after it now.
\r
69 if ( !block.getNext().getFirst( CKEDITOR.dom.walker.invisible(1) ) )
\r
70 block.getNext().remove();
\r
72 range.moveToElementEditStart( block );
\r
77 // Don't split <pre> if we're in the middle of it, act as shift enter key.
\r
78 else if ( block && block.is( 'pre' ) )
\r
82 enterBr( editor, mode, range, forceMode );
\r
86 // Don't split caption blocks. (#7944)
\r
87 else if ( block && CKEDITOR.dtd.$captionBlock[ block.getName() ] )
\r
89 enterBr( editor, mode, range, forceMode );
\r
93 // Determine the block element to be used.
\r
94 var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
97 var splitInfo = range.splitBlock( blockTag );
\r
102 // Get the current blocks.
\r
103 var previousBlock = splitInfo.previousBlock,
\r
104 nextBlock = splitInfo.nextBlock;
\r
106 var isStartOfBlock = splitInfo.wasStartOfBlock,
\r
107 isEndOfBlock = splitInfo.wasEndOfBlock;
\r
111 // If this is a block under a list item, split it as well. (#1647)
\r
114 node = nextBlock.getParent();
\r
115 if ( node.is( 'li' ) )
\r
117 nextBlock.breakParent( node );
\r
118 nextBlock.move( nextBlock.getNext(), 1 );
\r
121 else if ( previousBlock && ( node = previousBlock.getParent() ) && node.is( 'li' ) )
\r
123 previousBlock.breakParent( node );
\r
124 node = previousBlock.getNext();
\r
125 range.moveToElementEditStart( node );
\r
126 previousBlock.move( previousBlock.getPrevious() );
\r
129 // If we have both the previous and next blocks, it means that the
\r
130 // boundaries were on separated blocks, or none of them where on the
\r
131 // block limits (start/end).
\r
132 if ( !isStartOfBlock && !isEndOfBlock )
\r
134 // If the next block is an <li> with another list tree as the first
\r
135 // child, we'll need to append a filler (<br>/NBSP) or the list item
\r
136 // wouldn't be editable. (#1420)
\r
137 if ( nextBlock.is( 'li' )
\r
138 && ( node = nextBlock.getFirst( CKEDITOR.dom.walker.invisible( true ) ) )
\r
139 && node.is && node.is( 'ul', 'ol' ) )
\r
140 ( CKEDITOR.env.ie ? doc.createText( '\xa0' ) : doc.createElement( 'br' ) ).insertBefore( node );
\r
142 // Move the selection to the end block.
\r
144 range.moveToElementEditStart( nextBlock );
\r
151 if ( previousBlock )
\r
153 // Do not enter this block if it's a header tag, or we are in
\r
154 // a Shift+Enter (#77). Create a new block element instead
\r
155 // (later in the code).
\r
156 if ( previousBlock.is( 'li' ) ||
\r
157 ! ( headerTagRegex.test( previousBlock.getName() ) || previousBlock.is( 'pre' ) ) )
\r
159 // Otherwise, duplicate the previous block.
\r
160 newBlock = previousBlock.clone();
\r
163 else if ( nextBlock )
\r
164 newBlock = nextBlock.clone();
\r
168 // We have already created a new list item. (#6849)
\r
169 if ( node && node.is( 'li' ) )
\r
173 newBlock = doc.createElement( blockTag );
\r
174 if ( previousBlock && ( newBlockDir = previousBlock.getDirection() ) )
\r
175 newBlock.setAttribute( 'dir', newBlockDir );
\r
178 // Force the enter block unless we're talking of a list item.
\r
179 else if ( forceMode && !newBlock.is( 'li' ) )
\r
180 newBlock.renameNode( blockTag );
\r
182 // Recreate the inline elements tree, which was available
\r
183 // before hitting enter, so the same styles will be available in
\r
185 var elementPath = splitInfo.elementPath;
\r
188 for ( var i = 0, len = elementPath.elements.length ; i < len ; i++ )
\r
190 var element = elementPath.elements[ i ];
\r
192 if ( element.equals( elementPath.block ) || element.equals( elementPath.blockLimit ) )
\r
195 if ( CKEDITOR.dtd.$removeEmpty[ element.getName() ] )
\r
197 element = element.clone();
\r
198 newBlock.moveChildren( element );
\r
199 newBlock.append( element );
\r
204 if ( !CKEDITOR.env.ie )
\r
205 newBlock.appendBogus();
\r
207 if ( !newBlock.getParent() )
\r
208 range.insertNode( newBlock );
\r
210 // list item start number should not be duplicated (#7330), but we need
\r
211 // to remove the attribute after it's onto the DOM tree because of old IEs (#7581).
\r
212 newBlock.is( 'li' ) && newBlock.removeAttribute( 'value' );
\r
214 // This is tricky, but to make the new block visible correctly
\r
215 // we must select it.
\r
216 // The previousBlock check has been included because it may be
\r
217 // empty if we have fixed a block-less space (like ENTER into an
\r
218 // empty table cell).
\r
219 if ( CKEDITOR.env.ie && isStartOfBlock && ( !isEndOfBlock || !previousBlock.getChildCount() ) )
\r
221 // Move the selection to the new block.
\r
222 range.moveToElementEditStart( isEndOfBlock ? previousBlock : newBlock );
\r
226 // Move the selection to the new block.
\r
227 range.moveToElementEditStart( isStartOfBlock && !isEndOfBlock ? nextBlock : newBlock );
\r
230 if ( !CKEDITOR.env.ie )
\r
234 // If we have split the block, adds a temporary span at the
\r
235 // range position and scroll relatively to it.
\r
236 var tmpNode = doc.createElement( 'span' );
\r
238 // We need some content for Safari.
\r
239 tmpNode.setHtml( ' ' );
\r
241 range.insertNode( tmpNode );
\r
242 tmpNode.scrollIntoView();
\r
243 range.deleteContents();
\r
247 // We may use the above scroll logic for the new block case
\r
248 // too, but it gives some weird result with Opera.
\r
249 newBlock.scrollIntoView();
\r
256 enterBr : function( editor, mode, range, forceMode )
\r
258 // Get the range for the current selection.
\r
259 range = range || getRange( editor );
\r
261 // We may not have valid ranges to work on, like when inside a
\r
262 // contenteditable=false element.
\r
266 var doc = range.document;
\r
268 // Determine the block element to be used.
\r
269 var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
271 var isEndOfBlock = range.checkEndOfBlock();
\r
273 var elementPath = new CKEDITOR.dom.elementPath( editor.getSelection().getStartElement() );
\r
275 var startBlock = elementPath.block,
\r
276 startBlockTag = startBlock && elementPath.block.getName();
\r
280 if ( !forceMode && startBlockTag == 'li' )
\r
282 enterBlock( editor, mode, range, forceMode );
\r
286 // If we are at the end of a header block.
\r
287 if ( !forceMode && isEndOfBlock && headerTagRegex.test( startBlockTag ) )
\r
292 if ( ( newBlockDir = startBlock.getDirection() ) )
\r
294 newBlock = doc.createElement( 'div' );
\r
295 newBlock.setAttribute( 'dir', newBlockDir );
\r
296 newBlock.insertAfter( startBlock );
\r
297 range.setStart( newBlock, 0 );
\r
301 // Insert a <br> after the current paragraph.
\r
302 doc.createElement( 'br' ).insertAfter( startBlock );
\r
304 // A text node is required by Gecko only to make the cursor blink.
\r
305 if ( CKEDITOR.env.gecko )
\r
306 doc.createText( '' ).insertAfter( startBlock );
\r
308 // IE has different behaviors regarding position.
\r
309 range.setStartAt( startBlock.getNext(), CKEDITOR.env.ie ? CKEDITOR.POSITION_BEFORE_START : CKEDITOR.POSITION_AFTER_START );
\r
316 isPre = ( startBlockTag == 'pre' );
\r
318 // Gecko prefers <br> as line-break inside <pre> (#4711).
\r
319 if ( isPre && !CKEDITOR.env.gecko )
\r
320 lineBreak = doc.createText( CKEDITOR.env.ie ? '\r' : '\n' );
\r
322 lineBreak = doc.createElement( 'br' );
\r
324 range.deleteContents();
\r
325 range.insertNode( lineBreak );
\r
327 // IE has different behavior regarding position.
\r
328 if ( CKEDITOR.env.ie )
\r
329 range.setStartAt( lineBreak, CKEDITOR.POSITION_AFTER_END );
\r
332 // A text node is required by Gecko only to make the cursor blink.
\r
333 // We need some text inside of it, so the bogus <br> is properly
\r
335 doc.createText( '\ufeff' ).insertAfter( lineBreak );
\r
337 // If we are at the end of a block, we must be sure the bogus node is available in that block.
\r
338 if ( isEndOfBlock )
\r
339 lineBreak.getParent().appendBogus();
\r
341 // Now we can remove the text node contents, so the caret doesn't
\r
343 lineBreak.getNext().$.nodeValue = '';
\r
345 range.setStartAt( lineBreak.getNext(), CKEDITOR.POSITION_AFTER_START );
\r
347 // Scroll into view, for non IE.
\r
350 // BR is not positioned in Opera and Webkit.
\r
351 if ( !CKEDITOR.env.gecko )
\r
353 dummy = doc.createElement( 'span' );
\r
354 // We need have some contents for Webkit to position it
\r
355 // under parent node. ( #3681)
\r
356 dummy.setHtml(' ');
\r
359 dummy = doc.createElement( 'br' );
\r
361 dummy.insertBefore( lineBreak.getNext() );
\r
362 dummy.scrollIntoView();
\r
367 // This collapse guarantees the cursor will be blinking.
\r
368 range.collapse( true );
\r
370 range.select( isPre );
\r
374 var plugin = CKEDITOR.plugins.enterkey,
\r
375 enterBr = plugin.enterBr,
\r
376 enterBlock = plugin.enterBlock,
\r
377 headerTagRegex = /^h[1-6]$/;
\r
379 function shiftEnter( editor )
\r
381 // Only effective within document.
\r
382 if ( editor.mode != 'wysiwyg' )
\r
386 // 1. We want to enforce the mode to be respected, instead
\r
387 // of cloning the current block. (#77)
\r
388 return enter( editor, editor.config.shiftEnterMode, 1 );
\r
391 function enter( editor, mode, forceMode )
\r
393 forceMode = editor.config.forceEnterMode || forceMode;
\r
395 // Only effective within document.
\r
396 if ( editor.mode != 'wysiwyg' )
\r
400 mode = editor.config.enterMode;
\r
402 // Use setTimout so the keys get cancelled immediatelly.
\r
403 setTimeout( function()
\r
405 editor.fire( 'saveSnapshot' ); // Save undo step.
\r
407 if ( mode == CKEDITOR.ENTER_BR )
\r
408 enterBr( editor, mode, null, forceMode );
\r
410 enterBlock( editor, mode, null, forceMode );
\r
412 editor.fire( 'saveSnapshot' );
\r
419 function getRange( editor )
\r
421 // Get the selection ranges.
\r
422 var ranges = editor.getSelection().getRanges( true );
\r
424 // Delete the contents of all ranges except the first one.
\r
425 for ( var i = ranges.length - 1 ; i > 0 ; i-- )
\r
427 ranges[ i ].deleteContents();
\r
430 // Return the first range.
\r
431 return ranges[ 0 ];
\r