8
0
Просмотр исходного кода

Feature: Provide `options.selection` parameter for `DeleteCommand`.

Szymon Cofalik 6 лет назад
Родитель
Сommit
75cb6accec

+ 3 - 1
packages/ckeditor5-typing/src/deletecommand.js

@@ -66,6 +66,8 @@ export default class DeleteCommand extends Command {
 	 * @param {'character'} [options.unit='character'] See {@link module:engine/model/utils/modifyselection~modifySelection}'s options.
 	 * @param {Number} [options.sequence=1] A number describing which subsequent delete event it is without the key being released.
 	 * See the {@link module:engine/view/document~Document#event:delete} event data.
+	 * @param {module:engine/model/selection~Selection} [options.selection] Selection to remove. If not set, current model selection
+	 * will be used.
 	 */
 	execute( options = {} ) {
 		const model = this.editor.model;
@@ -74,7 +76,7 @@ export default class DeleteCommand extends Command {
 		model.enqueueChange( this._buffer.batch, writer => {
 			this._buffer.lock();
 
-			const selection = writer.createSelection( doc.selection );
+			const selection = writer.createSelection( options.selection || doc.selection );
 
 			// Do not replace the whole selected content if selection was collapsed.
 			// This prevents such situation:

+ 10 - 0
packages/ckeditor5-typing/tests/deletecommand.js

@@ -109,6 +109,16 @@ describe( 'DeleteCommand', () => {
 			expect( getData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
 		} );
 
+		it( 'deletes contents of selection passed in options', () => {
+			setData( model, '<paragraph>fo[ob]ar</paragraph>' );
+
+			const selection = model.createSelection( model.createRangeIn( model.document.getRoot() ) );
+
+			editor.execute( 'delete', { selection } );
+
+			expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
+		} );
+
 		it( 'merges elements', () => {
 			setData( model, '<paragraph>foo</paragraph><paragraph>[]bar</paragraph>' );