Skip to content

Commit

Permalink
Automatically update command when added after instaceReady and make s…
Browse files Browse the repository at this point in the history
…ure that commands added between #mode and #instanceReady are updated too.
  • Loading branch information
Reinmar committed Feb 21, 2013
1 parent 656c8fa commit a91dfb0
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions core/editor.js
Expand Up @@ -153,14 +153,18 @@
this.keystrokeHandler = new CKEDITOR.keystrokeHandler( this );

// Make the editor update its command states on mode change.
this.on( 'mode', updateCommands );
this.on( 'readOnly', updateCommands );
this.on( 'selectionChange', updateCommandsContext );

// Handle startup focus.
this.on( 'instanceReady', function() {
updateCommands.call( this );
// First 'mode' event is fired before this 'instanceReady',
// so to avoid updating commands twice, add this listener here.
this.on( 'mode', updateCommands );

this.config.startupFocus && this.focus();
});
} );

CKEDITOR.fire( 'instanceCreated', null, this );

Expand All @@ -185,17 +189,18 @@
}

function updateCommands() {
var command,
commands = this.commands,
var commands = this.commands,
mode = this.mode;

if ( !mode )
return;

for ( var name in commands ) {
command = commands[ name ];
command[ command.startDisabled ? 'disable' : this.readOnly && !command.readOnly ? 'disable' : command.modes[ mode ] ? 'enable' : 'disable' ]();
}
for ( var name in commands )
updateCommand( this, commands[ name ] );
}

function updateCommand( editor, cmd ) {
cmd[ cmd.startDisabled ? 'disable' : editor.readOnly && !cmd.readOnly ? 'disable' : cmd.modes[ editor.mode ] ? 'enable' : 'disable' ]();
}

function updateCommandsContext( ev ) {
Expand Down Expand Up @@ -585,6 +590,11 @@
addCommand: function( commandName, commandDefinition ) {
commandDefinition.name = commandName.toLowerCase();
var cmd = new CKEDITOR.command( this, commandDefinition );

// Update command when added after editor has been already initialized.
if ( this.status == 'ready' && this.mode )
updateCommand( this, cmd );

return this.commands[ commandName ] = cmd;
},

Expand Down

0 comments on commit a91dfb0

Please sign in to comment.