CKEDITOR.command
Represents a command that can be executed on an editor instance.
var command = new CKEDITOR.command( editor, {
exec: function( editor ) {
alert( editor.document.getBody().getHtml() );
}
} );
Filtering
Properties
async : BooleanCKEDITOR.command#asyncWhether the command is asynchronous, which means that the CKEDITOR.editor.afterCommandExec event will be fired by the command itself manually, and that the return value of this command is not to be returned by the exec function.
editorInstance.addCommand( 'loadoptions', { exec: function( editor ) { var cmd = this; // Asynchronous operation below. CKEDITOR.ajax.loadXml( 'data.xml', function() { editor.fire( 'afterCommandExec', { name: 'loadoptions', command: cmd } ); } ); }, async: true // The command needs some time to complete after the exec function returns. } );Defaults to
falsecanUndo : BooleanCKEDITOR.command#canUndoWhether the command needs to be hooked into the redo/undo system.
editorInstance.addCommand( 'alertName', { exec: function( editor ) { alert( editor.name ); }, canUndo: false // No support for undo/redo. } );Defaults to
truecontext : BooleanCKEDITOR.command#contextSets the element name used to reflect the command state on selection changes. If the selection is in a place where the element is not allowed, the command will be disabled. Setting this property overrides contextSensitive to
true.Defaults to
true-
Indicates that this command is sensitive to the selection context. If
true, the CKEDITOR.command.refresh method will be called for this command on selection changes, with a single parameter representing the current elements path.Defaults to
true -
Whether the command should give focus to the editor before execution.
editorInstance.addCommand( 'maximize', { exec: function( editor ) { // ... }, editorFocus: false // The command does not require focusing the editing document. } );See also CKEDITOR.command.editorFocus.
Defaults to
true -
A property that should be set when a command has no keystroke assigned by CKEDITOR.editor.setKeystroke, but the keystroke is still supported. For example:
cut,copyandpastecommands are handled that way. This property is used when displaying keystroke information in tooltips and context menus. It is used by CKEDITOR.editor.getCommandKeystroke. -
The editor modes within which the command can be executed. The execution will have no action if the current mode is not listed in this property.
editorInstance.addCommand( 'link', { exec: function( editor ) { // ... }, modes: { wysiwyg:1 } // The command is available in wysiwyg mode only. } );See also CKEDITOR.command.modes.
Defaults to
{ wysiwyg:1 } previousState : NumberCKEDITOR.command#previousState-
startDisabled : BooleanCKEDITOR.command#startDisabledWhether the command state should be set to CKEDITOR.TRISTATE_DISABLED on startup.
editorInstance.addCommand( 'unlink', { exec: function( editor ) { // ... }, startDisabled: true // The command is unavailable until the selection is inside a link. } );Defaults to
falsestate : NumberCKEDITOR.command#stateIndicates the editor state. Possible values are:
- CKEDITOR.TRISTATE_DISABLED: the command is disabled. It's execution will have no effect. Same as disable.
- CKEDITOR.TRISTATE_ON: the command is enabled and currently active in the editor (for context sensitive commands, for example).
- CKEDITOR.TRISTATE_OFF: the command is enabled and currently inactive in the editor (for context sensitive commands, for example).
Do not set this property directly, using the setState method instead.
if ( command.state == CKEDITOR.TRISTATE_DISABLED ) alert( 'This command is disabled' );Defaults to
CKEDITOR.TRISTATE_DISABLEDuiItems : ArrayCKEDITOR.command#uiItemsLists UI items that are associated to this command. This list can be used to interact with the UI on command execution (by the execution code itself, for example).
alert( 'Number of UI items associated to this command: ' + command.uiItems.length );Defaults to
[]
Static properties
Methods
constructor( editor, commandDefinition ) → commandCKEDITOR.command#constructorCreates a command class instance.
Parameters
editor : editorThe editor instance this command will be related to.
commandDefinition : commandDefinitionThe command definition.
Returns
command
capture()CKEDITOR.command#captureRegister event handler under the capturing stage on supported target.
-
Checks whether this command is allowed by the active allowed content filter (CKEDITOR.editor.activeFilter). This means that if command implements CKEDITOR.feature interface it will be tested by the CKEDITOR.filter.checkFeature method.
Parameters
[ noCache ] : BooleanSkip cache for example due to active filter change. Since CKEditor 4.2.0.
Returns
BooleanWhether this command is allowed.
define( name, meta )CKEDITOR.command#definePredefine some intrinsic properties on a specific event name.
Parameters
name : StringThe event name
meta : Object
disable()CKEDITOR.command#disableDisables the command for execution. The command state (see state) will be set to CKEDITOR.TRISTATE_DISABLED.
command.disable(); command.exec(); // "false" - Nothing happens.enable()CKEDITOR.command#enableEnables the command for execution. The command state (see state) available before disabling it is restored.
command.enable(); command.exec(); // Execute the command.-
The function to be fired when the commend is executed.
editorInstance.addCommand( 'sample', { exec: function( editor ) { alert( 'Executing a command for the editor name "' + editor.name + '"!' ); } } );Parameters
editor : editorThe editor within which to run the command.
[ data ] : ObjectAdditional data to be used to execute the command.
Returns
BooleanWhether the command has been successfully executed. Defaults to
trueif nothing is returned.
fire( eventName, [ data ], [ editor ] ) → Boolean | ObjectCKEDITOR.command#fireFires an specific event in the object. All registered listeners are called at this point.
someObject.on( 'someEvent', function() { ... } ); someObject.on( 'someEvent', function() { ... } ); someObject.fire( 'someEvent' ); // Both listeners are called. someObject.on( 'someEvent', function( event ) { alert( event.data ); // 'Example' } ); someObject.fire( 'someEvent', 'Example' );Parameters
eventName : StringThe event name to fire.
[ data ] : ObjectData to be sent as the CKEDITOR.eventInfo.data when calling the listeners.
[ editor ] : editorThe editor instance to send as the CKEDITOR.eventInfo.editor when calling the listener.
Returns
Boolean | ObjectA boolean indicating that the event is to be canceled, or data returned by one of the listeners.
fireOnce( eventName, [ data ], [ editor ] ) → Boolean | ObjectCKEDITOR.command#fireOnceFires an specific event in the object, releasing all listeners registered to that event. The same listeners are not called again on successive calls of it or of fire.
someObject.on( 'someEvent', function() { ... } ); someObject.fire( 'someEvent' ); // Above listener called. someObject.fireOnce( 'someEvent' ); // Above listener called. someObject.fire( 'someEvent' ); // No listeners called.Parameters
eventName : StringThe event name to fire.
[ data ] : ObjectData to be sent as the CKEDITOR.eventInfo.data when calling the listeners.
[ editor ] : editorThe editor instance to send as the CKEDITOR.eventInfo.editor when calling the listener.
Returns
Boolean | ObjectA booloan indicating that the event is to be canceled, or data returned by one of the listeners.
hasListeners( eventName ) → BooleanCKEDITOR.command#hasListenersChecks if there is any listener registered to a given event.
var myListener = function() { ... }; someObject.on( 'someEvent', myListener ); alert( someObject.hasListeners( 'someEvent' ) ); // true alert( someObject.hasListeners( 'noEvent' ) ); // falseParameters
eventName : StringThe event name.
Returns
Boolean
on( eventName, listenerFunction, [ scopeObj ], [ listenerData ], [ priority ] ) → ObjectCKEDITOR.command#onRegisters a listener to a specific event in the current object.
someObject.on( 'someEvent', function() { alert( this == someObject ); // true } ); someObject.on( 'someEvent', function() { alert( this == anotherObject ); // true }, anotherObject ); someObject.on( 'someEvent', function( event ) { alert( event.listenerData ); // 'Example' }, null, 'Example' ); someObject.on( 'someEvent', function() { ... } ); // 2nd called someObject.on( 'someEvent', function() { ... }, null, null, 100 ); // 3rd called someObject.on( 'someEvent', function() { ... }, null, null, 1 ); // 1st calledNote: CKEditor's event system has a limitation that one function cannot be used as a listener for the same event more than once. Hence, to reuse it with multiple listeners, it should be wrapped into additional wrapper function:
function listener( evt ) { ... }; someObject.on( 'someEvent', function() { listener(); } ); someObject.on( 'someEvent', function( evt ) { listener( evt ); } );Parameters
eventName : StringThe event name to which listen.
listenerFunction : FunctionThe function listening to the event. A single CKEDITOR.eventInfo object instanced is passed to this function containing all the event data.
[ scopeObj ] : ObjectThe object used to scope the listener call (the
thisobject). If omitted, the current object is used.[ listenerData ] : ObjectData to be sent as the CKEDITOR.eventInfo.listenerData when calling the listener.
[ priority ] : NumberThe listener priority. Lower priority listeners are called first. Listeners with the same priority value are called in registration order.
Defaults to
10
Returns
ObjectAn object containing the
removeListenerfunction, which can be used to remove the listener at any time.
once()CKEDITOR.command#onceSimiliar with on but the listener will be called only once upon the next event firing.
-
Defined by the command definition, a function to determine the command state. It will be invoked when the editor has its
statesorselectionchanged.Note: The function provided must be calling CKEDITOR.command.setState in all circumstances if it is intended to update the command state.
Parameters
editor : editorpath : elementPath
removeAllListeners()CKEDITOR.command#removeAllListenersRemove all existing listeners on this object, for cleanup purpose.
removeListener( eventName, listenerFunction )CKEDITOR.command#removeListenerUnregisters a listener function from being called at the specified event. No errors are thrown if the listener has not been registered previously.
var myListener = function() { ... }; someObject.on( 'someEvent', myListener ); someObject.fire( 'someEvent' ); // myListener called. someObject.removeListener( 'someEvent', myListener ); someObject.fire( 'someEvent' ); // myListener not called.Parameters
eventName : StringThe event name.
listenerFunction : FunctionThe listener function to unregister.
setState( newState ) → BooleanCKEDITOR.command#setStateSets the command state.
command.setState( CKEDITOR.TRISTATE_ON ); command.exec(); // Execute the command. command.setState( CKEDITOR.TRISTATE_DISABLED ); command.exec(); // 'false' - Nothing happens. command.setState( CKEDITOR.TRISTATE_OFF ); command.exec(); // Execute the command.Parameters
newState : NumberThe new state. See state.
Returns
BooleanReturns
trueif the command state changed.
toggleState()CKEDITOR.command#toggleStateToggles the on/off (active/inactive) state of the command. This is mainly used internally by context sensitive commands.
command.toggleState();
Static methods
-
Implements the CKEDITOR.event features in an object.
var myObject = { message: 'Example' }; CKEDITOR.event.implementOn( myObject ); myObject.on( 'testEvent', function() { alert( this.message ); } ); myObject.fire( 'testEvent' ); // 'Example'Parameters
targetObject : ObjectThe object into which implement the features.
Events
state( evt )CKEDITOR.command#stateFired when the command state changes.
command.on( 'state', function() { // Alerts the new state. alert( this.state ); } );Parameters
evt : eventInfo