8
0
Quellcode durchsuchen

Introduce forceValue parameter to TodoListCheckCommand.

Oskar Wróbel vor 6 Jahren
Ursprung
Commit
b0aae830ec

+ 10 - 3
packages/ckeditor5-list/src/todolistcheckcommand.js

@@ -90,12 +90,19 @@ export default class TodoListCheckCommand extends Command {
 	}
 
 	/**
-	 * @inheritDoc
+	 * Executes the command.
+	 *
+	 * @param {Object} [options]
+	 * @param {Boolean} [options.forceValue] If set, it will force the command behavior. If `true`, the command will apply
+	 * the attribute, otherwise the command will remove the attribute. If not set, the command will look for its current
+	 * value to decide what it should do.
 	 */
-	execute() {
+	execute( options = {} ) {
 		this.editor.model.change( writer => {
 			for ( const element of this._selectedElements ) {
-				if ( !this.value ) {
+				const value = ( options.forceValue === undefined ) ? !this.value : options.forceValue;
+
+				if ( value ) {
 					writer.setAttribute( attributeKey, true, element );
 				} else {
 					writer.removeAttribute( attributeKey, element );

+ 20 - 0
packages/ckeditor5-list/tests/todolistcheckcommand.js

@@ -216,5 +216,25 @@ describe( 'TodoListCheckCommand', () => {
 				command.execute();
 			} );
 		} );
+
+		it( 'should set attribute if `forceValue` parameter is set to `true`', () => {
+			setModelData( model, '<listItem listIndent="0" listType="todo" todoListChecked="true">b[]ar</listItem>' );
+
+			command.execute( { forceValue: true } );
+
+			expect( getModelData( model ) ).to.equal(
+				'<listItem listIndent="0" listType="todo" todoListChecked="true">b[]ar</listItem>'
+			);
+		} );
+
+		it( 'should remove attribute if `forceValue` parameter is set to `false`', () => {
+			setModelData( model, '<listItem listIndent="0" listType="todo">b[]ar</listItem>' );
+
+			command.execute( { forceValue: false } );
+
+			expect( getModelData( model ) ).to.equal(
+				'<listItem listIndent="0" listType="todo">b[]ar</listItem>'
+			);
+		} );
 	} );
 } );