Documentation

How to create a plugin

Quick start #

Create a JS file, write the plugin code in it and connect it after the JS file of the editor.

Here is a generic example of a plugin template:

(function() {
    ArticleEditor.add('plugin', 'myplugin', {
        // call when the editor initialize all modules & plugins (optional method)
        init: function() {
             // define local variables
             this.myvariable = true;

             // define services
             this.insertion = this.app.create('insertion');
        },

        // call when the editor starts (optional method)
        start: function() {
            if (this.myvariable) {
                this.myMethod();
                this._myPrivateMethod();
            }
        },

        // call when the editor stops (optional method)
        stop: function() {
            this.myvariable = false;
        },

        // public methods
        myMethod: function() {
            // do something
        },

        // private
        _myPrivateMethod: function() {
            // do something
        }
    });
})(ArticleEditor);

And now we run the plugin at the start of the Article.

ArticleEditor('#entry', {
    plugins: ['myplugin']
});

Methods #

init

The plugin may have an optional init method. This method is started by the editor when initializing the classes of all modules and plugins. I.e. before the editor starts.

In this method, we usually declare the plugin's local variables and define the necessary services for the plugin work.

start

The plugin may have an optional start method. This method starts after the initialization of all modules and plugins.

In this method, we usually build all the initial business logic of the plugin.

stop

The plugin may have an optional stop method. This method is called when the editor is stopped.

Usually in this method we build stop plugin actions, for example, stopping Dom events, deleting elements created by the plugin, etc.

public

Public methods are called outside the plugin, for example, they handle the commands of the plugin buttons or are called by the API by other plugins.

private

Private methods are called inside the plugin and are the main tool for building the plugin.

API #

Inside any plugin, the object this.app is available with which you can access any module of the editor.

Here is an example of accessing the Toolbar module to create a button:

(function() {
    ArticleEditor.add('plugin', 'myplugin', {
        start: function() {
            this.app.toolbar.add('mybutton', {
                title: 'My Button',
                icon: '<i class="far fa-file-alt"></i>',
                command: 'myplugin.toggle'
            });
        },
        toggle: function(args) {
            // do something
        }
    });
})(ArticleEditor);

To access the services API (they are indicated in the API description in a separate way), you must first define a service.

(function() {
    ArticleEditor.add('plugin', 'myplugin', {
        init: function() {
            // define service
            this.insertion = this.app.create('insertion');
        },
        mymethod: function() {
            // insert html char at caret
            var inserted = this.insertion.insertChar('&nbsp;');
        }
    });
})(ArticleEditor);

Access to another plugin's method is similar to accessing module methods using this.app.

(function() {
    ArticleEditor.add('plugin', 'myplugin', {
        start: function() {
            this.app.mysecondplugin.toggle();
        }
    });
})(ArticleEditor);

See more how the API works.

Event #

Plugins can subscribe to editor events with the subscribe object.

(function() {
    ArticleEditor.add('plugin', 'myplugin', {
        subscribe: {
            'block.remove': function() {
                this.catchBlockRemove();
            }
        },
        start: function() {
            // myplugin start actions
        },
        catchBlockRemove: function() {
            // do something
        }
    });
})(ArticleEditor);

In this example, the catchBlockRemove plugin method will be called each time a block is deleted in the editor.

Any plugin can create its own events using the this.app.broadcast method. These events as well as the editor events can be caught in another plugin.

Here is an example of creating an event in a plugin when it starts:

(function() {
    ArticleEditor.add('plugin', 'myplugin', {
        start: function() {
            this.app.broadcast('myplugin.start');
        }
    });
})(ArticleEditor);

And this is how you can caught this event in another plugin:

(function() {
    ArticleEditor.add('plugin', 'mysecondplugin', {
        subscribe: {
            'myplugin.start': function() {
                this.catchMypluginStart();
            }
        },
        catchMypluginStart: function() {
            // do something
        }
    });
})(ArticleEditor);

See more how events work.

Settings #

Plugin settings can be specified in the defaults object. In this case, the editor will automatically include them into a single object of all settings. This means that these settings can be changed at the start of the editor, and access them using this.opts inside the plugin.

Here is an example of specifying the defaults object.

(function() {
    ArticleEditor.add('plugin', 'myplugin', {
        defaults: {
            myvariable: true
        },
        start: function() {
            if (this.opts.myvariable) {
                this.myMethod();
            }
        },
        myMethod: function() {
            // do something
        }
    });
})(ArticleEditor);

And this is how you can change the settings of the plugin at the start of the editor:

ArticleEditor('#entry', {
    plugins: ['myplugin']
    myvariable: false
});

Translations #

The easiest way to translate a plugin is to add language variables directly to its code, using the translations object.

For example:

(function() {
    ArticleEditor.add('plugin', 'myplugin', {
        translations: {
            en: {
                "myplugin": {
                    "get-code": "Get Code",
                    "set-code": "Set Code"
                }
            },
            de: {
                "myplugin": {
                    "get-code": "Code abrufen",
                    "set-code": "Code einstellen"
                }
            }
        },
        start: function() {
            // get the language variable
            var value = this.lang.get('myplugin.get-code');

            // parse a string with language variable
            var str = this.lang.parse('My string with  variable.');
        }
    });
})(ArticleEditor);

In the same example, in the start method, you can see how to access the language variables in the plugin and how to convert the variable if it is in a text string.

You can see how to specify language variables in the plugin, using the example of the Inline Format plugin.

Shortcuts #

You can assign a keyboard shortcut to the plugin method. To do this, use the API method add of the Shortcuts module.

(function() {
    ArticleEditor.add('plugin', 'myplugin', {
        start: function() {
            this.app.shortcuts.add('ctrl+j, meta+j', {
                command: 'myplugin.toggle',
                params: { arg: val }
            });
        },
        toggle: function(event) {
            console.log('Shortcut action with a param ' + event.get('arg'));
        }
    });
})(ArticleEditor);