Browse Source

Merge pull request #35 from ckeditor/t/34

Changes param of _doExecute in toggleattributecommand to object. Adds new params.
Piotrek Koszuliński 9 years ago
parent
commit
88c733e8a9

+ 6 - 4
packages/ckeditor5-core/src/command/toggleattributecommand.js

@@ -76,13 +76,15 @@ export default class ToggleAttributeCommand extends Command {
 	 * If the command is disabled (`isEnabled == false`) when it is executed, nothing will happen.
 	 *
 	 * @private
-	 * @param {Boolean} [forceValue] If set it will force command behavior. If `true`, command will apply attribute,
+	 * @param {Object} [options] Options of command.
+	 * @param {Boolean} [options.forceValue] If set it will force command behavior. If `true`, command will apply attribute,
 	 * otherwise command will remove attribute. If not set, command will look for it's current value to decide what it should do.
+	 * @param {engine.model.Batch} [options.batch] Batch to group undo steps.
 	 */
-	_doExecute( forceValue ) {
+	_doExecute( options = {} ) {
 		const document = this.editor.document;
 		const selection = document.selection;
-		const value = ( forceValue === undefined ) ? !this.value : forceValue;
+		const value = ( options.forceValue === undefined ) ? !this.value : options.forceValue;
 
 		// If selection has non-collapsed ranges, we change attribute on nodes inside those ranges.
 		document.enqueueChanges( () => {
@@ -96,7 +98,7 @@ export default class ToggleAttributeCommand extends Command {
 				const ranges = getSchemaValidRanges( this.attributeKey, selection.getRanges(), document.schema );
 
 				// Keep it as one undo step.
-				const batch = document.batch();
+				const batch = options.batch || document.batch();
 
 				for ( let range of ranges ) {
 					if ( value ) {

+ 15 - 2
packages/ckeditor5-core/tests/command/toggleattributecommand.js

@@ -5,6 +5,7 @@
 
 import Editor from '/ckeditor5/core/editor/editor.js';
 import Document from '/ckeditor5/engine/model/document.js';
+import Batch from '/ckeditor5/engine/model/batch.js';
 import ToggleAttributeCommand from '/ckeditor5/core/command/toggleattributecommand.js';
 import Range from '/ckeditor5/engine/model/range.js';
 import Position from '/ckeditor5/engine/model/position.js';
@@ -80,7 +81,7 @@ describe( 'ToggleAttributeCommand', () => {
 
 			expect( command.value ).to.be.true;
 
-			command._doExecute( true );
+			command._doExecute( { forceValue: true } );
 
 			expect( command.value ).to.be.true;
 			expect( getData( modelDoc ) ).to.equal( '<p>abc<$text bold="true">foob[arx</$text>]yz</p>' );
@@ -89,7 +90,7 @@ describe( 'ToggleAttributeCommand', () => {
 		it( 'should remove attribute on selected nodes if execute parameter was set to false', () => {
 			setData( modelDoc, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
 
-			command._doExecute( false );
+			command._doExecute( { forceValue: false } );
 
 			expect( command.value ).to.be.false;
 			expect( getData( modelDoc ) ).to.equal( '<p>a[bcfo]<$text bold="true">obar</$text>xyz</p>' );
@@ -167,6 +168,18 @@ describe( 'ToggleAttributeCommand', () => {
 				.to.equal( '<p>ab[<$text bold="true">c</$text><image></image><$text bold="true">foobarxy</$text><image></image>]z</p>' );
 		} );
 
+		it( 'should use provided batch for storing undo steps', () => {
+			const batch = new Batch( new Document() );
+			setData( modelDoc, '<p>a[bc<$text bold="true">fo]obar</$text>xyz</p>' );
+
+			expect( batch.deltas.length ).to.equal( 0 );
+
+			command._doExecute( { batch } );
+
+			expect( batch.deltas.length ).to.equal( 1 );
+			expect( getData( modelDoc ) ).to.equal( '<p>a[<$text bold="true">bcfo]obar</$text>xyz</p>' );
+		} );
+
 		describe( 'should cause firing model document changesDone event', () => {
 			let spy;