entercommand.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module enter/entercommand
  7. */
  8. import Command from '../core/command/command.js';
  9. import Position from '../engine/model/position.js';
  10. /**
  11. * Enter command. It is used by the {@link module:enter/enter~Enter Enter feature} to handle the <kbd>Enter</kbd> key.
  12. *
  13. * @extends modue:core/command/command~Command
  14. */
  15. export default class EnterCommand extends Command {
  16. /**
  17. * @inheritDoc
  18. */
  19. _doExecute() {
  20. const doc = this.editor.document;
  21. const batch = doc.batch();
  22. doc.enqueueChanges( () => {
  23. enterBlock( this.editor.data, batch, doc.selection );
  24. this.fire( 'afterExecute', { batch } );
  25. } );
  26. }
  27. }
  28. // Creates a new block in the way that the <kbd>Enter</kbd> key is expected to work.
  29. //
  30. // @param {engine.controller.DataController} dataController
  31. // @param {module:engine/model/batch~Batch} batch A batch to which the deltas will be added.
  32. // @param {module:engine/model/selection~Selection} selection Selection on which the action should be performed.
  33. function enterBlock( dataController, batch, selection ) {
  34. const isSelectionEmpty = selection.isCollapsed;
  35. const range = selection.getFirstRange();
  36. const startElement = range.start.parent;
  37. const endElement = range.end.parent;
  38. // Don't touch the root.
  39. if ( startElement.root == startElement ) {
  40. if ( !isSelectionEmpty ) {
  41. dataController.deleteContent( selection, batch );
  42. }
  43. return;
  44. }
  45. if ( isSelectionEmpty ) {
  46. splitBlock( batch, selection, range.start );
  47. } else {
  48. const shouldMerge = range.start.isAtStart && range.end.isAtEnd;
  49. const isContainedWithinOneElement = ( startElement == endElement );
  50. dataController.deleteContent( selection, batch, { merge: shouldMerge } );
  51. if ( !shouldMerge ) {
  52. // Partially selected elements.
  53. //
  54. // <h>x[xx]x</h> -> <h>x^x</h> -> <h>x</h><h>^x</h>
  55. if ( isContainedWithinOneElement ) {
  56. splitBlock( batch, selection, selection.focus );
  57. }
  58. // Selection over multiple elements.
  59. //
  60. // <h>x[x</h><p>y]y<p> -> <h>x^</h><p>y</p> -> <h>x</h><p>^y</p>
  61. else {
  62. selection.collapse( endElement );
  63. }
  64. }
  65. }
  66. }
  67. function splitBlock( batch, selection, splitPos ) {
  68. if ( splitPos.isAtEnd ) {
  69. // If the split is at the end of element, instead of splitting, just create a clone of position's parent
  70. // element and insert it after cloned element. The result is the same but less operations are taken
  71. // and it's more semantically correct (when it comes to operational transformation).
  72. const oldElement = splitPos.parent;
  73. const newElement = new oldElement.constructor( oldElement.name, oldElement.getAttributes() );
  74. batch.insert( Position.createAfter( splitPos.parent ), newElement );
  75. } else {
  76. batch.split( splitPos );
  77. }
  78. selection.collapse( splitPos.parent.nextSibling );
  79. }