瀏覽代碼

Changed core.command.Command refreshState event to use object-reference to pass value.

Szymon Cofalik 9 年之前
父節點
當前提交
cbaacfbad6
共有 2 個文件被更改,包括 15 次插入30 次删除
  1. 8 9
      packages/ckeditor5-engine/src/command/command.js
  2. 7 21
      packages/ckeditor5-engine/tests/commands/command.js

+ 8 - 9
packages/ckeditor5-engine/src/command/command.js

@@ -45,10 +45,8 @@ export default class Command {
 		// If schema checking function is specified, add it to the `refreshState` listeners.
 		// Feature will be disabled if it does not apply to schema requirements.
 		if ( this.checkEnabled ) {
-			this.on( 'refreshState', ( evt ) => {
-				if ( !this.checkEnabled() ) {
-					return disableCallback( evt );
-				}
+			this.on( 'refreshState', ( evt, data ) => {
+				data.isEnabled = this.checkEnabled();
 			} );
 		}
 	}
@@ -70,7 +68,10 @@ export default class Command {
 	 * @fires {@link core.command.Command.refreshState refreshState}
 	 */
 	refreshState() {
-		this.isEnabled = this.fire( 'refreshState' ) !== false;
+		const data = { isEnabled: true };
+		this.fire( 'refreshState', data );
+
+		this.isEnabled = data.isEnabled;
 	}
 
 	/**
@@ -118,10 +119,8 @@ export default class Command {
 	}
 }
 
-function disableCallback( evt ) {
-	evt.stop();
-
-	return false;
+function disableCallback( evt, data ) {
+	data.isEnabled = false;
 }
 
 utils.mix( Command, EmitterMixin );

+ 7 - 21
packages/ckeditor5-engine/tests/commands/command.js

@@ -69,27 +69,15 @@ describe( 'refreshState', () => {
 		expect( spy.called ).to.be.true;
 	} );
 
-	it( 'should set isEnabled property to the value returned by last event callback', () => {
-		command.on( 'refreshState', () => {
-			return true;
-		} );
-
-		command.on( 'refreshState', ( evt ) => {
-			evt.stop();
-
-			return false;
-		} );
-
-		command.on( 'refreshState', () => {
-			return true;
+	it( 'should set isEnabled property to the value passed by object-reference', () => {
+		command.on( 'refreshState', ( evt, data ) => {
+			data.isEnabled = true;
 		} );
 
-		command.refreshState();
-
-		expect( command.isEnabled ).to.be.false;
+		expect( command.isEnabled ).to.be.true;
 	} );
 
-	it( 'should set isEnabled to false if checkSchema returns false', () => {
+	it( 'should set isEnabled to false if checkEnabled returns false', () => {
 		let disabledCommand = new CommandWithSchema( editor, false );
 
 		disabledCommand.refreshState();
@@ -129,10 +117,8 @@ describe( 'enable', () => {
 	it( 'should not make command enabled if there are other listeners disabling it', () => {
 		command._disable();
 
-		command.on( 'refreshState', ( evt ) => {
-			evt.stop();
-
-			return false;
+		command.on( 'refreshState', ( evt, data ) => {
+			data.isEnabled = false;
 		} );
 
 		command.refreshState();