JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[ckeditor.git] / _source / plugins / enterkey / plugin.js
diff --git a/_source/plugins/enterkey/plugin.js b/_source/plugins/enterkey/plugin.js
new file mode 100644 (file)
index 0000000..d171ae2
--- /dev/null
@@ -0,0 +1,324 @@
+/*\r
+Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+(function()\r
+{\r
+       CKEDITOR.plugins.add( 'enterkey',\r
+       {\r
+               requires : [ 'keystrokes', 'indent' ],\r
+\r
+               init : function( editor )\r
+               {\r
+                       var specialKeys = editor.specialKeys;\r
+                       specialKeys[ 13 ] = enter;\r
+                       specialKeys[ CKEDITOR.SHIFT + 13 ] = shiftEnter;\r
+               }\r
+       });\r
+\r
+       var forceMode,\r
+               headerTagRegex = /^h[1-6]$/;\r
+\r
+       function shiftEnter( editor )\r
+       {\r
+               // On SHIFT+ENTER we want to enforce the mode to be respected, instead\r
+               // of cloning the current block. (#77)\r
+               forceMode = 1;\r
+\r
+               return enter( editor, editor.config.shiftEnterMode );\r
+       }\r
+\r
+       function enter( editor, mode )\r
+       {\r
+               // Only effective within document.\r
+               if ( editor.mode != 'wysiwyg' )\r
+                       return false;\r
+\r
+               if ( !mode )\r
+                       mode = editor.config.enterMode;\r
+\r
+               // Use setTimout so the keys get cancelled immediatelly.\r
+               setTimeout( function()\r
+                       {\r
+                               editor.fire( 'saveSnapshot' );  // Save undo step.\r
+                               if ( mode == CKEDITOR.ENTER_BR || editor.getSelection().getStartElement().hasAscendant( 'pre', true ) )\r
+                                       enterBr( editor, mode );\r
+                               else\r
+                                       enterBlock( editor, mode );\r
+\r
+                               forceMode = 0;\r
+                       }, 0 );\r
+\r
+               return true;\r
+       }\r
+\r
+       function enterBlock( editor, mode, range )\r
+       {\r
+               // Get the range for the current selection.\r
+               range = range || getRange( editor );\r
+\r
+               var doc = range.document;\r
+\r
+               // Determine the block element to be used.\r
+               var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );\r
+\r
+               // Split the range.\r
+               var splitInfo = range.splitBlock( blockTag );\r
+\r
+               if ( !splitInfo )\r
+                       return;\r
+\r
+               // Get the current blocks.\r
+               var previousBlock       = splitInfo.previousBlock,\r
+                       nextBlock               = splitInfo.nextBlock;\r
+\r
+               var isStartOfBlock      = splitInfo.wasStartOfBlock,\r
+                       isEndOfBlock    = splitInfo.wasEndOfBlock;\r
+\r
+               var node;\r
+\r
+               // If this is a block under a list item, split it as well. (#1647)\r
+               if ( nextBlock )\r
+               {\r
+                       node = nextBlock.getParent();\r
+                       if ( node.is( 'li' ) )\r
+                       {\r
+                               nextBlock.breakParent( node );\r
+                               nextBlock.move( nextBlock.getNext(), true );\r
+                       }\r
+               }\r
+               else if ( previousBlock && ( node = previousBlock.getParent() ) && node.is( 'li' ) )\r
+               {\r
+                       previousBlock.breakParent( node );\r
+                       range.moveToElementEditStart( previousBlock.getNext() );\r
+                       previousBlock.move( previousBlock.getPrevious() );\r
+               }\r
+\r
+               // If we have both the previous and next blocks, it means that the\r
+               // boundaries were on separated blocks, or none of them where on the\r
+               // block limits (start/end).\r
+               if ( !isStartOfBlock && !isEndOfBlock )\r
+               {\r
+                       // If the next block is an <li> with another list tree as the first\r
+                       // child, we'll need to append a placeholder or the list item\r
+                       // wouldn't be editable. (#1420)\r
+                       if ( nextBlock.is( 'li' ) && ( node = nextBlock.getFirst() )\r
+                                       && node.is && node.is( 'ul', 'ol') )\r
+                               nextBlock.insertBefore( doc.createText( '\xa0' ), node );\r
+\r
+                       // Move the selection to the end block.\r
+                       if ( nextBlock )\r
+                               range.moveToElementEditStart( nextBlock );\r
+               }\r
+               else\r
+               {\r
+\r
+                       if ( isStartOfBlock && isEndOfBlock && previousBlock.is( 'li' ) )\r
+                       {\r
+                               editor.execCommand( 'outdent' );\r
+                               return;\r
+                       }\r
+\r
+                       var newBlock;\r
+\r
+                       if ( previousBlock )\r
+                       {\r
+                               // Do not enter this block if it's a header tag, or we are in\r
+                               // a Shift+Enter (#77). Create a new block element instead\r
+                               // (later in the code).\r
+                               if ( !forceMode && !headerTagRegex.test( previousBlock.getName() ) )\r
+                               {\r
+                                       // Otherwise, duplicate the previous block.\r
+                                       newBlock = previousBlock.clone();\r
+                               }\r
+                       }\r
+                       else if ( nextBlock )\r
+                               newBlock = nextBlock.clone();\r
+\r
+                       if ( !newBlock )\r
+                               newBlock = doc.createElement( blockTag );\r
+\r
+                       // Recreate the inline elements tree, which was available\r
+                       // before hitting enter, so the same styles will be available in\r
+                       // the new block.\r
+                       var elementPath = splitInfo.elementPath;\r
+                       if ( elementPath )\r
+                       {\r
+                               for ( var i = 0, len = elementPath.elements.length ; i < len ; i++ )\r
+                               {\r
+                                       var element = elementPath.elements[ i ];\r
+\r
+                                       if ( element.equals( elementPath.block ) || element.equals( elementPath.blockLimit ) )\r
+                                               break;\r
+\r
+                                       if ( CKEDITOR.dtd.$removeEmpty[ element.getName() ] )\r
+                                       {\r
+                                               element = element.clone();\r
+                                               newBlock.moveChildren( element );\r
+                                               newBlock.append( element );\r
+                                       }\r
+                               }\r
+                       }\r
+\r
+                       if ( !CKEDITOR.env.ie )\r
+                               newBlock.appendBogus();\r
+\r
+                       range.insertNode( newBlock );\r
+\r
+                       // This is tricky, but to make the new block visible correctly\r
+                       // we must select it.\r
+                       // The previousBlock check has been included because it may be\r
+                       // empty if we have fixed a block-less space (like ENTER into an\r
+                       // empty table cell).\r
+                       if ( CKEDITOR.env.ie && isStartOfBlock && ( !isEndOfBlock || !previousBlock.getChildCount() ) )\r
+                       {\r
+                               // Move the selection to the new block.\r
+                               range.moveToElementEditStart( isEndOfBlock ? previousBlock : newBlock );\r
+                               range.select();\r
+                       }\r
+\r
+                       // Move the selection to the new block.\r
+                       range.moveToElementEditStart( isStartOfBlock && !isEndOfBlock ? nextBlock : newBlock );\r
+               }\r
+\r
+               if ( !CKEDITOR.env.ie )\r
+               {\r
+                       if ( nextBlock )\r
+                       {\r
+                               // If we have split the block, adds a temporary span at the\r
+                               // range position and scroll relatively to it.\r
+                               var tmpNode = doc.createElement( 'span' );\r
+\r
+                               // We need some content for Safari.\r
+                               tmpNode.setHtml( '&nbsp;' );\r
+\r
+                               range.insertNode( tmpNode );\r
+                               tmpNode.scrollIntoView();\r
+                               range.deleteContents();\r
+                       }\r
+                       else\r
+                       {\r
+                               // We may use the above scroll logic for the new block case\r
+                               // too, but it gives some weird result with Opera.\r
+                               newBlock.scrollIntoView();\r
+                       }\r
+               }\r
+\r
+               range.select();\r
+       }\r
+\r
+       function enterBr( editor, mode )\r
+       {\r
+               // Get the range for the current selection.\r
+               var range = getRange( editor ),\r
+                       doc = range.document;\r
+\r
+               // Determine the block element to be used.\r
+               var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );\r
+\r
+               var isEndOfBlock = range.checkEndOfBlock();\r
+\r
+               var elementPath = new CKEDITOR.dom.elementPath( editor.getSelection().getStartElement() );\r
+\r
+               var startBlock = elementPath.block,\r
+                       startBlockTag = startBlock && elementPath.block.getName();\r
+\r
+               var isPre = false;\r
+\r
+               if ( !forceMode && startBlockTag == 'li' )\r
+               {\r
+                       enterBlock( editor, mode, range );\r
+                       return;\r
+               }\r
+\r
+               // If we are at the end of a header block.\r
+               if ( !forceMode && isEndOfBlock && headerTagRegex.test( startBlockTag ) )\r
+               {\r
+                       // Insert a <br> after the current paragraph.\r
+                       doc.createElement( 'br' ).insertAfter( startBlock );\r
+\r
+                       // A text node is required by Gecko only to make the cursor blink.\r
+                       if ( CKEDITOR.env.gecko )\r
+                               doc.createText( '' ).insertAfter( startBlock );\r
+\r
+                       // IE has different behaviors regarding position.\r
+                       range.setStartAt( startBlock.getNext(), CKEDITOR.env.ie ? CKEDITOR.POSITION_BEFORE_START : CKEDITOR.POSITION_AFTER_START );\r
+               }\r
+               else\r
+               {\r
+                       var lineBreak;\r
+\r
+                       isPre = ( startBlockTag == 'pre' );\r
+\r
+                       if ( isPre )\r
+                               lineBreak = doc.createText( CKEDITOR.env.ie ? '\r' : '\n' );\r
+                       else\r
+                               lineBreak = doc.createElement( 'br' );\r
+\r
+                       range.deleteContents();\r
+                       range.insertNode( lineBreak );\r
+\r
+                       // A text node is required by Gecko only to make the cursor blink.\r
+                       // We need some text inside of it, so the bogus <br> is properly\r
+                       // created.\r
+                       if ( !CKEDITOR.env.ie )\r
+                               doc.createText( '\ufeff' ).insertAfter( lineBreak );\r
+\r
+                       // If we are at the end of a block, we must be sure the bogus node is available in that block.\r
+                       if ( isEndOfBlock && !CKEDITOR.env.ie )\r
+                               lineBreak.getParent().appendBogus();\r
+\r
+                       // Now we can remove the text node contents, so the caret doesn't\r
+                       // stop on it.\r
+                       if ( !CKEDITOR.env.ie )\r
+                               lineBreak.getNext().$.nodeValue = '';\r
+                       // IE has different behavior regarding position.\r
+                       if ( CKEDITOR.env.ie )\r
+                               range.setStartAt( lineBreak, CKEDITOR.POSITION_AFTER_END );\r
+                       else\r
+                               range.setStartAt( lineBreak.getNext(), CKEDITOR.POSITION_AFTER_START );\r
+\r
+                       // Scroll into view, for non IE.\r
+                       if ( !CKEDITOR.env.ie )\r
+                       {\r
+                               var dummy = null;\r
+\r
+                               // BR is not positioned in Opera and Webkit.\r
+                               if ( !CKEDITOR.env.gecko )\r
+                               {\r
+                                       dummy = doc.createElement( 'span' );\r
+                                       // We need have some contents for Webkit to position it\r
+                                       // under parent node. ( #3681)\r
+                                       dummy.setHtml('&nbsp;');\r
+                               }\r
+                               else\r
+                                       dummy = doc.createElement( 'br' );\r
+\r
+                               dummy.insertBefore( lineBreak.getNext() );\r
+                               dummy.scrollIntoView();\r
+                               dummy.remove();\r
+                       }\r
+               }\r
+\r
+               // This collapse guarantees the cursor will be blinking.\r
+               range.collapse( true );\r
+\r
+               range.select( isPre );\r
+       }\r
+\r
+       function getRange( editor )\r
+       {\r
+               // Get the selection ranges.\r
+               var ranges = editor.getSelection().getRanges();\r
+\r
+               // Delete the contents of all ranges except the first one.\r
+               for ( var i = ranges.length - 1 ; i > 0 ; i-- )\r
+               {\r
+                       ranges[ i ].deleteContents();\r
+               }\r
+\r
+               // Return the first range.\r
+               return ranges[ 0 ];\r
+       }\r
+})();\r