8
0

deletecontent.js 7.8 KB

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