Hi there,
i want to cancel a command with "beforeCommandExec"
editor.on('beforeCommandExec', function(evt){
return false;
}It looks like CKEditor igores the false return and execute the command anyway.
https://github.com/ckeditor/ckeditor-dev/blob/master/core/editor.js#L821
Is it possible a bug?
Should it be with out the !== true check?
if ( command && command.state != CKEDITOR.TRISTATE_DISABLED ) {
if ( this.fire( 'beforeCommandExec', eventData )) {

Looking into your code,
Looking into your code, should it be?
editor.on( 'beforeCommandExec', function ( evt ) { evt.cancel(); }, null, null, 0 );Sample code:
Sample code:
editor.on( 'beforeCommandExec', function ( evt ) { var mode = evt.data.commandData; // Do NOT overwrite if HTML format is explicitly requested. if ( evt.data.name == 'paste' && mode != 'html' ) { editor.execCommand( 'pastetext' ); evt.cancel(); } }, null, null, 0 );Thanks for reply.
Thanks for reply.
I didn't test it yet, but i think
cancels the "beforeCommandExec" event and not, from your example code, the "paste" event.
-- edit
editor.on( 'beforeCommandExec', function ( evt ) { var mode = evt.data.commandData; // Do NOT overwrite if HTML format is explicitly requested. if ( evt.data.name == 'paste' && mode != 'html' ) { editor.execCommand( 'pastetext' ); // evt.name = "beforeCommandExec" and not "paste" evt.cancel(); // cancel the "beforeCommandExec" } }, null, null, 0 );Its should cancel the "beforeCommandExec" event not the "paste" event.
The function returns "undefined" and thats will changed to "false" (CKEDITOR.event.fire)
false !== true -> true, so exec() ist called.
That is just an example. :)
That is just an example. :) for a better idea...
Your example looks similar to
Your example looks similar to my code ;)
My purpose is to cancel the enter/shiftEnter command on certains elements.
My full code:
(function(){ // Check if selection is a figure or figurecaption var isFigure = function(e){ var selection = e.getSelection().getStartElement(); var name = selection ? selection.getName() : ""; return (name == "figure" || name == "figcaption"); } CKEDITOR.plugins.registered.enterkey.beforeInit = function(editor){ editor.on('beforeCommandExec', function( if((evt.data.name == 'enter' || evt.data.name == 'shiftEnter') && isFigure(e)){ // evt.cancel(); not working here return false; } return true; }); } })();But let see https://github.com/ckeditor/ckeditor-dev/blob/master/core/editor.js#L821 again
if (this.fire( 'beforeCommandExec', eventData ) !== true ) { eventData.returnValue = command.exec( eventData.commandData ); // Fire the 'afterCommandExec' immediately if command is synchronous. if ( !command.async && this.fire( 'afterCommandExec', eventData ) !== true ) return eventData.returnValue; }If beforeCommandExec return false, this.fire('beforeCommandExec', eventData) returns also false
If beforeCommandExec return true, this.fire('beforeCommandExec', eventData) returns an [Object obj]
if ( false !== true ) { // No canceling returnValue = command.exec( eventData.commandData ); } if( someObject !== true){ // also no canceling returnValue = command.exec( eventData.commandData ); }You can see the condition is totally useless, because its everytime "true"
Bug confirmed
Bug was reported and confirmed:
https://dev.ckeditor.com/ticket/11413
Will probably fixed in CKEditor 4.3.3
My temporary fix for this issue:
(function(){ /** * Enterkey: No enter/shift event on figure or figurecaption */ CKEDITOR.plugins.registered.enterkey.beforeInit = function(editor) { // Check if selection is a figure or figurecaption var isFigure = function(e) { var selection = e.getSelection().getStartElement(); var name = selection ? selection.getName() : ""; return (name == "figure" || name == "figcaption"); } editor.on('key', function(evt) { var keyStroke = evt.data.keyCode; // enter or shift + enter if ((keyStroke === 13 || keyStroke === CKEDITOR.SHIFT + 13) && isFigure(editor)) { evt.cancel(); } }); } });Thread can be closed