diff --git a/CHANGES.md b/CHANGES.md index 24609e09658..25945112231 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,7 @@ New Features: * [#16847](http://dev.ckeditor.com/ticket/16847): Added a support for parsing and inlining any formatting created using Microsoft Word's style system to the [Paste from Word](http://ckeditor.com/addon/pastefromword) plugin. * [#16818](http://dev.ckeditor.com/ticket/16818): Added table cell height parsing in the [Paste from Word](http://ckeditor.com/addon/pastefromword) plugin. * [#16850](http://dev.ckeditor.com/ticket/16850): Added new `config.enableContextMenu` configuration option for enabling and disabling [Context Menu](http://ckeditor.com/addon/contextmenu). +* [#16937](http://dev.ckeditor.com/ticket/16937): The `command` parameter in the [CKEDITOR.editor.getCommandKeystroke](http://docs.ckeditor.dev/#!/api/CKEDITOR.editor-method-getCommandKeystroke) accepts also command name as an argument. Fixed Issues: diff --git a/core/editor.js b/core/editor.js index 8dfce7fed15..4e31a286e8e 100644 --- a/core/editor.js +++ b/core/editor.js @@ -1325,28 +1325,33 @@ /** * Returns the keystroke that is assigned to a specified {@link CKEDITOR.command}. If no keystroke is assigned, - * it returns null. + * it returns `null`. + * + * Since version 4.7.0 this function also accepts `command` parameter as a string. * * @since 4.6.0 - * @param {CKEDITOR.command} command - * @returns {Number} The keystroke assigned to the provided command or null if there is no keystroke. + * @param {CKEDITOR.command/String} command The {@link CKEDITOR.command} instance or a string with command name. + * @returns {Number/null} The keystroke assigned to the provided command or `null` if there is no keystroke. */ getCommandKeystroke: function( command ) { - var commandName = command.name, - keystrokes = this.keystrokeHandler.keystrokes, - key; + var commandInstance = ( typeof command === 'string' ? this.getCommand( command ) : command ); - // Some commands have a fake keystroke - for example CUT/COPY/PASTE commands are handled natively. - if ( command.fakeKeystroke ) { - return command.fakeKeystroke; - } + if ( commandInstance ) { + var commandName = commandInstance && commandInstance.name, + keystrokes = this.keystrokeHandler.keystrokes, + key; - for ( key in keystrokes ) { - if ( keystrokes.hasOwnProperty( key ) && keystrokes[ key ] == commandName ) { - return key; + // Some commands have a fake keystroke - for example CUT/COPY/PASTE commands are handled natively. + if ( commandInstance.fakeKeystroke ) { + return commandInstance.fakeKeystroke; } - } + for ( key in keystrokes ) { + if ( keystrokes.hasOwnProperty( key ) && keystrokes[ key ] == commandName ) { + return key; + } + } + } return null; }, diff --git a/tests/core/editor/keystrokehandler.js b/tests/core/editor/keystrokehandler.js index 4658cfc0648..14b6505cf9f 100644 --- a/tests/core/editor/keystrokehandler.js +++ b/tests/core/editor/keystrokehandler.js @@ -29,8 +29,14 @@ bender.test( editor.setKeystroke( keyCombo1, command1 ); assert.areEqual( command1, keystrokes[ keyCombo1 ] ); + + // Get by command instance. keystroke = editor.getCommandKeystroke( editor.getCommand( command1 ) ); - assert.areEqual( keyCombo1, keystroke, 'Keystrokes should be equal.' ); + assert.areEqual( keyCombo1, keystroke, 'Keystrokes should be equal (command).' ); + + // Get by command name. + keystroke = editor.getCommandKeystroke( command1 ); + assert.areEqual( keyCombo1, keystroke, 'Keystrokes should be equal (command name).' ); }, 'test keystroke array assignment': function() { @@ -51,10 +57,17 @@ bender.test( assert.areEqual( command1, keystrokes[ keyCombo1 ] ); assert.areEqual( command2, keystrokes[ keyCombo2 ] ); + // Get by command instance. keystroke1 = editor.getCommandKeystroke( editor.getCommand( command1 ) ); keystroke2 = editor.getCommandKeystroke( editor.getCommand( command2 ) ); - assert.areEqual( keyCombo1, keystroke1, 'Keystrokes should be equal.' ); - assert.areEqual( keyCombo2, keystroke2, 'Keystrokes should be equal.' ); + assert.areEqual( keyCombo1, keystroke1, 'Keystrokes should be equal (command).' ); + assert.areEqual( keyCombo2, keystroke2, 'Keystrokes should be equal (command).' ); + + // Get by command name. + keystroke1 = editor.getCommandKeystroke( command1 ); + keystroke2 = editor.getCommandKeystroke( command2 ); + assert.areEqual( keyCombo1, keystroke1, 'Keystrokes should be equal (command name).' ); + assert.areEqual( keyCombo2, keystroke2, 'Keystrokes should be equal (command name).' ); }, 'test editor#key event': function() { @@ -76,5 +89,10 @@ bender.test( assert.areSame( CKEDITOR.CTRL + CKEDITOR.SHIFT + 66, evtData.keyCode, 'keyCode' ); assert.isInstanceOf( CKEDITOR.dom.event, evtData.domEvent, 'domEvent' ); assert.areSame( 66, evtData.domEvent.getKey(), 'domEvent.getKey()' ); + }, + + 'test editor#getCommandKeystroke with empty name': function() { + var editor = this.editor; + assert.isNull( editor.getCommandKeystroke( '' ), 'Returned keystroke.' ); } } );