Report an issue
Class

CKEDITOR.command

class

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

    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

    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

    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

  • contextSensitive : Boolean

    Indicates that this command is sensible to the selection context. If true, the refresh method will be called for this command on the CKEDITOR.editor.selectionChange event.

    Defaults to false

  • editorFocus : Boolean

    Indicates that the editor will get the focus before executing the command.

    // Do not force the editor to have focus when executing the command.
    command.editorFocus = false;
    

    Defaults to true

  • fakeKeystroke : Number

    since 4.6.0

    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 and paste 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.

  • modes : Object

    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.

    // Enable the command in both WYSIWYG and Source modes.
    command.modes = { wysiwyg:1,source:1 };
    
    // Enable the command in Source mode only.
    command.modes = { source:1 };
    

    CKEDITOR.editor.mode

    Defaults to {wysiwyg: 1}

  • previousState : Number

    Indicates the previous command state.

    alert( command.previousState );state
    
  • readOnly : Boolean

    since 4.0

    Whether the command should be enabled in the read-only mode.

    Defaults to false

  • startDisabled : Boolean

    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

    Indicates the editor state. Possible values are:

    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

    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

    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()

    Register event handler under the capturing stage on supported target.

  • checkAllowed( [ noCache ] ) → Boolean

    since 4.1

    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.

    Returns

    Boolean

    Whether this command is allowed.

  • define( name, meta )

    Predefine some intrinsic properties on a specific event name.

    Parameters

    name : String

    The event name

    meta : Object
    Properties
    [ errorProof ]

    Whether the event firing should catch error thrown from a per listener call.

    Defaults to false

  • 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()

    Enables the command for execution. The command state (see state) available before disabling it is restored.

    command.enable();
    command.exec(); // Execute the command.
    
  • exec( [ data ] ) → Boolean

    Executes the command.

    command.exec(); // The command gets executed.
    

    Note: You should use the CKEDITOR.editor.execCommand method instead of calling command.exec() directly.

    Parameters

    [ data ] : Object

    Any data to pass to the command. Depends on the command implementation and requirements.

    Returns

    Boolean

    A boolean indicating that the command has been successfully executed.

  • fire( eventName, [ data ], [ editor ] ) → Boolean | Object

    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

    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

    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

    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
    

    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()

    Similiar with on but the listener will be called only once upon the next event firing.

  • refresh( editor, path )

    Explicitly update the status of the command, by firing the refresh event, as well as invoke the CKEDITOR.commandDefinition.refresh method if defined, this method is to allow different parts of the editor code to contribute in command status resolution.

    Parameters

    editor : editor

    The editor instance.

    path : elementPath
  • removeAllListeners()

    Remove all existing listeners on this object, for cleanup purpose.

  • removeListener( eventName, listenerFunction )

    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

    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()

    Toggles the on/off (active/inactive) state of the command. This is mainly used internally by context sensitive commands.

    command.toggleState();
    

Events

  • refresh( evt )

    Parameters

    evt : eventInfo
  • state( evt )

    Fired when the command state changes.

    command.on( 'state', function() {
        // Alerts the new state.
        alert( this.state );
    } );
    

    Parameters

    evt : eventInfo