/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module engine/controller/deletecontent
*/
import LivePosition from '../model/liveposition';
import Position from '../model/position';
import Range from '../model/range';
import Element from '../model/element';
/**
* Deletes content of the selection and merge siblings. The resulting selection is always collapsed.
*
* @param {module:engine/model/selection~Selection} selection Selection of which the content should be deleted.
* @param {module:engine/model/batch~Batch} batch Batch to which the deltas will be added.
* @param {Object} [options]
* @param {Boolean} [options.leaveUnmerged=false] Whether to merge elements after removing the content of the selection.
*
* For example `
y]y
` will become: * * `y
` with enabled (`leaveUnmerged == true`). * * Note: {@link module:engine/model/schema~Schema#objects object} and {@link module:engine/model/schema~Schema#limits limit} * elements will not be merged. */ export default function deleteContent( selection, batch, options = {} ) { if ( selection.isCollapsed ) { return; } // 1. Replace the entire content with paragraph. // See: https://github.com/ckeditor/ckeditor5-engine/issues/1012#issuecomment-315017594. if ( shouldEntireContentBeReplacedWithParagraph( batch.document.schema, selection ) ) { replaceEntireContentWithParagraph( batch, selection ); return; } const selRange = selection.getFirstRange(); const startPos = selRange.start; const endPos = LivePosition.createFromPosition( selRange.end ); // 2. Remove the content if there is any. if ( !selRange.start.isTouching( selRange.end ) ) { batch.remove( selRange ); } // 3. Merge elements in the right branch to the elements in the left branch. // The only reasonable (in terms of data and selection correctness) case in which we need to do that is: // //x[]
{}y
$root> => <$root>xy
[]{}$root> if ( startParent == endParent ) { return; } // If one of the positions is a root, then there's nothing more to merge (at least in the current state of implementation). // Theoretically in this case we could unwrap the: <$root>x[]
{}y
$root>, but we don't need to support it yet // so let's just abort. if ( !startParent.parent || !endParent.parent ) { return; } // Check if operations we'll need to do won't need to cross object or limit boundaries. // E.g., we can't merge endParent into startParent in this case: //ab
[]{}cd
if ( !endPos.isEqual( startPos ) ) { batch.move( endParent, startPos ); } // To then become: // xy[]x[]
,