diff --git a/CHANGES.md b/CHANGES.md index 4a449b86576..7f508f0b8c2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,10 @@ Fixed Issues: * [#1776](https://github.com/ckeditor/ckeditor-dev/issues/1776): [Image Base](https://ckeditor.com/cke4/addon/imagebase) empty caption placeholder is not hidden when blurred. * [#1592](https://github.com/ckeditor/ckeditor-dev/issues/1592): [Image Base](https://ckeditor.com/cke4/addon/imagebase) caption is not visible after paste. +API Changes: + +* [#1582](https://github.com/ckeditor/ckeditor-dev/issues/1582): The [`CKEDITOR.editor.addCommand`](https://docs.ckeditor.com/ckeditor4/latest/api/CKEDITOR_editor.html#method-addCommand) can accept [`CKEDITOR.command`](https://docs.ckeditor.com/ckeditor4/latest/api/CKEDITOR_command.html) instance as parameter. + ## CKEditor 4.9 New Features: diff --git a/core/editor.js b/core/editor.js index 568dc313678..c7d2686720c 100644 --- a/core/editor.js +++ b/core/editor.js @@ -740,12 +740,14 @@ * } * } ); * + * Since 4.10.0 this method also accepts `CKEDITOR.command` instance as param. + * * @param {String} commandName The indentifier name of the command. - * @param {CKEDITOR.commandDefinition} commandDefinition The command definition. + * @param {CKEDITOR.commandDefinition/CKEDITOR.command} commandDefinition The command definition or the command instance. */ addCommand: function( commandName, commandDefinition ) { commandDefinition.name = commandName.toLowerCase(); - var cmd = new CKEDITOR.command( this, commandDefinition ); + var cmd = commandDefinition instanceof CKEDITOR.command ? commandDefinition : new CKEDITOR.command( this, commandDefinition ); // Update command when mode is set. // This guarantees that commands added before first editor#mode diff --git a/tests/core/command/command.js b/tests/core/command/command.js index c2fb0c7b8aa..ed3bfbf0cde 100644 --- a/tests/core/command/command.js +++ b/tests/core/command/command.js @@ -9,6 +9,20 @@ var READ_ONLY_CMDS = [ 'preview', 'print', 'showblocks', 'showborders', 'source', 'toolbarCollapse', 'toolbarFocus', 'selectAll' ]; +function assertCommand( editor, cmd, commandDefinition ) { + // Register command from command instance. + editor.addCommand( 'cmd1', cmd ); + + // Register command from command definition. + editor.addCommand( 'cmd2', commandDefinition ); + + // Registered command should be same as the command that was passed as definition. + assert.areSame( editor.getCommand( 'cmd1' ), cmd ); + + // Registered command should't be same to another command with same definition. + assert.areNotSame( editor.getCommand( 'cmd2' ), cmd ); +} + bender.editor = true; bender.test( { @@ -302,5 +316,38 @@ bender.test( { editor._.elementsPath.onClick( 0 ); wait(); + }, + + // (#1582) + 'test addCommand from command instance': function() { + var editor = this.editor, + styleDefinition = { + element: 'span', + attributes: { + bar: 'foo' + } + }, + style = new CKEDITOR.style( styleDefinition ), + commandDefinition = new CKEDITOR.styleCommand( style ), + cmd = new CKEDITOR.command( editor, commandDefinition ); + + assertCommand( editor, cmd, commandDefinition ); + }, + + // (#1582) + 'test addCommand from command subclass': function() { + var editor = this.editor, + styleDefinition = { + element: 'span', + attributes: { + bar: 'foo' + } + }, + subCommand = CKEDITOR.tools.createClass( { base: CKEDITOR.command } ), + style = new CKEDITOR.style( styleDefinition ), + commandDefinition = new CKEDITOR.styleCommand( style ), + cmd = new subCommand( editor, commandDefinition ); + + assertCommand( editor, cmd, commandDefinition ); } } );