deletecommand.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module typing/deletecommand
  7. */
  8. import Command from '@ckeditor/ckeditor5-core/src/command';
  9. import Selection from '@ckeditor/ckeditor5-engine/src/model/selection';
  10. import Element from '@ckeditor/ckeditor5-engine/src/model/element';
  11. import Position from '@ckeditor/ckeditor5-engine/src/model/position';
  12. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  13. import ChangeBuffer from './changebuffer';
  14. import count from '@ckeditor/ckeditor5-utils/src/count';
  15. /**
  16. * The delete command. Used by the {@link module:typing/delete~Delete delete feature} to handle the <kbd>Delete</kbd> and
  17. * <kbd>Backspace</kbd> keys.
  18. *
  19. * @extends module:core/command~Command
  20. */
  21. export default class DeleteCommand extends Command {
  22. /**
  23. * Creates an instance of the command.
  24. *
  25. * @param {module:core/editor/editor~Editor} editor
  26. * @param {'forward'|'backward'} direction The directionality of the delete describing in what direction it
  27. * should consume the content when the selection is collapsed.
  28. */
  29. constructor( editor, direction ) {
  30. super( editor );
  31. /**
  32. * The directionality of the delete describing in what direction it should
  33. * consume the content when the selection is collapsed.
  34. *
  35. * @readonly
  36. * @member {'forward'|'backward'} #direction
  37. */
  38. this.direction = direction;
  39. /**
  40. * Delete's change buffer used to group subsequent changes into batches.
  41. *
  42. * @readonly
  43. * @private
  44. * @member {typing.ChangeBuffer} #buffer
  45. */
  46. this._buffer = new ChangeBuffer( editor.document, editor.config.get( 'typing.undoStep' ) );
  47. }
  48. /**
  49. * Executes the delete command. Depending on whether the selection is collapsed or not, deletes its content
  50. * or a piece of content in the {@link #direction defined direction}.
  51. *
  52. * @fires execute
  53. * @param {Object} [options] The command options.
  54. * @param {'character'} [options.unit='character'] See {@link module:engine/controller/modifyselection~modifySelection}'s options.
  55. * @param {Number} [options.sequence=1] A number describing which subsequent delete event it is without the key being released.
  56. * See the {@link module:engine/view/document~Document#event:delete} event data.
  57. */
  58. execute( options = {} ) {
  59. const doc = this.editor.document;
  60. const dataController = this.editor.data;
  61. doc.enqueueChanges( () => {
  62. this._buffer.lock();
  63. const selection = Selection.createFromSelection( doc.selection );
  64. // Do not replace the whole selected content if selection was collapsed.
  65. // This prevents such situation:
  66. //
  67. // <h1></h1><p>[]</p> --> <h1>[</h1><p>]</p> --> <p></p>
  68. // starting content --> after `modifySelection` --> after `deleteContent`.
  69. const doNotResetEntireContent = selection.isCollapsed;
  70. // Try to extend the selection in the specified direction.
  71. if ( selection.isCollapsed ) {
  72. dataController.modifySelection( selection, { direction: this.direction, unit: options.unit } );
  73. }
  74. // Check if deleting in an empty editor. See #61.
  75. if ( this._shouldEntireContentBeReplacedWithParagraph( options.sequence || 1 ) ) {
  76. this._replaceEntireContentWithParagraph();
  77. return;
  78. }
  79. // If selection is still collapsed, then there's nothing to delete.
  80. if ( selection.isCollapsed ) {
  81. return;
  82. }
  83. let changeCount = 0;
  84. selection.getFirstRange().getMinimalFlatRanges().forEach( range => {
  85. changeCount += count(
  86. range.getWalker( { singleCharacters: true, ignoreElementEnd: true, shallow: true } )
  87. );
  88. } );
  89. dataController.deleteContent( selection, this._buffer.batch, { doNotResetEntireContent } );
  90. this._buffer.input( changeCount );
  91. doc.selection.setRanges( selection.getRanges(), selection.isBackward );
  92. this._buffer.unlock();
  93. } );
  94. }
  95. /**
  96. * If the user keeps <kbd>Backspace</kbd> or <kbd>Delete</kbd> key pressed, the content of the current
  97. * editable will be cleared. However, this will not yet lead to resetting the remaining block to a paragraph
  98. * (which happens e.g. when the user does <kbd>Ctrl</kbd> + <kbd>A</kbd>, <kbd>Backspace</kbd>).
  99. *
  100. * But, if the user pressed the key in an empty editable for the first time,
  101. * we want to replace the entire content with a paragraph if:
  102. *
  103. * * the current limit element is empty,
  104. * * the paragraph is allowed in the limit element,
  105. * * other empty paragraph does not occur in the limit element.
  106. *
  107. * See https://github.com/ckeditor/ckeditor5-typing/issues/61.
  108. *
  109. * @private
  110. * @param {Number} sequence A number describing which subsequent delete event it is without the key being released.
  111. * @returns {Boolean}
  112. */
  113. _shouldEntireContentBeReplacedWithParagraph( sequence ) {
  114. // Does nothing if user pressed and held the "Backspace" or "Delete" key.
  115. if ( sequence > 1 ) {
  116. return false;
  117. }
  118. const document = this.editor.document;
  119. const selection = document.selection;
  120. const limitElement = document.schema.getLimitElement( selection );
  121. // If a collapsed selection contains the whole content it means that the content is empty
  122. // (from the user perspective).
  123. const limitElementIsEmpty = selection.isCollapsed && selection.containsEntireContent( limitElement );
  124. if ( !limitElementIsEmpty ) {
  125. return false;
  126. }
  127. if ( !document.schema.check( { name: 'paragraph', inside: limitElement.name } ) ) {
  128. return false;
  129. }
  130. const limitElementFirstChild = limitElement.getChild( 0 );
  131. // Does nothing if limit element already contains an empty paragraph.
  132. if ( limitElementFirstChild && limitElementFirstChild.name === 'paragraph' ) {
  133. return false;
  134. }
  135. return true;
  136. }
  137. /**
  138. * The entire content is replaced with the paragraph. Selection is moved inside the paragraph.
  139. *
  140. * @private
  141. */
  142. _replaceEntireContentWithParagraph() {
  143. const document = this.editor.document;
  144. const selection = document.selection;
  145. const limitElement = document.schema.getLimitElement( selection );
  146. const paragraph = new Element( 'paragraph' );
  147. this._buffer.batch.remove( Range.createIn( limitElement ) );
  148. this._buffer.batch.insert( Position.createAt( limitElement ), paragraph );
  149. selection.setCollapsedAt( paragraph );
  150. }
  151. }