JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[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 placeholder or the list item\r
105                         // wouldn't be editable. (#1420)\r
106                         if ( nextBlock.is( 'li' ) && ( node = nextBlock.getFirst() )\r
107                                         && node.is && node.is( 'ul', 'ol') )\r
108                                 nextBlock.insertBefore( doc.createText( '\xa0' ), node );\r
109 \r
110                         // Move the selection to the end block.\r
111                         if ( nextBlock )\r
112                                 range.moveToElementEditStart( nextBlock );\r
113                 }\r
114                 else\r
115                 {\r
116 \r
117                         if ( isStartOfBlock && isEndOfBlock && previousBlock.is( 'li' ) )\r
118                         {\r
119                                 editor.execCommand( 'outdent' );\r
120                                 return;\r
121                         }\r
122 \r
123                         var newBlock;\r
124 \r
125                         if ( previousBlock )\r
126                         {\r
127                                 // Do not enter this block if it's a header tag, or we are in\r
128                                 // a Shift+Enter (#77). Create a new block element instead\r
129                                 // (later in the code).\r
130                                 if ( !forceMode && !headerTagRegex.test( previousBlock.getName() ) )\r
131                                 {\r
132                                         // Otherwise, duplicate the previous block.\r
133                                         newBlock = previousBlock.clone();\r
134                                 }\r
135                         }\r
136                         else if ( nextBlock )\r
137                                 newBlock = nextBlock.clone();\r
138 \r
139                         if ( !newBlock )\r
140                                 newBlock = doc.createElement( blockTag );\r
141 \r
142                         // Recreate the inline elements tree, which was available\r
143                         // before hitting enter, so the same styles will be available in\r
144                         // the new block.\r
145                         var elementPath = splitInfo.elementPath;\r
146                         if ( elementPath )\r
147                         {\r
148                                 for ( var i = 0, len = elementPath.elements.length ; i < len ; i++ )\r
149                                 {\r
150                                         var element = elementPath.elements[ i ];\r
151 \r
152                                         if ( element.equals( elementPath.block ) || element.equals( elementPath.blockLimit ) )\r
153                                                 break;\r
154 \r
155                                         if ( CKEDITOR.dtd.$removeEmpty[ element.getName() ] )\r
156                                         {\r
157                                                 element = element.clone();\r
158                                                 newBlock.moveChildren( element );\r
159                                                 newBlock.append( element );\r
160                                         }\r
161                                 }\r
162                         }\r
163 \r
164                         if ( !CKEDITOR.env.ie )\r
165                                 newBlock.appendBogus();\r
166 \r
167                         range.insertNode( newBlock );\r
168 \r
169                         // This is tricky, but to make the new block visible correctly\r
170                         // we must select it.\r
171                         // The previousBlock check has been included because it may be\r
172                         // empty if we have fixed a block-less space (like ENTER into an\r
173                         // empty table cell).\r
174                         if ( CKEDITOR.env.ie && isStartOfBlock && ( !isEndOfBlock || !previousBlock.getChildCount() ) )\r
175                         {\r
176                                 // Move the selection to the new block.\r
177                                 range.moveToElementEditStart( isEndOfBlock ? previousBlock : newBlock );\r
178                                 range.select();\r
179                         }\r
180 \r
181                         // Move the selection to the new block.\r
182                         range.moveToElementEditStart( isStartOfBlock && !isEndOfBlock ? nextBlock : newBlock );\r
183                 }\r
184 \r
185                 if ( !CKEDITOR.env.ie )\r
186                 {\r
187                         if ( nextBlock )\r
188                         {\r
189                                 // If we have split the block, adds a temporary span at the\r
190                                 // range position and scroll relatively to it.\r
191                                 var tmpNode = doc.createElement( 'span' );\r
192 \r
193                                 // We need some content for Safari.\r
194                                 tmpNode.setHtml( '&nbsp;' );\r
195 \r
196                                 range.insertNode( tmpNode );\r
197                                 tmpNode.scrollIntoView();\r
198                                 range.deleteContents();\r
199                         }\r
200                         else\r
201                         {\r
202                                 // We may use the above scroll logic for the new block case\r
203                                 // too, but it gives some weird result with Opera.\r
204                                 newBlock.scrollIntoView();\r
205                         }\r
206                 }\r
207 \r
208                 range.select();\r
209         }\r
210 \r
211         function enterBr( editor, mode )\r
212         {\r
213                 // Get the range for the current selection.\r
214                 var range = getRange( editor ),\r
215                         doc = range.document;\r
216 \r
217                 // Determine the block element to be used.\r
218                 var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );\r
219 \r
220                 var isEndOfBlock = range.checkEndOfBlock();\r
221 \r
222                 var elementPath = new CKEDITOR.dom.elementPath( editor.getSelection().getStartElement() );\r
223 \r
224                 var startBlock = elementPath.block,\r
225                         startBlockTag = startBlock && elementPath.block.getName();\r
226 \r
227                 var isPre = false;\r
228 \r
229                 if ( !forceMode && startBlockTag == 'li' )\r
230                 {\r
231                         enterBlock( editor, mode, range );\r
232                         return;\r
233                 }\r
234 \r
235                 // If we are at the end of a header block.\r
236                 if ( !forceMode && isEndOfBlock && headerTagRegex.test( startBlockTag ) )\r
237                 {\r
238                         // Insert a <br> after the current paragraph.\r
239                         doc.createElement( 'br' ).insertAfter( startBlock );\r
240 \r
241                         // A text node is required by Gecko only to make the cursor blink.\r
242                         if ( CKEDITOR.env.gecko )\r
243                                 doc.createText( '' ).insertAfter( startBlock );\r
244 \r
245                         // IE has different behaviors regarding position.\r
246                         range.setStartAt( startBlock.getNext(), CKEDITOR.env.ie ? CKEDITOR.POSITION_BEFORE_START : CKEDITOR.POSITION_AFTER_START );\r
247                 }\r
248                 else\r
249                 {\r
250                         var lineBreak;\r
251 \r
252                         isPre = ( startBlockTag == 'pre' );\r
253 \r
254                         if ( isPre )\r
255                                 lineBreak = doc.createText( CKEDITOR.env.ie ? '\r' : '\n' );\r
256                         else\r
257                                 lineBreak = doc.createElement( 'br' );\r
258 \r
259                         range.deleteContents();\r
260                         range.insertNode( lineBreak );\r
261 \r
262                         // A text node is required by Gecko only to make the cursor blink.\r
263                         // We need some text inside of it, so the bogus <br> is properly\r
264                         // created.\r
265                         if ( !CKEDITOR.env.ie )\r
266                                 doc.createText( '\ufeff' ).insertAfter( lineBreak );\r
267 \r
268                         // If we are at the end of a block, we must be sure the bogus node is available in that block.\r
269                         if ( isEndOfBlock && !CKEDITOR.env.ie )\r
270                                 lineBreak.getParent().appendBogus();\r
271 \r
272                         // Now we can remove the text node contents, so the caret doesn't\r
273                         // stop on it.\r
274                         if ( !CKEDITOR.env.ie )\r
275                                 lineBreak.getNext().$.nodeValue = '';\r
276                         // IE has different behavior regarding position.\r
277                         if ( CKEDITOR.env.ie )\r
278                                 range.setStartAt( lineBreak, CKEDITOR.POSITION_AFTER_END );\r
279                         else\r
280                                 range.setStartAt( lineBreak.getNext(), CKEDITOR.POSITION_AFTER_START );\r
281 \r
282                         // Scroll into view, for non IE.\r
283                         if ( !CKEDITOR.env.ie )\r
284                         {\r
285                                 var dummy = null;\r
286 \r
287                                 // BR is not positioned in Opera and Webkit.\r
288                                 if ( !CKEDITOR.env.gecko )\r
289                                 {\r
290                                         dummy = doc.createElement( 'span' );\r
291                                         // We need have some contents for Webkit to position it\r
292                                         // under parent node. ( #3681)\r
293                                         dummy.setHtml('&nbsp;');\r
294                                 }\r
295                                 else\r
296                                         dummy = doc.createElement( 'br' );\r
297 \r
298                                 dummy.insertBefore( lineBreak.getNext() );\r
299                                 dummy.scrollIntoView();\r
300                                 dummy.remove();\r
301                         }\r
302                 }\r
303 \r
304                 // This collapse guarantees the cursor will be blinking.\r
305                 range.collapse( true );\r
306 \r
307                 range.select( isPre );\r
308         }\r
309 \r
310         function getRange( editor )\r
311         {\r
312                 // Get the selection ranges.\r
313                 var ranges = editor.getSelection().getRanges();\r
314 \r
315                 // Delete the contents of all ranges except the first one.\r
316                 for ( var i = ranges.length - 1 ; i > 0 ; i-- )\r
317                 {\r
318                         ranges[ i ].deleteContents();\r
319                 }\r
320 \r
321                 // Return the first range.\r
322                 return ranges[ 0 ];\r
323         }\r
324 })();\r