Browse Source

Updated _doExecute method to use options object. Added batch option. Updated automated tests.

Maksymilian Barnaś 9 years ago
parent
commit
a68e6cac63

+ 1 - 1
packages/ckeditor5-heading/src/heading.js

@@ -64,7 +64,7 @@ export default class Heading extends Feature {
 
 		// Execute command when an item from the dropdown is selected.
 		this.listenTo( dropdownModel, 'execute', ( evt ) => {
-			editor.execute( 'heading', evt.source.id );
+			editor.execute( 'heading', { formatId: evt.source.id } );
 			editor.editing.view.focus();
 		} );
 

+ 7 - 3
packages/ckeditor5-heading/src/headingcommand.js

@@ -67,12 +67,16 @@ export default class HeadingCommand extends Command {
 	 * Executes command.
 	 *
 	 * @protected
-	 * @param {String} [formatId] The identifier of the heading format that should be applied. It should be one of the
+	 * @param {Object} [options] Options for executed command.
+	 * @param {String} [options.formatId] The identifier of the heading format that should be applied. It should be one of the
 	 * {@link heading.HeadingFormat heading formats} provided to the command constructor. If this parameter is not provided,
 	 * the value from {@link heading.HeadingCommand#defaultFormat defaultFormat} will be used.
+	 * @param {engine.model.Batch} [options.batch] Batch to collect all the change steps.
+	 * New batch will be created if this option is not set.
 	 */
-	_doExecute( formatId = this.defaultFormat.id ) {
+	_doExecute( options = {} ) {
 		// TODO: What should happen if format is not found?
+		const formatId = options.formatId || this.defaultFormat.id;
 		const doc = this.editor.document;
 		const selection = doc.selection;
 		const startPosition = selection.getFirstPosition();
@@ -106,7 +110,7 @@ export default class HeadingCommand extends Command {
 		}
 
 		doc.enqueueChanges( () => {
-			const batch = doc.batch();
+			const batch = options.batch || doc.batch();
 
 			for ( let element of elements ) {
 				// When removing applied format.

+ 1 - 1
packages/ckeditor5-heading/tests/heading.js

@@ -57,7 +57,7 @@ describe( 'Heading', () => {
 		model.fire( 'execute' );
 
 		sinon.assert.calledOnce( executeSpy );
-		sinon.assert.calledWithExactly( executeSpy, 'heading', 'foo' );
+		sinon.assert.calledWithExactly( executeSpy, 'heading', { formatId: 'foo' } );
 	} );
 
 	it( 'should focus view after command execution', () => {

+ 19 - 6
packages/ckeditor5-heading/tests/headingcommand.js

@@ -65,6 +65,19 @@ describe( 'HeadingCommand', () => {
 	} );
 
 	describe( '_doExecute', () => {
+		describe( 'custom options', () => {
+			it( 'should use provided batch', () => {
+				const batch = editor.document.batch();
+				setData( document, '<paragraph>foo[]bar</paragraph>' );
+
+				expect( batch.deltas.length ).to.equal( 0 );
+
+				command._doExecute( { batch } );
+
+				expect( batch.deltas.length ).to.be.above( 0 );
+			} );
+		} );
+
 		describe( 'collapsed selection', () => {
 			let convertTo = formats[ formats.length - 1 ];
 
@@ -82,14 +95,14 @@ describe( 'HeadingCommand', () => {
 
 			it( 'converts to default format when executed with already applied format', () => {
 				setData( document, '<heading1>foo[]bar</heading1>' );
-				command._doExecute( 'heading1' );
+				command._doExecute( { formatId: 'heading1' } );
 
 				expect( getData( document ) ).to.equal( '<paragraph>foo[]bar</paragraph>' );
 			} );
 
 			it( 'converts topmost blocks', () => {
 				setData( document, '<heading1><b>foo[]</b>bar</heading1>' );
-				command._doExecute( 'heading1' );
+				command._doExecute( { formatId: 'heading1' } );
 
 				expect( getData( document ) ).to.equal( '<paragraph><b>foo[]</b>bar</paragraph>' );
 			} );
@@ -97,7 +110,7 @@ describe( 'HeadingCommand', () => {
 			function test( from, to ) {
 				it( `converts ${ from.id } to ${ to.id } on collapsed selection`, () => {
 					setData( document, `<${ from.id }>foo[]bar</${ from.id }>` );
-					command._doExecute( to.id );
+					command._doExecute( { formatId: to.id } );
 
 					expect( getData( document ) ).to.equal( `<${ to.id }>foo[]bar</${ to.id }>` );
 				} );
@@ -114,7 +127,7 @@ describe( 'HeadingCommand', () => {
 
 			it( 'converts all elements where selection is applied', () => {
 				setData( document, '<heading1>foo[</heading1><heading2>bar</heading2><heading2>]baz</heading2>' );
-				command._doExecute( 'paragraph' );
+				command._doExecute( { formatId: 'paragraph' } );
 
 				expect( getData( document ) ).to.equal(
 					'<paragraph>foo[</paragraph><paragraph>bar</paragraph><paragraph>]baz</paragraph>'
@@ -123,7 +136,7 @@ describe( 'HeadingCommand', () => {
 
 			it( 'resets to default value all elements with same format', () => {
 				setData( document, '<heading1>foo[</heading1><heading1>bar</heading1><heading2>baz</heading2>]' );
-				command._doExecute( 'heading1' );
+				command._doExecute( { formatId: 'heading1' } );
 
 				expect( getData( document ) ).to.equal(
 					'<paragraph>foo[</paragraph><paragraph>bar</paragraph><heading2>baz</heading2>]'
@@ -133,7 +146,7 @@ describe( 'HeadingCommand', () => {
 			function test( from, to ) {
 				it( `converts ${ from.id } to ${ to.id } on non-collapsed selection`, () => {
 					setData( document, `<${ from.id }>foo[bar</${ from.id }><${ from.id }>baz]qux</${ from.id }>` );
-					command._doExecute( to.id );
+					command._doExecute( { formatId: to.id } );
 
 					expect( getData( document ) ).to.equal( `<${ to.id }>foo[bar</${ to.id }><${ to.id }>baz]qux</${ to.id }>` );
 				} );