/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ 'use strict'; import LivePosition from '../liveposition.js'; import Position from '../position.js'; import compareArrays from '../../../utils/comparearrays.js'; /** * Delete contents of the selection and merge siblings. The resulting selection is always collapsed. * * @method engine.model.composer.deleteContents * @param {engine.model.Batch} batch Batch to which the deltas will be added. * @param {engine.model.Selection} selection Selection of which the content should be deleted. * @param {Object} [options] * @param {Boolean} [options.merge=false] Merge elements after removing the contents of the selection. * For example, `x[x

y]y

` will become: `x^y` with the option enabled * and: `x^

y

` without it. */ export default function deleteContents( batch, selection, options = {} ) { if ( selection.isCollapsed ) { return; } const selRange = selection.getFirstRange(); const startPos = selRange.start; const endPos = LivePosition.createFromPosition( selRange.end ); // 1. Remove the contents if there are any. if ( !selRange.isEmpty ) { batch.remove( selRange ); } // 2. 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: // // Fo[]ar => Fo^ar // // However, the algorithm supports also merging deeper structures (up to the depth of the shallower branch), // as it's hard to imagine what should actually be the default behavior. Usually, specific features will // want to override that behavior anyway. if ( options.merge ) { const endPath = endPos.path; const mergeEnd = Math.min( startPos.path.length - 1, endPath.length - 1 ); let mergeDepth = compareArrays( startPos.path, endPath ); if ( typeof mergeDepth == 'number' ) { for ( ; mergeDepth < mergeEnd; mergeDepth++ ) { const mergePath = startPos.path.slice( 0, mergeDepth ); mergePath.push( startPos.path[ mergeDepth ] + 1 ); batch.merge( new Position( endPos.root, mergePath ) ); } } } selection.collapse( startPos ); endPos.detach(); }