jQuery Editor: Custom Tools

jqxEditor's createCommand function enables you to add custom commands to the toolbar.

A live JavaScript/jQuery example of jqxEditor, the jQWidgets Editor component, with its complete source code below. It is one of 14 jqxEditor demos for jQuery.

jqxEditor API used in this demo

Source code

To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $. Getting started with jqxEditor shows the setup step by step.

The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxbuttons.js, jqxscrollbar.js, jqxlistbox.js, jqxdropdownlist.js, jqxdropdownbutton.js, jqxcolorpicker.js, jqxwindow.js, jqxeditor.js, jqxtooltip.js, jqxcheckbox.js.

It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers).

script.js

$(document).ready(function () {
    $('#editor').jqxEditor({
        height: 400,
        width: getWidth('editor'),
        tools: 'datetime | print | clear | backcolor | font | bold italic underline',
        createCommand: function (name) {
            switch (name) {
                case "datetime":
                    return {
                        type: 'list',
                        tooltip: "Insert Date/Time",
                        init: function (widget) {
                            widget.jqxDropDownList({ placeHolder: "Insert Custom HTML", width: 170, source: ['Insert Time', 'Insert Date'], autoDropDownHeight: true });
                        },
                        refresh: function (widget, style) {
                            // do something here when the selection is changed.
                            widget.jqxDropDownList('clearSelection');
                        },
                        action: function (widget, editor) {
                            var widgetValue = widget.val();
                            var date = new Date();
                            // return object with command and value members.
                            return { command: "inserthtml", value: widgetValue == "Insert Time" ? date.getHours() + ":" + date.getMinutes() : date.getDate() + "-" + date.getMonth() + "-" + date.getFullYear() };
                        }
                    }
                case "print":
                    return {
                        type: 'button',
                        tooltip: 'Print',
                        init: function (widget) {
                            widget.jqxButton({ height: 25, width: 40 });
                            widget.html("<span style='line-height: 24px;'>Print</span>");
                        },
                        refresh: function (widget, style) {
                            // do something here when the selection is changed.
                        },
                        action: function (widget, editor) {
                            // return nothing and perform a custom action.
                            $('#editor').jqxEditor('print');
                        }
                    }
                case "clear":
                    return {
                        type: 'button',
                        tooltip: 'Clear',
                        init: function (widget) {
                            widget.jqxButton({ height: 25, width: 40 });
                            widget.html("<span style='line-height: 24px;'>Clear</span>");
                        },
                        refresh: function (widget, style) {
                            // do something here when the selection is changed.
                        },
                        action: function (widget, editor) {
                            // return nothing and perform a custom action.
                            $('#editor').val('');
                        }
                    }
                case "backcolor":
                    return {
                        type: 'colorPicker',
                        tooltip: 'Background',
                        init: function (widget) {
                            widget.jqxDropDownButton({ width: 170 });
                            widget.jqxDropDownButton('setContent', '<span style="line-height: 24px;">Choose Background</span>');
                        },
                        refresh: function (widget, style) {
                            // do something here when the selection is changed.
                        },
                        action: function (widget, editor) {
                            // return nothing and perform a custom action.
                            var color = widget.val();
                            editor.css('background', color);
                        }
                    }
            }
        }
    });
});

body.html

<textarea id="editor">   
</textarea>

Learn more

More jQuery Editor demos