JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
28dc2443dfb80b005c217c583e4196284eede79e
[ckeditor.git] / _source / plugins / enterkey / plugin.js
1 /*\r
2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 (function()\r
7 {\r
8         CKEDITOR.plugins.add( 'enterkey',\r
9         {\r
10                 requires : [ 'keystrokes', 'indent' ],\r
11 \r
12                 init : function( editor )\r
13                 {\r
14                         var specialKeys = editor.specialKeys;\r
15                         specialKeys[ 13 ] = enter;\r
16                         specialKeys[ CKEDITOR.SHIFT + 13 ] = shiftEnter;\r
17                 }\r
18         });\r
19 \r
20         var forceMode,\r
21                 headerTagRegex = /^h[1-6]$/;\r
22 \r
23         function shiftEnter( editor )\r
24         {\r
25                 // On SHIFT+ENTER we want to enforce the mode to be respected, instead\r
26                 // of cloning the current block. (#77)\r
27                 forceMode = 1;\r
28 \r
29                 return enter( editor, editor.config.shiftEnterMode );\r
30         }\r
31 \r
32         function enter( editor, mode )\r
33         {\r
34                 // Only effective within document.\r
35                 if ( editor.mode != 'wysiwyg' )\r
36                         return false;\r
37 \r
38                 if ( !mode )\r
39                         mode = editor.config.enterMode;\r
40 \r
41                 // Use setTimout so the keys get cancelled immediatelly.\r
42                 setTimeout( function()\r
43                         {\r
44                                 editor.fire( 'saveSnapshot' );  // Save undo step.\r
45                                 if ( mode == CKEDITOR.ENTER_BR || editor.getSelection().getStartElement().hasAscendant( 'pre', true ) )\r
46                                         enterBr( editor, mode );\r
47                                 else\r
48                                         enterBlock( editor, mode );\r
49 \r
50                                 forceMode = 0;\r
51                         }, 0 );\r
52 \r
53                 return true;\r
54         }\r
55 \r
56         function enterBlock( editor, mode, range )\r
57         {\r
58                 // Get the range for the current selection.\r
59                 range = range || getRange( editor );\r
60 \r
61                 var doc = range.document;\r
62 \r
63                 // Determine the block element to be used.\r
64                 var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );\r
65 \r
66                 // Split the range.\r
67                 var splitInfo = range.splitBlock( blockTag );\r
68 \r
69                 if ( !splitInfo )\r
70                         return;\r
71 \r
72                 // Get the current blocks.\r
73                 var previousBlock       = splitInfo.previousBlock,\r
74                         nextBlock               = splitInfo.nextBlock;\r
75 \r
76                 var isStartOfBlock      = splitInfo.wasStartOfBlock,\r
77                         isEndOfBlock    = splitInfo.wasEndOfBlock;\r
78 \r
79                 var node;\r
80 \r
81                 // If this is a block under a list item, split it as well. (#1647)\r
82                 if ( nextBlock )\r
83                 {\r
84                         node = nextBlock.getParent();\r
85                         if ( node.is( 'li' ) )\r
86                         {\r
87                                 nextBlock.breakParent( node );\r
88                                 nextBlock.move( nextBlock.getNext(), true );\r
89                         }\r
90                 }\r
91                 else if ( previousBlock && ( node = previousBlock.getParent() ) && node.is( 'li' ) )\r
92                 {\r
93                         previousBlock.breakParent( node );\r
94                         range.moveToElementEditStart( previousBlock.getNext() );\r
95                         previousBlock.move( previousBlock.getPrevious() );\r
96                 }\r
97 \r
98                 // If we have both the previous and next blocks, it means that the\r
99                 // boundaries were on separated blocks, or none of them where on the\r
100                 // block limits (start/end).\r
101                 if ( !isStartOfBlock && !isEndOfBlock )\r
102                 {\r
103                         // If the next block is an <li> with another list tree as the first\r
104                         // child, we'll need to append a filler (<br>/NBSP) or the list item\r
105                         // wouldn't be editable. (#1420)\r
106                         if ( nextBlock.is( 'li' )\r
107                                  && ( node = nextBlock.getFirst( CKEDITOR.dom.walker.invisible( true ) ) )\r
108                                  && node.is && node.is( 'ul', 'ol' ) )\r
109                                 ( CKEDITOR.env.ie ? doc.createText( '\xa0' ) : doc.createElement( 'br' ) ).insertBefore( node );\r
110 \r
111                         // Move the selection to the end block.\r
112                         if ( nextBlock )\r
113                                 range.moveToElementEditStart( nextBlock );\r
114                 }\r
115                 else\r
116                 {\r
117 \r
118                         if ( isStartOfBlock && isEndOfBlock && previousBlock.is( 'li' ) )\r
119                         {\r
120                                 editor.execCommand( 'outdent' );\r
121                                 return;\r
122                         }\r
123 \r
124                         var newBlock;\r
125 \r
126                         if ( previousBlock )\r
127                         {\r
128                                 // Do not enter this block if it's a header tag, or we are in\r
129                                 // a Shift+Enter (#77). Create a new block element instead\r
130                                 // (later in the code).\r
131                                 if ( !forceMode && !headerTagRegex.test( previousBlock.getName() ) )\r
132                                 {\r
133                                         // Otherwise, duplicate the previous block.\r
134                                         newBlock = previousBlock.clone();\r
135                                 }\r
136                         }\r
137                         else if ( nextBlock )\r
138                                 newBlock = nextBlock.clone();\r
139 \r
140                         if ( !newBlock )\r
141                                 newBlock = doc.createElement( blockTag );\r
142 \r
143                         // Recreate the inline elements tree, which was available\r
144                         // before hitting enter, so the same styles will be available in\r
145                         // the new block.\r
146                         var elementPath = splitInfo.elementPath;\r
147                         if ( elementPath )\r
148                         {\r
149                                 for ( var i = 0, len = elementPath.elements.length ; i < len ; i++ )\r
150                                 {\r
151                                         var element = elementPath.elements[ i ];\r
152 \r
153                                         if ( element.equals( elementPath.block ) || element.equals( elementPath.blockLimit ) )\r
154                                                 break;\r
155 \r
156                                         if ( CKEDITOR.dtd.$removeEmpty[ element.getName() ] )\r
157                                         {\r
158                                                 element = element.clone();\r
159                                                 newBlock.moveChildren( element );\r
160                                                 newBlock.append( element );\r
161                                         }\r
162                                 }\r
163                         }\r
164 \r
165                         if ( !CKEDITOR.env.ie )\r
166                                 newBlock.appendBogus();\r
167 \r
168                         range.insertNode( newBlock );\r
169 \r
170                         // This is tricky, but to make the new block visible correctly\r
171                         // we must select it.\r
172                         // The previousBlock check has been included because it may be\r
173                         // empty if we have fixed a block-less space (like ENTER into an\r
174                         // empty table cell).\r
175                         if ( CKEDITOR.env.ie && isStartOfBlock && ( !isEndOfBlock || !previousBlock.getChildCount() ) )\r
176                         {\r
177                                 // Move the selection to the new block.\r
178                                 range.moveToElementEditStart( isEndOfBlock ? previousBlock : newBlock );\r
179                                 range.select();\r
180                         }\r
181 \r
182                         // Move the selection to the new block.\r
183                         range.moveToElementEditStart( isStartOfBlock && !isEndOfBlock ? nextBlock : newBlock );\r
184                 }\r
185 \r
186                 if ( !CKEDITOR.env.ie )\r
187                 {\r
188                         if ( nextBlock )\r
189                         {\r
190                                 // If we have split the block, adds a temporary span at the\r
191                                 // range position and scroll relatively to it.\r
192                                 var tmpNode = doc.createElement( 'span' );\r
193 \r
194                                 // We need some content for Safari.\r
195                                 tmpNode.setHtml( '&nbsp;' );\r
196 \r
197                                 range.insertNode( tmpNode );\r
198                                 tmpNode.scrollIntoView();\r
199                                 range.deleteContents();\r
200                         }\r
201                         else\r
202                         {\r
203                                 // We may use the above scroll logic for the new block case\r
204                                 // too, but it gives some weird result with Opera.\r
205                                 newBlock.scrollIntoView();\r
206                         }\r
207                 }\r
208 \r
209                 range.select();\r
210         }\r
211 \r
212         function enterBr( editor, mode )\r
213         {\r
214                 // Get the range for the current selection.\r
215                 var range = getRange( editor ),\r
216                         doc = range.document;\r
217 \r
218                 // Determine the block element to be used.\r
219                 var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );\r
220 \r
221                 var isEndOfBlock = range.checkEndOfBlock();\r
222 \r
223                 var elementPath = new CKEDITOR.dom.elementPath( editor.getSelection().getStartElement() );\r
224 \r
225                 var startBlock = elementPath.block,\r
226                         startBlockTag = startBlock && elementPath.block.getName();\r
227 \r
228                 var isPre = false;\r
229 \r
230                 if ( !forceMode && startBlockTag == 'li' )\r
231                 {\r
232                         enterBlock( editor, mode, range );\r
233                         return;\r
234                 }\r
235 \r
236                 // If we are at the end of a header block.\r
237                 if ( !forceMode && isEndOfBlock && headerTagRegex.test( startBlockTag ) )\r
238                 {\r
239                         // Insert a <br> after the current paragraph.\r
240                         doc.createElement( 'br' ).insertAfter( startBlock );\r
241 \r
242                         // A text node is required by Gecko only to make the cursor blink.\r
243                         if ( CKEDITOR.env.gecko )\r
244                                 doc.createText( '' ).insertAfter( startBlock );\r
245 \r
246                         // IE has different behaviors regarding position.\r
247                         range.setStartAt( startBlock.getNext(), CKEDITOR.env.ie ? CKEDITOR.POSITION_BEFORE_START : CKEDITOR.POSITION_AFTER_START );\r
248                 }\r
249                 else\r
250                 {\r
251                         var lineBreak;\r
252 \r
253                         isPre = ( startBlockTag == 'pre' );\r
254 \r
255                         if ( isPre )\r
256                                 lineBreak = doc.createText( CKEDITOR.env.ie ? '\r' : '\n' );\r
257                         else\r
258                                 lineBreak = doc.createElement( 'br' );\r
259 \r
260                         range.deleteContents();\r
261                         range.insertNode( lineBreak );\r
262 \r
263                         // A text node is required by Gecko only to make the cursor blink.\r
264                         // We need some text inside of it, so the bogus <br> is properly\r
265                         // created.\r
266                         if ( !CKEDITOR.env.ie )\r
267                                 doc.createText( '\ufeff' ).insertAfter( lineBreak );\r
268 \r
269                         // If we are at the end of a block, we must be sure the bogus node is available in that block.\r
270                         if ( isEndOfBlock && !CKEDITOR.env.ie )\r
271                                 lineBreak.getParent().appendBogus();\r
272 \r
273                         // Now we can remove the text node contents, so the caret doesn't\r
274                         // stop on it.\r
275                         if ( !CKEDITOR.env.ie )\r
276                                 lineBreak.getNext().$.nodeValue = '';\r
277                         // IE has different behavior regarding position.\r
278                         if ( CKEDITOR.env.ie )\r
279                                 range.setStartAt( lineBreak, CKEDITOR.POSITION_AFTER_END );\r
280                         else\r
281                                 range.setStartAt( lineBreak.getNext(), CKEDITOR.POSITION_AFTER_START );\r
282 \r
283                         // Scroll into view, for non IE.\r
284                         if ( !CKEDITOR.env.ie )\r
285                         {\r
286                                 var dummy = null;\r
287 \r
288                                 // BR is not positioned in Opera and Webkit.\r
289                                 if ( !CKEDITOR.env.gecko )\r
290                                 {\r
291                                         dummy = doc.createElement( 'span' );\r
292                                         // We need have some contents for Webkit to position it\r
293                                         // under parent node. ( #3681)\r
294                                         dummy.setHtml('&nbsp;');\r
295                                 }\r
296                                 else\r
297                                         dummy = doc.createElement( 'br' );\r
298 \r
299                                 dummy.insertBefore( lineBreak.getNext() );\r
300                                 dummy.scrollIntoView();\r
301                                 dummy.remove();\r
302                         }\r
303                 }\r
304 \r
305                 // This collapse guarantees the cursor will be blinking.\r
306                 range.collapse( true );\r
307 \r
308                 range.select( isPre );\r
309         }\r
310 \r
311         function getRange( editor )\r
312         {\r
313                 // Get the selection ranges.\r
314                 var ranges = editor.getSelection().getRanges();\r
315 \r
316                 // Delete the contents of all ranges except the first one.\r
317                 for ( var i = ranges.length - 1 ; i > 0 ; i-- )\r
318                 {\r
319                         ranges[ i ].deleteContents();\r
320                 }\r
321 \r
322                 // Return the first range.\r
323                 return ranges[ 0 ];\r
324         }\r
325 })();\r