entercommand.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module enter/entercommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  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 module:core/command~Command
  14. */
  15. export default class EnterCommand extends Command {
  16. /**
  17. * @inheritDoc
  18. */
  19. execute() {
  20. const doc = this.editor.document;
  21. const batch = doc.batch();
  22. doc.enqueueChanges( () => {
  23. enterBlock( this.editor.data, batch, doc.selection, doc.schema );
  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. // @param {module:engine/model/schema~Schema} schema
  34. function enterBlock( dataController, batch, selection, schema ) {
  35. const isSelectionEmpty = selection.isCollapsed;
  36. const range = selection.getFirstRange();
  37. const startElement = range.start.parent;
  38. const endElement = range.end.parent;
  39. // Don't touch the roots and other limit elements.
  40. if ( schema.limits.has( startElement.name ) || schema.limits.has( endElement.name ) ) {
  41. // Delete the selected content but only if inside a single limit element.
  42. // Abort, when crossing limit elements boundary (e.g. <limit1>x[x</limit1>donttouchme<limit2>y]y</limit2>).
  43. // This is an edge case and it's hard to tell what should actually happen because such a selection
  44. // is not entirely valid.
  45. if ( !isSelectionEmpty && startElement == endElement ) {
  46. dataController.deleteContent( selection, batch );
  47. }
  48. return;
  49. }
  50. if ( isSelectionEmpty ) {
  51. splitBlock( batch, selection, range.start );
  52. } else {
  53. const leaveUnmerged = !( range.start.isAtStart && range.end.isAtEnd );
  54. const isContainedWithinOneElement = ( startElement == endElement );
  55. dataController.deleteContent( selection, batch, { leaveUnmerged } );
  56. if ( leaveUnmerged ) {
  57. // Partially selected elements.
  58. //
  59. // <h>x[xx]x</h> -> <h>x^x</h> -> <h>x</h><h>^x</h>
  60. if ( isContainedWithinOneElement ) {
  61. splitBlock( batch, selection, selection.focus );
  62. }
  63. // Selection over multiple elements.
  64. //
  65. // <h>x[x</h><p>y]y<p> -> <h>x^</h><p>y</p> -> <h>x</h><p>^y</p>
  66. else {
  67. selection.setCollapsedAt( endElement );
  68. }
  69. }
  70. }
  71. }
  72. function splitBlock( batch, selection, splitPos ) {
  73. const oldElement = splitPos.parent;
  74. const newElement = new oldElement.constructor( oldElement.name, oldElement.getAttributes() );
  75. if ( splitPos.isAtEnd ) {
  76. // If the split is at the end of element, instead of splitting, just create a clone of position's parent
  77. // element and insert it after split element. The result is the same but less operations are done
  78. // and it's more semantically correct (when it comes to operational transformation).
  79. batch.insert( Position.createAfter( splitPos.parent ), newElement );
  80. } else if ( splitPos.isAtStart ) {
  81. // If the split is at the start of element, instead of splitting, just create a clone of position's parent
  82. // element and insert it before split element. The result is the same but less operations are done
  83. // and it's more semantically correct (when it comes to operational transformation).
  84. batch.insert( Position.createBefore( splitPos.parent ), newElement );
  85. } else {
  86. batch.split( splitPos );
  87. }
  88. selection.setCollapsedAt( splitPos.parent.nextSibling );
  89. }