From 412015d8086eb63212c3c15a1bd05c4aa120b246 Mon Sep 17 00:00:00 2001
From: Piotr Jasiun <p.jasiun@cksource.com>
Date: Thu, 9 Oct 2014 11:19:50 +0200
Subject: [PATCH] Paragraph in list on shift-enter.
---
plugins/enterkey/plugin.js | 36 ++++++++++++++++++++++++++++--------
1 file changed, 28 insertions(+), 8 deletions(-)
diff --git a/plugins/enterkey/plugin.js b/plugins/enterkey/plugin.js
index cd94c92..546c2a3 100644
a
|
b
|
|
33 | 33 | bookmark = CKEDITOR.dom.walker.bookmark(); |
34 | 34 | |
35 | 35 | CKEDITOR.plugins.enterkey = { |
36 | | enterBlock: function( editor, mode, range, forceMode ) { |
| 36 | enterBlock: function( editor, mode, range, forceMode, shiftEnter ) { |
37 | 37 | // Get the range for the current selection. |
38 | 38 | range = range || getRange( editor ); |
39 | 39 | |
… |
… |
|
52 | 52 | atBlockEnd = range.checkEndOfBlock(), |
53 | 53 | path = editor.elementPath( range.startContainer ), |
54 | 54 | block = path.block, |
| 55 | liBookmark, p, |
55 | 56 | |
56 | 57 | // Determine the block element to be used. |
57 | 58 | blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' ), |
… |
… |
|
271 | 272 | } |
272 | 273 | } |
273 | 274 | |
| 275 | // Put <p> into <li> and move the content: |
| 276 | // <li>foo[]</li> -> <li><p>foo[]</p></li> |
| 277 | if ( shiftEnter && block.is( 'li' ) ) { |
| 278 | liBookmark = range.createBookmark(), |
| 279 | p = new CKEDITOR.dom.element( 'p' ); |
| 280 | |
| 281 | block.moveChildren( p ); |
| 282 | block.append( p ); |
| 283 | block = p; |
| 284 | |
| 285 | range.moveToBookmark( liBookmark ); |
| 286 | } |
| 287 | |
274 | 288 | // Split the range. |
275 | 289 | var splitInfo = range.splitBlock( blockTag ); |
276 | 290 | |
… |
… |
|
289 | 303 | // If this is a block under a list item, split it as well. (#1647) |
290 | 304 | if ( nextBlock ) { |
291 | 305 | node = nextBlock.getParent(); |
292 | | if ( node.is( 'li' ) ) { |
| 306 | if ( !shiftEnter && node.is( 'li' ) ) { |
293 | 307 | nextBlock.breakParent( node ); |
294 | 308 | nextBlock.move( nextBlock.getNext(), 1 ); |
295 | 309 | } |
296 | | } else if ( previousBlock && ( node = previousBlock.getParent() ) && node.is( 'li' ) ) { |
| 310 | } else if ( previousBlock && ( node = previousBlock.getParent() ) && !shiftEnter && node.is( 'li' ) ) { |
297 | 311 | previousBlock.breakParent( node ); |
298 | 312 | node = previousBlock.getNext(); |
299 | 313 | range.moveToElementEditStart( node ); |
… |
… |
|
307 | 321 | // If the next block is an <li> with another list tree as the first |
308 | 322 | // child, we'll need to append a filler (<br>/NBSP) or the list item |
309 | 323 | // wouldn't be editable. (#1420) |
310 | | if ( nextBlock.is( 'li' ) ) { |
| 324 | if ( !shiftEnter && nextBlock.is( 'li' ) ) { |
311 | 325 | var walkerRange = range.clone(); |
312 | 326 | walkerRange.selectNodeContents( nextBlock ); |
313 | 327 | var walker = new CKEDITOR.dom.walker( walkerRange ); |
… |
… |
|
330 | 344 | // Do not enter this block if it's a header tag, or we are in |
331 | 345 | // a Shift+Enter (#77). Create a new block element instead |
332 | 346 | // (later in the code). |
333 | | if ( previousBlock.is( 'li' ) || !( headerTagRegex.test( previousBlock.getName() ) || previousBlock.is( 'pre' ) ) ) { |
| 347 | if ( ( !shiftEnter && previousBlock.is( 'li' ) ) || !( headerTagRegex.test( previousBlock.getName() ) || previousBlock.is( 'pre' ) ) ) { |
334 | 348 | // Otherwise, duplicate the previous block. |
335 | 349 | newBlock = previousBlock.clone(); |
336 | 350 | } |
… |
… |
|
494 | 508 | headerTagRegex = /^h[1-6]$/; |
495 | 509 | |
496 | 510 | function shiftEnter( editor ) { |
| 511 | var mode = editor.activeShiftEnterMode, |
| 512 | path = editor.elementPath(); |
| 513 | |
| 514 | if ( path.contains( 'li' ) ) { |
| 515 | mode = CKEDITOR.ENTER_P; |
| 516 | } |
497 | 517 | // On SHIFT+ENTER: |
498 | 518 | // 1. We want to enforce the mode to be respected, instead |
499 | 519 | // of cloning the current block. (#77) |
500 | | return enter( editor, editor.activeShiftEnterMode, 1 ); |
| 520 | return enter( editor, mode, 1, 1 ); |
501 | 521 | } |
502 | 522 | |
503 | | function enter( editor, mode, forceMode ) { |
| 523 | function enter( editor, mode, forceMode, shiftEnter ) { |
504 | 524 | forceMode = editor.config.forceEnterMode || forceMode; |
505 | 525 | |
506 | 526 | // Only effective within document. |
… |
… |
|
524 | 544 | if ( mode == CKEDITOR.ENTER_BR ) |
525 | 545 | enterBr( editor, mode, null, forceMode ); |
526 | 546 | else |
527 | | enterBlock( editor, mode, null, forceMode ); |
| 547 | enterBlock( editor, mode, null, forceMode, shiftEnter ); |
528 | 548 | |
529 | 549 | editor.fire( 'saveSnapshot' ); |
530 | 550 | } |