deletecontent.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 Element from '../model/element';
  11. import compareArrays from '@ckeditor/ckeditor5-utils/src/comparearrays';
  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.merge=false] Merge elements after removing the contents of the selection.
  19. * For example, `<h>x[x</h><p>y]y</p>` will become: `<h>x^y</h>` with the option enabled
  20. * and: `<h>x^</h><p>y</p>` without it.
  21. */
  22. export default function deleteContent( selection, batch, options = {} ) {
  23. if ( selection.isCollapsed ) {
  24. return;
  25. }
  26. const selRange = selection.getFirstRange();
  27. const startPos = selRange.start;
  28. const endPos = LivePosition.createFromPosition( selRange.end );
  29. // 1. Remove the contents if there are any.
  30. if ( !selRange.isEmpty ) {
  31. batch.remove( selRange );
  32. }
  33. // 2. Merge elements in the right branch to the elements in the left branch.
  34. // The only reasonable (in terms of data and selection correctness) case in which we need to do that is:
  35. //
  36. // <heading type=1>Fo[</heading><paragraph>]ar</paragraph> => <heading type=1>Fo^ar</heading>
  37. //
  38. // However, the algorithm supports also merging deeper structures (up to the depth of the shallower branch),
  39. // as it's hard to imagine what should actually be the default behavior. Usually, specific features will
  40. // want to override that behavior anyway.
  41. if ( options.merge ) {
  42. const endPath = endPos.path;
  43. const mergeEnd = Math.min( startPos.path.length - 1, endPath.length - 1 );
  44. let mergeDepth = compareArrays( startPos.path, endPath );
  45. if ( typeof mergeDepth == 'number' ) {
  46. for ( ; mergeDepth < mergeEnd; mergeDepth++ ) {
  47. const mergePath = startPos.path.slice( 0, mergeDepth );
  48. mergePath.push( startPos.path[ mergeDepth ] + 1 );
  49. const mergePos = new Position( endPos.root, mergePath );
  50. const nextNode = mergePos.nodeAfter;
  51. if ( nextNode.childCount > 0 ) {
  52. batch.merge( mergePos );
  53. } else {
  54. batch.remove( nextNode );
  55. }
  56. }
  57. }
  58. }
  59. selection.collapse( startPos );
  60. // 3. Autoparagraphing.
  61. // Check if a text is allowed in the new container. If not, try to create a new paragraph (if it's allowed here).
  62. if ( shouldAutoparagraph( batch.document, startPos ) ) {
  63. const paragraph = new Element( 'paragraph' );
  64. batch.insert( startPos, paragraph );
  65. selection.collapse( paragraph );
  66. }
  67. endPos.detach();
  68. }
  69. function shouldAutoparagraph( doc, position ) {
  70. const isTextAllowed = doc.schema.check( { name: '$text', inside: position } );
  71. const isParagraphAllowed = doc.schema.check( { name: 'paragraph', inside: position } );
  72. return !isTextAllowed && isParagraphAllowed;
  73. }