Report an issue
Class

DecoupledEditor (editor-decoupled)

@ckeditor/ckeditor5-editor-decoupled/src/decouplededitor

class

The decoupled editor implementation. It provides an inline editable and a toolbar. However, unlike other editors, it does not render these components anywhere in the DOM unless configured.

This type of an editor is dedicated to integrations which require a customized UI with an open structure, allowing developers to specify the exact location of the interface.

See the document editor demo to learn about possible use cases for the decoupled editor.

In order to create a decoupled editor instance, use the static DecoupledEditor.create() method.

Decoupled editor and document editor build

The decoupled editor can be used directly from source (if you installed the @ckeditor/ckeditor5-editor-decoupled package) but it is also available in the document editor build.

Builds are ready-to-use editors with plugins bundled in. When using the editor from source you need to take care of loading all plugins by yourself (through the config.plugins option). Using the editor from source gives much better flexibility and allows for easier customization.

Read more about initializing the editor from source or as a build in DecoupledEditor.create().

Filtering

Properties

  • commands : CommandCollection

    readonly inherited

    Commands registered to the editor.

    Use the shorthand editor.execute() method to execute commands:

    // Execute the bold command:
    editor.execute( 'bold' );
    
    // Check the state of the bold command:
    editor.commands.get( 'bold' ).value;
  • config : Config

    readonly inherited

    Holds all configurations specific to this editor instance.

    editor.config.get( 'image.toolbar' );
    // -> [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
  • conversion : Conversion

    readonly inherited

    Conversion manager through which you can register model to view and view to model converters.

    See Conversion's documentation to learn how to add converters.

  • data : DataController

    readonly inherited

    The data controller. Used e.g. for setting and retrieving editor data.

  • editing : EditingController

    readonly inherited

    The editing controller. Controls user input and rendering the content for editing.

  • element : HTMLElement

    readonly

    The element used as an editable. The data will be loaded from it and loaded back to it once the editor is destroyed.

    Note: The property is available only when such element has been passed to the constructor.

  • isReadOnly : Boolean

    inherited observable

    Defines whether this editor is in read-only mode.

    In read-only mode the editor commands are disabled so it is not possible to modify document by using them.

  • keystrokes : EditingKeystrokeHandler

    readonly inherited

    Instance of the EditingKeystrokeHandler.

    It allows setting simple keystrokes:

    // Execute the bold command on Ctrl+E:
    editor.keystrokes.set( 'Ctrl+E', 'bold' );
    
    // Execute your own callback:
    editor.keystrokes.set( 'Ctrl+E', ( data, cancel ) => {
        console.log( data.keyCode );
    
        // Prevent default (native) action and stop the underlying keydown event
        // so no other editor feature will interfere.
        cancel();
    } );
  • locale : Locale

    readonly inherited

  • model : Model

    readonly inherited

    The editor's model.

    The central point of the editor's abstract data model.

  • plugins : PluginCollection

    readonly inherited

    The plugins loaded and in use by this editor instance.

    editor.plugins.get( 'Clipboard' ); // -> instance of the Clipboard plugin.
  • ui : EditorUI

    readonly

    Editor UI instance.

Static properties

  • build : Object

    inherited static

    Additional data built into the editor class. It's used while bundling the editor in order to provide the default set of plugins and config options which are later used during editor initialization.

    Two properties are supported:

    • plugins – an array of plugin constructors. They will be automatically initialized by the editor, unless listed in config.removePlugins or unless config.plugins is passed.
    • config – the defalt config options.

Methods

  • constructor( elementOrData, config )

    protected

    Creates an instance of the decoupled editor.

    Note: Do not use the constructor to create editor instances. Use the static DecoupledEditor.create() method instead.

    Parameters

    elementOrData : HTMLElement | String

    The DOM element that serves as an editable. The data will be loaded from it and loaded back to it once the editor is destroyed. Alternatively, a data string to be loaded into the editor.

    config : EditorConfig

    The editor configuration.

    Overrides: Editor#constructor
  • bind( bindProperties ) → Object

    mixed

    Binds observable properties to another objects implementing Observable interface (like Model).

    Once bound, the observable will immediately share the current state of properties of the observable it is bound to and react to the changes to these properties in the future.

    Note: To release the binding use unbind.

    Using bind().to() chain:

    A.bind( 'a' ).to( B );
    A.bind( 'a' ).to( B, 'b' );
    A.bind( 'a', 'b' ).to( B, 'c', 'd' );
    A.bind( 'a' ).to( B, 'b', C, 'd', ( b, d ) => b + d );

    It is also possible to bind to the same property in a observables collection using bind().toMany() chain:

    A.bind( 'a' ).toMany( [ B, C, D ], 'x', ( a, b, c ) => a + b + c );
    A.bind( 'a' ).toMany( [ B, C, D ], 'x', ( ...x ) => x.every( x => x ) );

    Parameters

    bindProperties : String

    Observable properties that will be bound to another observable(s).

    Returns

    Object

    The bind chain with the to() and toMany() methods.

  • decorate( methodName )

    mixed

    Turns the given methods of this object into event-based ones. This means that the new method will fire an event (named after the method) and the original action will be plugged as a listener to that event.

    This is a very simplified method decoration. Itself it doesn't change the behavior of a method (expect adding the event), but it allows to modify it later on by listening to the method's event.

    For example, in order to cancel the method execution one can stop the event:

    class Foo {
        constructor() {
            this.decorate( 'method' );
        }
    
        method() {
            console.log( 'called!' );
        }
    }
    
    const foo = new Foo();
    foo.on( 'method', ( evt ) => {
        evt.stop();
    }, { priority: 'high' } );
    
    foo.method(); // Nothing is logged.

    Note: we used a high priority listener here to execute this callback before the one which calls the original method (which used the default priority).

    It's also possible to change the return value:

    foo.on( 'method', ( evt ) => {
        evt.return = 'Foo!';
    } );
    
    foo.method(); // -> 'Foo'

    Finally, it's possible to access and modify the parameters:

    method( a, b ) {
        console.log( `${ a }, ${ b }`  );
    }
    
    // ...
    
    foo.on( 'method', ( evt, args ) => {
        args[ 0 ] = 3;
    
        console.log( args[ 1 ] ); // -> 2
    }, { priority: 'high' } );
    
    foo.method( 1, 2 ); // -> '3, 2'

    Parameters

    methodName : String

    Name of the method to decorate.

  • delegate( events ) → EmitterMixinDelegateChain

    mixed

    Delegates selected events to another Emitter. For instance:

    emitterA.delegate( 'eventX' ).to( emitterB );
    emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );

    then eventX is delegated (fired by) emitterB and emitterC along with data:

    emitterA.fire( 'eventX', data );

    and eventY is delegated (fired by) emitterC along with data:

    emitterA.fire( 'eventY', data );

    Parameters

    events : String

    Event names that will be delegated to another emitter.

    Returns

    EmitterMixinDelegateChain
  • destroy() → Promise

    Destroys the editor instance, releasing all resources used by it.

    Note: The decoupled editor does not remove the toolbar and editable when destroyed. You can do that yourself in the destruction chain:

    editor.destroy()
        .then( () => {
            // Remove the toolbar from DOM.
            editor.ui.view.toolbar.element.remove();
    
            // Remove the editable from DOM.
            editor.ui.view.editable.element.remove();
    
            console.log( 'Editor was destroyed' );
        } );

    Returns

    Promise
    Overrides: Editor#destroy
  • execute( commandName, [ ...commandParams ] )

    inherited

    Executes specified command with given parameters.

    Shorthand for:

    editor.commands.get( commandName ).execute( ... );

    Parameters

    commandName : String

    Name of command to execute.

    [ ...commandParams ] : *

    Command parameters.

  • fire( eventOrInfo, [ args ] ) → *

    mixed

    Fires an event, executing all callbacks registered for it.

    The first parameter passed to callbacks is an EventInfo object, followed by the optional args provided in the fire() method call.

    Parameters

    eventOrInfo : String | EventInfo

    The name of the event or EventInfo object if event is delegated.

    [ args ] : *

    Additional arguments to be passed to the callbacks.

    Returns

    *

    By default the method returns undefined. However, the return value can be changed by listeners through modification of the evt.return's property (the event info is the first param of every callback).

  • getData() → String

    mixed

    Gets the data from the editor.

    editor.getData(); // -> '<p>This is editor!</p>'

    By default the editor outputs HTML. This can be controlled by injecting a different data processor. See Markdown output guide for more details.

    Note: Not only is the format of the data configurable, but the type of the getData()'s return value does not have to be a string either. You can e.g. return an object or a DOM DocumentFragment if you consider this the right format for you.

    Returns

    String

    Output data.

  • initPlugins() → Promise

    inherited

    Loads and initializes plugins specified in the config.

    Returns

    Promise

    A promise which resolves once the initialization is completed.

  • listenTo( emitter, event, callback, [ options ] = { [options.priority] } )

    mixed

    Registers a callback function to be executed when an event is fired in a specific (emitter) object.

    Events can be grouped in namespaces using :. When namespaced event is fired, it additionally fires all callbacks for that namespace.

    // myEmitter.on( ... ) is a shorthand for myEmitter.listenTo( myEmitter, ... ).
    myEmitter.on( 'myGroup', genericCallback );
    myEmitter.on( 'myGroup:myEvent', specificCallback );
    
    // genericCallback is fired.
    myEmitter.fire( 'myGroup' );
    // both genericCallback and specificCallback are fired.
    myEmitter.fire( 'myGroup:myEvent' );
    // genericCallback is fired even though there are no callbacks for "foo".
    myEmitter.fire( 'myGroup:foo' );

    An event callback can stop the event and set the return value of the fire method.

    Parameters

    emitter : Emitter

    The object that fires the event.

    event : String

    The name of the event.

    callback : function

    The function to be called on event.

    [ options ] : Object

    Additional options.

    Properties
    [ options.priority ] : PriorityString | Number

    The priority of this event callback. The higher the priority value the sooner the callback will be fired. Events having the same priority are called in the order they were added.

    Defaults to 'normal'

    Defaults to {}

  • off( event, callback )

    mixed

    Stops executing the callback on the given event. Shorthand for this.stopListening( this, event, callback ).

    Parameters

    event : String

    The name of the event.

    callback : function

    The function to stop being called.

  • on( event, callback, [ options ] = { [options.priority] } )

    mixed

    Registers a callback function to be executed when an event is fired.

    Shorthand for this.listenTo( this, event, callback, options ) (it makes the emitter listen on itself).

    Parameters

    event : String

    The name of the event.

    callback : function

    The function to be called on event.

    [ options ] : Object

    Additional options.

    Properties
    [ options.priority ] : PriorityString | Number

    The priority of this event callback. The higher the priority value the sooner the callback will be fired. Events having the same priority are called in the order they were added.

    Defaults to 'normal'

    Defaults to {}

  • once( event, callback, [ options ] = { [options.priority] } )

    mixed

    Registers a callback function to be executed on the next time the event is fired only. This is similar to calling on followed by off in the callback.

    Parameters

    event : String

    The name of the event.

    callback : function

    The function to be called on event.

    [ options ] : Object

    Additional options.

    Properties
    [ options.priority ] : PriorityString | Number

    The priority of this event callback. The higher the priority value the sooner the callback will be fired. Events having the same priority are called in the order they were added.

    Defaults to 'normal'

    Defaults to {}

  • set( name, [ value ] )

    mixed

    Creates and sets the value of an observable property of this object. Such an property becomes a part of the state and is be observable.

    It accepts also a single object literal containing key/value pairs with properties to be set.

    This method throws the observable-set-cannot-override error if the observable instance already have a property with the given property name. This prevents from mistakenly overriding existing properties and methods, but means that foo.set( 'bar', 1 ) may be slightly slower than foo.bar = 1.

    Parameters

    name : String | Object

    The property's name or object with name=>value pairs.

    [ value ] : *

    The property's value (if name was passed in the first parameter).

  • setData( data )

    mixed

    Sets the data in the editor.

    editor.setData( '<p>This is editor!</p>' );

    By default the editor accepts HTML. This can be controlled by injecting a different data processor. See Markdown output guide for more details.

    Note: Not only is the format of the data configurable, but the type of the setData()'s parameter does not have to be a string either. You can e.g. accept an object or a DOM DocumentFragment if you consider this the right format for you.

    Parameters

    data : String

    Input data.

  • stopDelegating( [ event ], [ emitter ] )

    mixed

    Stops delegating events. It can be used at different levels:

    • To stop delegating all events.
    • To stop delegating a specific event to all emitters.
    • To stop delegating a specific event to a specific emitter.

    Parameters

    [ event ] : String

    The name of the event to stop delegating. If omitted, stops it all delegations.

    [ emitter ] : Emitter

    (requires event) The object to stop delegating a particular event to. If omitted, stops delegation of event to all emitters.

  • stopListening( [ emitter ], [ event ], [ callback ] )

    mixed

    Stops listening for events. It can be used at different levels:

    • To stop listening to a specific callback.
    • To stop listening to a specific event.
    • To stop listening to all events fired by a specific object.
    • To stop listening to all events fired by all objects.

    Parameters

    [ emitter ] : Emitter

    The object to stop listening to. If omitted, stops it for all objects.

    [ event ] : String

    (Requires the emitter) The name of the event to stop listening to. If omitted, stops it for all events from emitter.

    [ callback ] : function

    (Requires the event) The function to be removed from the call list for the given event.

  • t()

    inherited

    Shorthand for t.

  • unbind( [ unbindProperties ] )

    mixed

    Removes the binding created with bind.

    A.unbind( 'a' );
    A.unbind();

    Parameters

    [ unbindProperties ] : String

    Observable properties to be unbound. All the bindings will be released if no properties provided.

Static methods

  • create( elementOrData, config ) → Promise

    static

    Creates a decoupled editor instance.

    Creating an instance when using the CKEditor 5 build:

    DecoupledEditor
        .create( document.querySelector( '#editor' ) )
        .then( editor => {
            // Append the toolbar to the <body> element.
            document.body.appendChild( editor.ui.view.toolbar.element );
    
            console.log( 'Editor was initialized', editor );
        } )
        .catch( err => {
            console.error( err.stack );
        } );

    Creating an instance when using CKEditor from source (make sure to specify the list of plugins to load and the toolbar):

    import DecoupledEditor from '@ckeditor/ckeditor5-editor-decoupled/src/decouplededitor';
    import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
    import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
    import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
    import ...
    
    DecoupledEditor
        .create( document.querySelector( '#editor' ), {
            plugins: [ Essentials, Bold, Italic, ... ],
            toolbar: [ 'bold', 'italic', ... ]
        } )
        .then( editor => {
            // Append the toolbar to the <body> element.
            document.body.appendChild( editor.ui.view.toolbar.element );
    
            console.log( 'Editor was initialized', editor );
        } )
        .catch( err => {
            console.error( err.stack );
        } );

    Note: It is possible to create the editor out of a pure data string. The editor will then render an editable element that must be inserted into the DOM for the editor to work properly:

    DecoupledEditor
        .create( '<p>Editor data</p>' )
        .then( editor => {
            // Append the toolbar to the <body> element.
            document.body.appendChild( editor.ui.view.toolbar.element );
    
            // Append the editable to the <body> element.
            document.body.appendChild( editor.ui.view.editable.element );
    
            console.log( 'Editor was initialized', editor );
        } )
        .catch( err => {
            console.error( err.stack );
        } );

    Parameters

    elementOrData : HTMLElement | String

    The DOM element that serves as an editable. The data will be loaded from it and loaded back to it once the editor is destroyed. Alternatively, a data string to be loaded into the editor.

    config : EditorConfig

    The editor configuration.

    Returns

    Promise

    A promise resolved once the editor is ready. The promise returns the created DecoupledEditor instance.

Events

  • change:isReadOnly( eventInfo, name, value, oldValue )

    inherited

    Fired when the isReadOnly property changed value.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    name : String

    Name of the changed property (isReadOnly).

    value : Boolean

    New value of the isReadOnly property with given key or null, if operation should remove property.

    oldValue : Boolean

    Old value of the isReadOnly property with given key or null, if property was not set before.

  • change:{property}( eventInfo, name, value, oldValue )

    mixed

    Fired when a property changed value.

    observable.set( 'prop', 1 );
    
    observable.on( 'change:prop', ( evt, propertyName, newValue, oldValue ) => {
        console.log( `${ propertyName } has changed from ${ oldValue } to ${ newValue }` );
    } )
    
    observable.prop = 2; // -> 'prop has changed from 1 to 2'

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    name : String

    The property name.

    value : *

    The new property value.

    oldValue : *

    The previous property value.

  • dataReady( eventInfo )

    inherited

    Fired when the data loaded to the editor is ready. If a specific editor doesn't load any data initially, this event will be fired right before event-ready.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

  • destroy( eventInfo )

    inherited

    Fired when this editor instance is destroyed. The editor at this point is not usable and this event should be used to perform the clean-up in any plugin.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

  • pluginsReady( eventInfo )

    inherited

    Fired after plugins are initialized.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

  • ready( eventInfo )

    inherited

    Fired when plugins, and data and all additional editor components are ready.

    Note: This event is most useful for plugin developers. When integrating the editor with your website or application you do not have to listen to editor#ready because when the promise returned by the static Editor.create() event is resolved, the editor is already ready. In fact, since the first moment when the editor instance is available to you is inside then()'s callback, you cannot even add a listener to the editor#ready event.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

  • uiReady( eventInfo )

    Fired when the editor UI is ready.

    Fired after event-pluginsReady and before event-dataReady.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.