Get an added block

This example shows how to get an added block and manipulate it. Add text via plus button on the toolbar.

    Code

    <!DOCTYPE html>
    <html>
    <head>
        <title>Redactor X</title>
        <meta charset="utf-8">
    
        <!-- css -->
        <link rel="stylesheet" href="/your-dist-path/redactorx.min.css" />
    </head>
    <body>
        <!-- element -->
        <textarea id="entry">
            <p>Content</p>
        </textarea>
    
        <!-- js -->
        <script src="/your-dist-path/redactorx.min.js"></script>
    
        <!-- call -->
        <script>
        RedactorX('#entry', {
            subscribe: {
                'block.add': function(event) {
                    var instance = event.get('instance');
                    if (instance.isType('paragraph')) {
                        var $block = instance.getBlock();
    
                        // example of manipulation of a block
                        $block.html('My text');
    
                        // set caret to the end
                        this.app.caret.set($block, 'end')
                    }
                }
            }
        });
        </script>
    </body>
    </html>

    Usage

    The example shows how to get a block by setting event observation when the editor starts. Another way is to make a plugin and catch the event in it.

    // plugin code
    RedactorX.add('plugin', 'observe-add', {
        subscribe: {
            'block.add': function(event) {
                this.observe(event);
            }
        },
        observe: function(event) {
            var instance = event.get('instance');
            if (instance.isType('paragraph')) {
                var $block = instance.getBlock();
    
                // example of manipulation of a block
                $block.html('My text');
    
                // set caret to the end
                this.app.caret.set($block, 'end')
            }
        }
    });
    
    // run with plugin
    RedactorX('#entry', {
        plugins: ['observe-add']
    });