Skip to content

Commit a91dfb0

Browse files
committed
Automatically update command when added after instaceReady and make sure that commands added between #mode and #instanceReady are updated too.
1 parent 656c8fa commit a91dfb0

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

core/editor.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,18 @@
153153
this.keystrokeHandler = new CKEDITOR.keystrokeHandler( this );
154154

155155
// Make the editor update its command states on mode change.
156-
this.on( 'mode', updateCommands );
157156
this.on( 'readOnly', updateCommands );
158157
this.on( 'selectionChange', updateCommandsContext );
159158

160159
// Handle startup focus.
161160
this.on( 'instanceReady', function() {
161+
updateCommands.call( this );
162+
// First 'mode' event is fired before this 'instanceReady',
163+
// so to avoid updating commands twice, add this listener here.
164+
this.on( 'mode', updateCommands );
165+
162166
this.config.startupFocus && this.focus();
163-
});
167+
} );
164168

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

@@ -185,17 +189,18 @@
185189
}
186190

187191
function updateCommands() {
188-
var command,
189-
commands = this.commands,
192+
var commands = this.commands,
190193
mode = this.mode;
191194

192195
if ( !mode )
193196
return;
194197

195-
for ( var name in commands ) {
196-
command = commands[ name ];
197-
command[ command.startDisabled ? 'disable' : this.readOnly && !command.readOnly ? 'disable' : command.modes[ mode ] ? 'enable' : 'disable' ]();
198-
}
198+
for ( var name in commands )
199+
updateCommand( this, commands[ name ] );
200+
}
201+
202+
function updateCommand( editor, cmd ) {
203+
cmd[ cmd.startDisabled ? 'disable' : editor.readOnly && !cmd.readOnly ? 'disable' : cmd.modes[ editor.mode ] ? 'enable' : 'disable' ]();
199204
}
200205

201206
function updateCommandsContext( ev ) {
@@ -585,6 +590,11 @@
585590
addCommand: function( commandName, commandDefinition ) {
586591
commandDefinition.name = commandName.toLowerCase();
587592
var cmd = new CKEDITOR.command( this, commandDefinition );
593+
594+
// Update command when added after editor has been already initialized.
595+
if ( this.status == 'ready' && this.mode )
596+
updateCommand( this, cmd );
597+
588598
return this.commands[ commandName ] = cmd;
589599
},
590600

0 commit comments

Comments
 (0)