Skip to content

Commit

Permalink
Merge pull request #7651 from ckeditor/i/7647-execute
Browse files Browse the repository at this point in the history
Other: The `Editor`, `CommandCollection` and `MultiCommand`'s `execute()` method will the result of the called `command.execute()`. Closes #7647.
  • Loading branch information
jodator authored Jul 22, 2020
2 parents 4a12d38 + 8579bd5 commit 152ffc9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/ckeditor5-core/src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ export default class Command {
*
* In order to see how to disable a command from "outside" see the {@link #isEnabled} documentation.
*
* This method may return a value, which would be forwarded all the way down to the
* {@link module:core/editor/editor~Editor#execute `editor.execute()`}.
*
* @fires execute
*/
execute() {}
Expand Down
3 changes: 2 additions & 1 deletion packages/ckeditor5-core/src/commandcollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default class CommandCollection {
*
* @param {String} commandName The name of the command.
* @param {*} [...commandParams] Command parameters.
* @returns {*} The value returned by the {@link module:core/command~Command#execute `command.execute()`}.
*/
execute( commandName, ...args ) {
const command = this.get( commandName );
Expand All @@ -65,7 +66,7 @@ export default class CommandCollection {
throw new CKEditorError( 'commandcollection-command-not-found: Command does not exist.', this, { commandName } );
}

command.execute( ...args );
return command.execute( ...args );
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/ckeditor5-core/src/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,11 @@ export default class Editor {
*
* @param {String} commandName The name of the command to execute.
* @param {*} [...commandParams] Command parameters.
* @returns {*} The value returned by the {@link module:core/commandcollection~CommandCollection#execute `commands.execute()`}.
*/
execute( ...args ) {
try {
this.commands.execute( ...args );
return this.commands.execute( ...args );
} catch ( err ) {
// @if CK_DEBUG // throw err;
/* istanbul ignore next */
Expand Down
4 changes: 3 additions & 1 deletion packages/ckeditor5-core/src/multicommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ export default class MultiCommand extends Command {

/**
* Executes the first of it registered child commands.
*
* @returns {*} The value returned by the {@link module:core/command~Command#execute `command.execute()`}.
*/
execute( ...args ) {
const command = this._getFirstEnabledCommand();

command.execute( args );
return command.execute( args );
}

/**
Expand Down
14 changes: 14 additions & 0 deletions packages/ckeditor5-core/tests/commandcollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ describe( 'CommandCollection', () => {
expect( command.execute.args[ 0 ] ).to.deep.equal( [ 1, 2 ] );
} );

it( 'returns the result of command\'s execute()', () => {
const command = new SomeCommand( editor );

const commandResult = { foo: 'bar' };
sinon.stub( command, 'execute' ).returns( commandResult );

collection.add( 'foo', command );

const collectionResult = collection.execute( 'foo' );

expect( collectionResult, 'collection.execute()' ).to.equal( commandResult );
expect( collectionResult, 'collection.execute()' ).to.deep.equal( { foo: 'bar' } );
} );

it( 'throws an error if command does not exist', () => {
const command = new SomeCommand( editor );
collection.add( 'bar', command );
Expand Down
19 changes: 19 additions & 0 deletions packages/ckeditor5-core/tests/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,25 @@ describe( 'Editor', () => {
expect( command.execute.calledOnce ).to.be.true;
} );

it( 'should return the result of command\'s execute()', () => {
class SomeCommand extends Command {
execute() {}
}

const editor = new TestEditor();
const command = new SomeCommand( editor );

const commandResult = { foo: 'bar' };
sinon.stub( command, 'execute' ).returns( commandResult );

editor.commands.add( 'someCommand', command );

const editorResult = editor.execute( 'someCommand' );

expect( editorResult, 'editor.execute()' ).to.equal( commandResult );
expect( editorResult, 'editor.execute()' ).to.deep.equal( { foo: 'bar' } );
} );

it( 'should throw an error if specified command has not been added', () => {
const editor = new TestEditor();

Expand Down
15 changes: 15 additions & 0 deletions packages/ckeditor5-core/tests/multicommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ describe( 'MultiCommand', () => {
sinon.assert.calledOnce( spyC );
} );

it( 'returns the result of command\'s execute()', () => {
const command = new Command( editor );
const commandResult = { foo: 'bar' };
sinon.stub( command, 'execute' ).returns( commandResult );

multiCommand.registerChildCommand( command );

command.isEnabled = true;

const multiCommandResult = multiCommand.execute();

expect( multiCommandResult, 'multiCommand.execute()' ).to.equal( commandResult );
expect( multiCommandResult, 'multiCommand.execute()' ).to.deep.equal( { foo: 'bar' } );
} );

it( 'executes first registered command if many are enabled', () => {
const commandA = new Command( editor );
const commandB = new Command( editor );
Expand Down

0 comments on commit 152ffc9

Please sign in to comment.