Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

T/ckeditor5/1636: MultiCommand #179

Merged
merged 32 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
fdb0c86
Multi command proof of concept.
jodator Jun 6, 2019
5f6039e
Add indent/outdent buttons stubs.
jodator Jun 6, 2019
c7a9f7b
Stub indent conversion.
jodator Jun 6, 2019
db2d11f
Prepare buttons POC.
jodator Jun 7, 2019
e83aaac
Remove block indentation POC.
jodator Jun 7, 2019
7833999
Extract Indent plugin stub.
jodator Jun 11, 2019
a53d8c5
Add docs for indent plugin.
jodator Jun 12, 2019
efc9fa0
Remove .only from tests.
jodator Jun 12, 2019
2c35198
Update docs.
jodator Jun 12, 2019
c93c280
Merge branch 'master' into t/ckeditor5/1636
jodator Jun 13, 2019
618b31a
Add missing test for MultiCommand.
jodator Jun 13, 2019
900aeaf
Add missing test for indent.
jodator Jun 13, 2019
79a6aa4
Split the Indent plugin to Indent, IndentEditing and IndentUI.
jodator Jun 14, 2019
569eba3
Use VirtualTestEditor in IndentEditing tests.
jodator Jun 14, 2019
b1e2f8d
Move Indent* plugins to indent/Indent.
jodator Jun 14, 2019
7e7b4c7
Add example to MultiCommand.
jodator Jun 14, 2019
dd39d16
Remove invalid itself inports.
jodator Jun 14, 2019
94f0edb
Merge branch 'master' into t/ckeditor5/1636
Reinmar Jun 26, 2019
ccb9c2c
Update src/multicommand.js
jodator Jun 26, 2019
e970541
Update src/multicommand.js
jodator Jun 26, 2019
d531bb9
Update src/multicommand.js
jodator Jun 26, 2019
2ef389a
Update src/multicommand.js
jodator Jun 26, 2019
7340ad2
Update src/indent/indent.js
jodator Jun 26, 2019
dafe54d
Update src/indent/indent.js
jodator Jun 26, 2019
e1661d0
Update src/indent/indent.js
jodator Jun 26, 2019
f2296c8
Update src/indent/indent.js
jodator Jun 26, 2019
401ac7a
Update src/indent/indent.js
jodator Jun 26, 2019
75b3176
Move Indent* classes from core to intend plugin.
jodator Jun 26, 2019
3849a8c
Use an Array for internal commands store in MultiCommand.
jodator Jun 27, 2019
f23cd70
Move Indent* tests to indent also.
jodator Jun 27, 2019
d8517ce
Revert article manual tests html file.
jodator Jun 27, 2019
0b23e78
Remove useless import from MultiCommand.
jodator Jun 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions src/multicommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

import Command from './command';

/**
* @module core/multicommand
*/

/**
* A CKEditor command that aggregates other commands.
*
* This command is used to proxy multiple commands. The multi-command is enabled when
* at least one of its registered child commands is enabled.
* When executing a multi-command the first command that is enabled will be executed.
*
* const multiCommand = new MultiCommand( editor );
*
* const commandFoo = new Command( editor );
* const commandBar = new Command( editor );
*
* // Register child commands.
* multiCommand.registerChildCommand( commandFoo );
* multiCommand.registerChildCommand( commandBar );
*
* // Enable one of the commands.
* commandBar.isEnabled = true;
*
* multiCommand.execute(); // Will execute commandBar.
*
* @extends module:core/command~Command
*/
export default class MultiCommand extends Command {
/**
* @inheritDoc
*/
constructor( editor ) {
super( editor );

/**
* Registered child commands.
*
* @type {Array.<module:core/command~Command>}
* @private
*/
this._childCommands = [];
}

/**
* @inheritDoc
*/
refresh() {
// Override base command refresh(): the command's state is changed when one of child commands changes states.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I became unsure about this. I'll report a followup, though.

}

/**
* Executes the first of it registered child commands.
*/
execute( ...args ) {
const command = this._getFirstEnabledCommand();

command.execute( args );
}

/**
* Registers a child command.
*
* @param {module:core/command~Command} command
*/
registerChildCommand( command ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just addChildCommand()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although, "register" reads quite well in the docs. So I don't have a strong opinion on that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "register" implies something more than just adding it (like adding to a collection). So if it also reads well in the docs I'll leave it as is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. We actually start watching those commands.

this._childCommands.push( command );

// Change multi command enabled state when one of registered commands changes state.
command.on( 'change:isEnabled', () => this._checkEnabled() );

this._checkEnabled();
}

/**
* Checks if any of child commands is enabled.
*
* @private
*/
_checkEnabled() {
this.isEnabled = !!this._getFirstEnabledCommand();
}

/**
* Returns a first enabled command or undefined if none of them is enabled.
*
* @returns {module:core/command~Command|undefined}
* @private
*/
_getFirstEnabledCommand() {
return this._childCommands.find( command => command.isEnabled );
}
}
124 changes: 124 additions & 0 deletions tests/multicommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

import MultiCommand from '../src/multicommand';
import ModelTestEditor from './_utils/modeltesteditor';
import Command from '../src/command';

describe( 'MultiCommand', () => {
let editor, multiCommand;

beforeEach( () => {
return ModelTestEditor
.create()
.then( newEditor => {
editor = newEditor;
multiCommand = new MultiCommand( editor );
} );
} );

afterEach( () => {
multiCommand.destroy();

return editor.destroy();
} );

describe( 'isEnabled', () => {
it( 'is always falsy when no child commands are registered', () => {
expect( multiCommand.isEnabled ).to.false;

multiCommand.refresh();

expect( multiCommand.isEnabled ).to.false;
} );

it( 'is set to true if one of registered commands is true', () => {
expect( multiCommand.isEnabled ).to.false;

const commandA = new Command( editor );
const commandB = new Command( editor );

multiCommand.registerChildCommand( commandA );
multiCommand.registerChildCommand( commandB );

expect( multiCommand.isEnabled ).to.false;

commandA.isEnabled = true;

expect( multiCommand.isEnabled ).to.be.true;

commandA.isEnabled = false;

expect( multiCommand.isEnabled ).to.be.false;

commandB.isEnabled = true;

expect( multiCommand.isEnabled ).to.be.true;
} );
} );

describe( 'execute()', () => {
it( 'does not call any command if all are disabled', () => {
const commandA = new Command( editor );
const commandB = new Command( editor );

multiCommand.registerChildCommand( commandA );
multiCommand.registerChildCommand( commandB );

const spyA = sinon.spy( commandA, 'execute' );
const spyB = sinon.spy( commandB, 'execute' );

multiCommand.execute();

sinon.assert.notCalled( spyA );
sinon.assert.notCalled( spyB );
} );

it( 'executes enabled command', () => {
const commandA = new Command( editor );
const commandB = new Command( editor );
const commandC = new Command( editor );

multiCommand.registerChildCommand( commandA );
multiCommand.registerChildCommand( commandB );
multiCommand.registerChildCommand( commandC );

const spyA = sinon.spy( commandA, 'execute' );
const spyB = sinon.spy( commandB, 'execute' );
const spyC = sinon.spy( commandC, 'execute' );

commandC.isEnabled = true;

multiCommand.execute();

sinon.assert.notCalled( spyA );
sinon.assert.notCalled( spyB );
sinon.assert.calledOnce( spyC );
} );

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

multiCommand.registerChildCommand( commandA );
multiCommand.registerChildCommand( commandB );
multiCommand.registerChildCommand( commandC );

const spyA = sinon.spy( commandA, 'execute' );
const spyB = sinon.spy( commandB, 'execute' );
const spyC = sinon.spy( commandC, 'execute' );

commandC.isEnabled = true;
commandB.isEnabled = true;

multiCommand.execute();

sinon.assert.notCalled( spyA );
sinon.assert.calledOnce( spyB );
sinon.assert.notCalled( spyC );
} );
} );
} );