Documentation

How to create a custom block

Examples #

The easiest way to learn how to create your blocks is by examples. Here are some plugins with custom blocks created by us and examples that you can download and study:

Creating block #

Let's say that our content should have a block of tags, with the ability to create and edit them. For example, this html should be parsed in Article as a tags block:

<div class="tags">
    <span class="tag">Tag 1</span>
    <span class="tag">Tag 2</span>
</div>

To do this, you need to create a block code, for example, like this:

ArticleEditor.add('block', 'block.tags', {
    mixins: ['block'],
    type: 'tags',
    parser: {
        tag: 'div',
        parse: function($el) {
            return ($el.hasClass('tags')) ? 'tags' : false;
        }
    },
    toolbar: {
        add: {
            command: 'addbar.popup',
            title: ''
        },
        tags: {
            command: 'tags.edit',
            title: '',
            icon: '<svg ...>'
        }
    },
    control: {
        trash: {
            command: 'block.remove',
            title: ''
        },
        duplicate: {
            command: 'block.duplicate',
            title: ''
        }
    },
    create: function() {
        return this.dom('<div>').addClass('tags');
    }
});

Let's see what it's made of.

mixins

The mixins parameter indicates that the block will inherit methods from Block.instance. This will allow you to work with a custom block inside the editor the same way as with an already built in block.

type

Indicates the name or type of block.

parser

This parameter tells the editor which tag the block has and which class or attribute the block should have when parsing content.

toolbar

This parameter sets the buttons that will be displayed in toolbar when you click on the block. You can turn off the toolbar by specifying false.

ArticleEditor.add('block', 'block.myblock', {
    mixins: ['block'],
    type: 'myblock',
    toolbar: false

    ...

});

control

This parameter sets the buttons that will be displayed in control when you click on the block. You can turn off the control by specifying false.

ArticleEditor.add('block', 'block.myblock', {
    mixins: ['block'],
    type: 'myblock',
    control: false

    ...

});

create

The method of block creation. This method can be called if a block is created via addbar or using this.app.create:

var myblock = this.app.create('block.myblock');

nested

A true value will indicate that there may be other blocks inside the block.

ArticleEditor.add('block', 'block.myblock', {
    mixins: ['block'],
    type: 'myblock',
    nested: true

    ...

});

editable

A true value will indicate that the block content is editable.

ArticleEditor.add('block', 'block.myblock', {
    mixins: ['block'],
    type: 'myblock',
    editable: true

    ...

});

Summary #

Example Tags Plugin shows a fully created custom block, how to add its icon to the toolbar, addbar, and how to edit content of the block.