Create plugin with popup form

This example shows how to create a plugin with popup form. Click on the content to see the plugin button on the toolbar. See more about how to create plugins.

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>


    <!-- plugin code with fontawesome icon -->
    <script>
    RedactorX.add('plugin', 'myplugin', {
        start: function() {
            this.app.toolbar.add('mybutton', {
                title: 'My Button',
                icon: '<i class="fa fa-superpowers"></i>',
                command: 'myplugin.popup'
            });
        },
        popup: function(params, button) {
            // create
            var popup = this.app.popup.create('myplugin', {
                title: 'My plugin',
                width: '400px',
                form: {
                    id: { type: 'input', label: 'ID' }
                },
                footer: {
                    save: { title: 'Save', command: 'myplugin.save', type: 'primary' },
                    cancel: { title: 'Cancel', command: 'popup.close' }

                }
            });

            // data
            var instance = this.app.block.get();
            var $block = instance.getBlock();
            var id = $block.attr('id');

            // set form
            popup.setData({
                id: id
            });

            // open
            this.app.popup.open({ button: button });
        },
        save: function(popup) {
            this.app.popup.close();

            var data = popup.getData();
            var instance = this.app.block.get();
            var $block = instance.getBlock();

            if (data.id !== '') {
                $block.attr('id', data.id);
            }
            else {
                $block.removeAttr('id');
            }
        }
    });
    </script>


    <!-- call -->
    <script>
    RedactorX('#entry', {
        plugins: ['myplugin']
    });
    </script>
</body>
</html>