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 : Boolean
CKEDITOR.command#async
Whether 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
false
-
canUndo : Boolean
CKEDITOR.command#canUndo
Whether 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
true
-
context : Boolean
CKEDITOR.command#context
Sets 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
,copy
andpaste
commands 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 : Number
CKEDITOR.command#previousState
-
-
startDisabled : Boolean
CKEDITOR.command#startDisabled
Whether 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
false
-
state : Number
CKEDITOR.command#state
Indicates 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_DISABLED
-
uiItems : Array
CKEDITOR.command#uiItems
Lists 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 ) → command
CKEDITOR.command#constructor
Creates a command class instance.
Parameters
editor : editor
The editor instance this command will be related to.
commandDefinition : commandDefinition
The command definition.
Returns
command
-
capture()
CKEDITOR.command#capture
Register 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 ] : Boolean
Skip cache for example due to active filter change. Since CKEditor 4.2.0.
Returns
Boolean
Whether this command is allowed.
-
define( name, meta )
CKEDITOR.command#define
Predefine some intrinsic properties on a specific event name.
Parameters
name : String
The event name
meta : Object
-
disable()
CKEDITOR.command#disable
Disables 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#enable
Enables 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 : editor
The editor within which to run the command.
[ data ] : Object
Additional data to be used to execute the command.
Returns
Boolean
Whether the command has been successfully executed. Defaults to
true
if nothing is returned.
-
fire( eventName, [ data ], [ editor ] ) → Boolean | Object
CKEDITOR.command#fire
Fires 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 : String
The event name to fire.
[ data ] : Object
Data to be sent as the CKEDITOR.eventInfo.data when calling the listeners.
[ editor ] : editor
The editor instance to send as the CKEDITOR.eventInfo.editor when calling the listener.
Returns
Boolean | Object
A boolean indicating that the event is to be canceled, or data returned by one of the listeners.
-
fireOnce( eventName, [ data ], [ editor ] ) → Boolean | Object
CKEDITOR.command#fireOnce
Fires 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 : String
The event name to fire.
[ data ] : Object
Data to be sent as the CKEDITOR.eventInfo.data when calling the listeners.
[ editor ] : editor
The editor instance to send as the CKEDITOR.eventInfo.editor when calling the listener.
Returns
Boolean | Object
A booloan indicating that the event is to be canceled, or data returned by one of the listeners.
-
hasListeners( eventName ) → Boolean
CKEDITOR.command#hasListeners
Checks 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' ) ); // false
Parameters
eventName : String
The event name.
Returns
Boolean
-
on( eventName, listenerFunction, [ scopeObj ], [ listenerData ], [ priority ] ) → Object
CKEDITOR.command#on
Registers 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 called
Note: 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 : String
The event name to which listen.
listenerFunction : Function
The function listening to the event. A single CKEDITOR.eventInfo object instanced is passed to this function containing all the event data.
[ scopeObj ] : Object
The object used to scope the listener call (the
this
object). If omitted, the current object is used.[ listenerData ] : Object
Data to be sent as the CKEDITOR.eventInfo.listenerData when calling the listener.
[ priority ] : Number
The listener priority. Lower priority listeners are called first. Listeners with the same priority value are called in registration order.
Defaults to
10
Returns
Object
An object containing the
removeListener
function, which can be used to remove the listener at any time.
-
once()
CKEDITOR.command#once
Similiar 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
states
orselection
changed.Note: The function provided must be calling CKEDITOR.command.setState in all circumstances if it is intended to update the command state.
Parameters
editor : editor
path : elementPath
-
removeAllListeners()
CKEDITOR.command#removeAllListeners
Remove all existing listeners on this object, for cleanup purpose.
-
removeListener( eventName, listenerFunction )
CKEDITOR.command#removeListener
Unregisters 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 : String
The event name.
listenerFunction : Function
The listener function to unregister.
-
setState( newState ) → Boolean
CKEDITOR.command#setState
Sets 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 : Number
The new state. See state.
Returns
Boolean
Returns
true
if the command state changed.
-
toggleState()
CKEDITOR.command#toggleState
Toggles 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 : Object
The object into which implement the features.
Events
-
state( evt )
CKEDITOR.command#state
Fired when the command state changes.
command.on( 'state', function() { // Alerts the new state. alert( this.state ); } );
Parameters
evt : eventInfo