8
0

deletecontent.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/controller/deletecontent
  7. */
  8. import LivePosition from '../model/liveposition';
  9. import Position from '../model/position';
  10. import Range from '../model/range';
  11. import Element from '../model/element';
  12. /**
  13. * Deletes content of the selection and merge siblings. The resulting selection is always collapsed.
  14. *
  15. * @param {module:engine/model/selection~Selection} selection Selection of which the content should be deleted.
  16. * @param {module:engine/model/batch~Batch} batch Batch to which the deltas will be added.
  17. * @param {Object} [options]
  18. * @param {Boolean} [options.leaveUnmerged=false] Whether to merge elements after removing the content of the selection.
  19. *
  20. * For example `<h>x[x</h><p>y]y</p>` will become:
  21. * * `<h>x^y</h>` with the option disabled (`leaveUnmerged == false`)
  22. * * `<h>x^</h><p>y</p>` with enabled (`leaveUnmerged == true`).
  23. *
  24. * Note: {@link module:engine/model/schema~Schema#objects object} and {@link module:engine/model/schema~Schema#limits limit}
  25. * elements will not be merged.
  26. */
  27. export default function deleteContent( selection, batch, options = {} ) {
  28. if ( selection.isCollapsed ) {
  29. return;
  30. }
  31. // 1. Replace the entire content with paragraph.
  32. // See: https://github.com/ckeditor/ckeditor5-engine/issues/1012#issuecomment-315017594.
  33. if ( shouldEntireContentBeReplacedWithParagraph( batch.document.schema, selection ) ) {
  34. replaceEntireContentWithParagraph( batch, selection );
  35. return;
  36. }
  37. const selRange = selection.getFirstRange();
  38. const startPos = selRange.start;
  39. const endPos = LivePosition.createFromPosition( selRange.end );
  40. // 2. Remove the content if there is any.
  41. if ( !selRange.start.isTouching( selRange.end ) ) {
  42. batch.remove( selRange );
  43. }
  44. // 3. Merge elements in the right branch to the elements in the left branch.
  45. // The only reasonable (in terms of data and selection correctness) case in which we need to do that is:
  46. //
  47. // <heading type=1>Fo[</heading><paragraph>]ar</paragraph> => <heading type=1>Fo^ar</heading>
  48. //
  49. // However, the algorithm supports also merging deeper structures (up to the depth of the shallower branch),
  50. // as it's hard to imagine what should actually be the default behavior. Usually, specific features will
  51. // want to override that behavior anyway.
  52. if ( !options.leaveUnmerged ) {
  53. mergeBranches( batch, startPos, endPos );
  54. }
  55. selection.collapse( startPos );
  56. // 4. Autoparagraphing.
  57. // Check if a text is allowed in the new container. If not, try to create a new paragraph (if it's allowed here).
  58. if ( shouldAutoparagraph( batch.document, startPos ) ) {
  59. insertParagraph( batch, startPos, selection );
  60. }
  61. endPos.detach();
  62. }
  63. // This function is a result of reaching the Ballmer's peak for just the right amount of time.
  64. // Even I had troubles documenting it after a while and after reading it again I couldn't believe that it really works.
  65. function mergeBranches( batch, startPos, endPos ) {
  66. const startParent = startPos.parent;
  67. const endParent = endPos.parent;
  68. // If both positions ended up in the same parent, then there's nothing more to merge:
  69. // <$root><p>x[]</p><p>{}y</p></$root> => <$root><p>xy</p>[]{}</$root>
  70. if ( startParent == endParent ) {
  71. return;
  72. }
  73. // If one of the positions is a root, then there's nothing more to merge (at least in the current state of implementation).
  74. // Theoretically in this case we could unwrap the <p>: <$root>x[]<p>{}y</p></$root>, but we don't need to support it yet
  75. // so let's just abort.
  76. if ( !startParent.parent || !endParent.parent ) {
  77. return;
  78. }
  79. // Check if operations we'll need to do won't need to cross object or limit boundaries.
  80. // E.g., we can't merge endParent into startParent in this case:
  81. // <limit><startParent>x[]</startParent></limit><endParent>{}</endParent>
  82. if ( !checkCanBeMerged( startPos, endPos ) ) {
  83. return;
  84. }
  85. // Remember next positions to merge. For example:
  86. // <a><b>x[]</b></a><c><d>{}y</d></c>
  87. // will become:
  88. // <a><b>xy</b>[]</a><c>{}</c>
  89. startPos = Position.createAfter( startParent );
  90. endPos = Position.createBefore( endParent );
  91. if ( endParent.isEmpty ) {
  92. batch.remove( endParent );
  93. } else {
  94. // At the moment, next startPos is also the position to which the endParent
  95. // needs to be moved:
  96. // <a><b>x[]</b></a><c><d>{}y</d></c>
  97. // becomes:
  98. // <a><b>x</b>[]<d>y</d></a><c>{}</c>
  99. // Move the end parent only if needed.
  100. // E.g. not in this case: <p>ab</p>[]{}<p>cd</p>
  101. if ( !endPos.isEqual( startPos ) ) {
  102. batch.move( endParent, startPos );
  103. }
  104. // To then become:
  105. // <a><b>xy</b>[]</a><c>{}</c>
  106. batch.merge( startPos );
  107. }
  108. // Removes empty end ancestors:
  109. // <a>fo[o</a><b><a><c>bar]</c></a></b>
  110. // becomes:
  111. // <a>fo[]</a><b><a>{}</a></b>
  112. // So we can remove <a> and <b>.
  113. while ( endPos.parent.isEmpty ) {
  114. const parentToRemove = endPos.parent;
  115. endPos = Position.createBefore( parentToRemove );
  116. batch.remove( parentToRemove );
  117. }
  118. // Continue merging next level.
  119. mergeBranches( batch, startPos, endPos );
  120. }
  121. function shouldAutoparagraph( doc, position ) {
  122. const isTextAllowed = doc.schema.check( { name: '$text', inside: position } );
  123. const isParagraphAllowed = doc.schema.check( { name: 'paragraph', inside: position } );
  124. return !isTextAllowed && isParagraphAllowed;
  125. }
  126. // Check if parents of two positions can be merged by checking if there are no limit/object
  127. // boundaries between those two positions.
  128. //
  129. // E.g. in <bQ><p>x[]</p></bQ><widget><caption>{}</caption></widget>
  130. // we'll check <p>, <bQ>, <widget> and <caption>.
  131. // Usually, widget and caption are marked as objects/limits in the schema, so in this case merging will be blocked.
  132. function checkCanBeMerged( leftPos, rightPos ) {
  133. const schema = leftPos.root.document.schema;
  134. const rangeToCheck = new Range( leftPos, rightPos );
  135. for ( const value of rangeToCheck.getWalker() ) {
  136. if ( schema.objects.has( value.item.name ) || schema.limits.has( value.item.name ) ) {
  137. return false;
  138. }
  139. }
  140. return true;
  141. }
  142. function insertParagraph( batch, position, selection ) {
  143. const paragraph = new Element( 'paragraph' );
  144. batch.insert( position, paragraph );
  145. selection.collapse( paragraph );
  146. }
  147. function replaceEntireContentWithParagraph( batch, selection ) {
  148. const limitElement = batch.document.schema.getLimitElement( selection );
  149. batch.remove( Range.createIn( limitElement ) );
  150. insertParagraph( batch, Position.createAt( limitElement ), selection );
  151. }
  152. // We want to replace the entire content with a paragraph when:
  153. // * the entire content is selected,
  154. // * selection contains at least two elements,
  155. // * whether the paragraph is allowed in schema in the common ancestor.
  156. function shouldEntireContentBeReplacedWithParagraph( schema, selection ) {
  157. const limitElement = schema.getLimitElement( selection );
  158. const limitStartPosition = Position.createAt( limitElement );
  159. const limitEndPosition = Position.createAt( limitElement, 'end' );
  160. if (
  161. !limitStartPosition.isTouching( selection.getFirstPosition() ) ||
  162. !limitEndPosition.isTouching( selection.getLastPosition() )
  163. ) {
  164. return false;
  165. }
  166. const range = selection.getFirstRange();
  167. if ( range.start.parent == range.end.parent ) {
  168. return false;
  169. }
  170. if ( !schema.check( { name: 'paragraph', inside: limitElement.name } ) ) {
  171. return false;
  172. }
  173. return true;
  174. }