Skip to main content

UI Components

The UI Registry API lets you register custom toolbar buttons, menu items, context menus, autocompleters, sidebars, and other UI components. All methods are available on editor.ui.registry inside the setup callback.

editor42.init({
selector: 'textarea',
setup: (editor) => {
editor.ui.registry.addButton('mybutton', {
text: 'My Button',
onAction: () => editor.insertContent('Hello!')
});
},
toolbar: 'undo redo | mybutton'
});

Toolbar Buttons

Basic Button

Registers a toolbar button that executes a command when clicked.

editor.ui.registry.addButton(name, spec)
PropertyTypeDescription
textstring?Button label text.
iconstring?Icon name (registered or built-in).
tooltipstring?Tooltip shown on hover.
enabledboolean?Initial enabled state. Defaults to true.
onSetupfunction?Called when the button is rendered. Return a teardown function.
onActionfunctionCalled when the button is clicked.
editor.ui.registry.addButton('insertdate', {
icon: 'insert-time',
tooltip: 'Insert date',
onAction: () => {
editor.insertContent(new Date().toDateString());
}
});

Toggle Button

Registers a toggle button whose state can be switched on/off. Useful for format buttons like bold or italic that reflect the current selection state.

editor.ui.registry.addToggleButton(name, spec)
PropertyTypeDescription
textstring?Button label text.
iconstring?Icon name.
tooltipstring?Tooltip shown on hover.
enabledboolean?Initial enabled state.
activeboolean?Initial toggle state. Defaults to false.
onSetupfunction?Called when the button is rendered. Use api.setActive(state) to update the toggle.
onActionfunctionCalled when the button is clicked. Receives api with isActive() and setActive().
editor.ui.registry.addToggleButton('customformat', {
icon: 'highlight-bg-color',
tooltip: 'Toggle highlight',
onAction: (api) => {
editor.execCommand('editor42ToggleFormat', false, 'hilitecolor');
},
onSetup: (api) => {
const changed = editor.formatter.formatChanged('hilitecolor', (state) => {
api.setActive(state);
});
return changed;
}
});

Registers a toolbar button that opens a dropdown menu when clicked.

editor.ui.registry.addMenuButton(name, spec)
PropertyTypeDescription
textstring?Button label text.
iconstring?Icon name.
tooltipstring?Tooltip shown on hover.
fetchfunction(callback) => void — call callback with an array of menu items.
onSetupfunction?Called when the button is rendered.
editor.ui.registry.addMenuButton('myinsert', {
text: 'Insert',
fetch: (callback) => {
const items = [
{ type: 'menuitem', text: 'Horizontal rule', onAction: () => editor.insertContent('<hr>') },
{ type: 'menuitem', text: 'Page break', onAction: () => editor.insertContent('<!-- pagebreak -->') }
];
callback(items);
}
});

Split Button

Registers a split button — the main area executes a default action, and the arrow opens a dropdown menu. Used by the advlist plugin for list styles.

editor.ui.registry.addSplitButton(name, spec)
PropertyTypeDescription
textstring?Button label text.
iconstring?Icon name.
tooltipstring?Tooltip shown on hover.
onActionfunctionCalled when the main button area is clicked.
onItemActionfunctionCalled when a dropdown item is selected. Receives the item value.
fetchfunction(callback) => void — call callback with an array of choice items.
onSetupfunction?Called when the button is rendered.
editor.ui.registry.addSplitButton('mycolors', {
text: 'Color',
onAction: () => {
editor.execCommand('ForeColor', false, '#000000');
},
onItemAction: (_api, value) => {
editor.execCommand('ForeColor', false, value);
},
fetch: (callback) => {
callback([
{ type: 'choiceitem', text: 'Red', value: '#FF0000' },
{ type: 'choiceitem', text: 'Green', value: '#00FF00' },
{ type: 'choiceitem', text: 'Blue', value: '#0000FF' }
]);
}
});

Group Toolbar Button

Registers a button that opens a floating toolbar containing other buttons when clicked. Only available when toolbar_mode is set to 'floating'.

editor.ui.registry.addGroupToolbarButton(name, spec)
PropertyTypeDescription
textstring?Button label text.
iconstring?Icon name.
tooltipstring?Tooltip shown on hover.
itemsstringSpace-separated list of toolbar button names.
onSetupfunction?Called when the button is rendered.
editor.ui.registry.addGroupToolbarButton('formatting', {
icon: 'more-drawer',
tooltip: 'Formatting',
items: 'bold italic underline | forecolor backcolor'
});

Basic Menu Item

Registers a menu item that executes a command when clicked.

editor.ui.registry.addMenuItem(name, spec)
PropertyTypeDescription
textstring?Menu item label.
iconstring?Icon name.
shortcutstring?Keyboard shortcut hint displayed beside the label.
enabledboolean?Initial enabled state.
onSetupfunction?Called when the item is rendered.
onActionfunctionCalled when the item is clicked.
editor.ui.registry.addMenuItem('inserthr', {
text: 'Horizontal rule',
icon: 'horizontal-rule',
onAction: () => editor.insertContent('<hr>')
});

Nested Menu Item

Registers a menu item that reveals a submenu on hover or click.

editor.ui.registry.addNestedMenuItem(name, spec)
PropertyTypeDescription
textstring?Menu item label.
iconstring?Icon name.
getSubmenuItemsfunctionReturns a string of registered item names or an array of MenuItemSpec objects.
onSetupfunction?Called when the item is rendered.
editor.ui.registry.addNestedMenuItem('mysubmenu', {
text: 'Insert template',
getSubmenuItems: () => [
{ type: 'menuitem', text: 'Header', onAction: () => editor.insertContent('<h2>Header</h2>') },
{ type: 'menuitem', text: 'Footer', onAction: () => editor.insertContent('<footer>Footer</footer>') }
]
});

Toggle Menu Item

Registers a menu item with a checkmark that toggles state on click. Useful for boolean settings like "Paste as text".

editor.ui.registry.addToggleMenuItem(name, spec)
PropertyTypeDescription
textstring?Menu item label.
iconstring?Icon name.
activeboolean?Initial toggle state.
onSetupfunction?Called when the item is rendered. Use api.setActive(state) to update the checkmark.
onActionfunctionCalled when the item is clicked.
editor.ui.registry.addToggleMenuItem('darkmode', {
text: 'Dark mode',
onAction: (api) => {
const newState = !api.isActive();
api.setActive(newState);
editor.getBody().classList.toggle('dark', newState);
}
});

Context UI

Context Menu

Registers a context menu section that appears on right-click when a content condition is met. Add the registered name to the contextmenu option.

editor.ui.registry.addContextMenu(name, spec)
PropertyTypeDescription
updatefunction`(element) => string
editor.ui.registry.addContextMenu('mycontext', {
update: (element) => {
return element.nodeName === 'IMG' ? 'link image' : '';
}
});
editor42.init({
selector: 'textarea',
contextmenu: 'mycontext'
});

Context Toolbar

Registers a floating toolbar that appears when a content predicate matches. For example, the quickbars plugin uses this to show a toolbar when the cursor is on an image.

editor.ui.registry.addContextToolbar(name, spec)
PropertyTypeDescription
predicatefunction(node) => boolean — return true to show the toolbar.
itemsstringSpace-separated list of toolbar button names.
positionstring?Positioning: 'selection', 'node', or 'line'.
scopestring?'node' (default) or 'editor'.
editor.ui.registry.addContextToolbar('imagetools', {
predicate: (node) => node.nodeName === 'IMG',
items: 'alignleft aligncenter alignright',
position: 'node'
});

Context Form

Registers a contextual form with an input field that appears when a content predicate matches. Used by the link plugin for inline URL editing when link_context_toolbar: true.

editor.ui.registry.addContextForm(name, spec)
PropertyTypeDescription
predicatefunction(node) => boolean — return true to show the form.
initValuefunctionReturns the initial value for the input field.
commandsContextFormButtonSpec[]Action buttons displayed beside the input.
positionstring?Positioning: 'selection', 'node', or 'line'.
scopestring?'node' (default) or 'editor'.
editor.ui.registry.addContextForm('quicklink', {
predicate: (node) => node.nodeName === 'A',
initValue: () => {
const node = editor.selection.getNode();
return node.nodeName === 'A' ? node.href : '';
},
commands: [
{
type: 'contextformbutton',
icon: 'checkmark',
tooltip: 'Apply',
primary: true,
onAction: (formApi) => {
const value = formApi.getValue();
editor.execCommand('editor42InsertLink', false, { href: value });
formApi.hide();
}
},
{
type: 'contextformbutton',
icon: 'remove',
tooltip: 'Remove',
onAction: (formApi) => {
editor.execCommand('Unlink');
formApi.hide();
}
}
]
});

Autocompleters

Registers an autocompleter triggered by a character pattern. When the user types the trigger string followed by text, a dropdown appears with matching options. Used by the emoticons plugin (triggered by :) and the charmap plugin.

editor.ui.registry.addAutocompleter(name, spec)
PropertyTypeDescription
triggerstringCharacter(s) that activate the autocompleter (e.g., ':', '/').
minCharsnumber?Minimum characters after the trigger before fetching. Defaults to 1.
fetchfunction(pattern, maxResults, fetchOptions) => Promise — returns a promise resolving to an array of autocomplete items.
onActionfunction(autocompleterApi, range, value) => void — called when an item is selected.
columnsnumber/string?Number of columns for grid layout. Use 'auto' or 1 for a list.
editor.ui.registry.addAutocompleter('mentions', {
trigger: '@',
minChars: 1,
fetch: (pattern) => {
const users = [
{ text: 'Alice', value: 'alice' },
{ text: 'Bob', value: 'bob' },
{ text: 'Carol', value: 'carol' }
];
const filtered = users.filter((u) => u.text.toLowerCase().includes(pattern.toLowerCase()));
return Promise.resolve(
filtered.map((u) => ({
type: 'autocompleteitem',
value: u.value,
text: u.text
}))
);
},
onAction: (api, rng, value) => {
editor.selection.setRng(rng);
editor.insertContent(`<span class="mention">@${value}</span>&nbsp;`);
api.hide();
}
});

Icons

Registers a custom SVG icon that can be referenced by name in any UI component.

editor.ui.registry.addIcon(name, svgData)
PropertyTypeDescription
namestringUnique icon name to reference in buttons and menu items.
svgDatastringSVG markup string.
editor.ui.registry.addIcon('custom-star', '<svg width="24" height="24"><path d="M12 2l3.09 6.26L22 9.27l-5 4.87L18.18 22 12 18.27 5.82 22 7 14.14 2 9.27l6.91-1.01L12 2z"/></svg>');

editor.ui.registry.addButton('star', {
icon: 'custom-star',
tooltip: 'Insert star',
onAction: () => editor.insertContent('&#9733;')
});

Registers a sidebar panel attached to the right side of the editor. A toggle toolbar button is created automatically. The sidebar can also be controlled via the ToggleSidebar command.

editor.ui.registry.addSidebar(name, spec)
PropertyTypeDescription
tooltipstring?Tooltip for the auto-generated toolbar toggle button.
iconstring?Icon for the toggle button.
onSetupfunction?Called when the sidebar is first rendered. Receives api.
onShowfunction?Called each time the sidebar becomes visible. Receives api with element().
onHidefunction?Called each time the sidebar is hidden.
editor.ui.registry.addSidebar('comments', {
tooltip: 'Comments',
icon: 'comment',
onShow: (api) => {
const el = api.element();
el.innerHTML = '<div style="padding: 10px">No comments yet.</div>';
},
onHide: () => {}
});

Toggle the sidebar programmatically:

editor.execCommand('ToggleSidebar', false, 'comments');

View

Registers a view that replaces the editor content area when toggled on. Useful for alternate editing modes like source code views. Controlled via the ToggleView command.

editor.ui.registry.addView(name, spec)
PropertyTypeDescription
buttonsViewButtonSpec[]Action buttons shown in the view header.
onShowfunction(api) => void — called when the view becomes visible. Use api.getContainer() to access the view DOM.
onHidefunction(api) => void — called when the view is hidden.
editor.ui.registry.addView('codeview', {
buttons: [
{
type: 'button',
text: 'Back to editor',
buttonType: 'primary',
onAction: () => editor.execCommand('ToggleView', false, 'codeview')
}
],
onShow: (api) => {
const container = api.getContainer();
const textarea = document.createElement('textarea');
textarea.style.width = '100%';
textarea.style.height = '100%';
textarea.value = editor.getContent();
container.appendChild(textarea);
},
onHide: (api) => {
const textarea = api.getContainer().querySelector('textarea');
if (textarea) {
editor.setContent(textarea.value);
}
}
});

Toggle the view programmatically:

editor.execCommand('ToggleView', false, 'codeview');

Built-in Toolbar Buttons

These buttons are registered by default and can be used in the toolbar option without additional setup.

Format toggle buttons

ButtonDescription
boldToggles bold formatting.
italicToggles italic formatting.
underlineToggles underline formatting.
strikethroughToggles strikethrough formatting.
subscriptToggles subscript formatting.
superscriptToggles superscript formatting.
h1Toggles heading level 1.
h2Toggles heading level 2.
h3Toggles heading level 3.
h4Toggles heading level 4.
h5Toggles heading level 5.
h6Toggles heading level 6.
blockquoteToggles blockquote formatting.

Command buttons

ButtonDescription
copyCopies the selection to the clipboard.
cutCuts the selection to the clipboard.
pastePastes from the clipboard.
selectallSelects all content.
newdocumentClears the editor content.
printOpens the browser print dialog.
removeformatRemoves formatting from the selection.
removeDeletes the selected content.
hrInserts a horizontal rule.
helpOpens the help dialog.

Other core buttons

ButtonDescription
undoUndoes the last action.
redoRedoes the last undone action.
outdentDecreases indentation.
indentIncreases indentation.
alignleftAligns content to the left.
aligncenterCenters content.
alignrightAligns content to the right.
alignjustifyJustifies content.
alignnoneRemoves alignment.
pastetextToggles paste-as-plain-text mode.
visualaidToggles visual aids for invisible elements.

Bespoke controls

ButtonTypeDescription
fontfamilyNested menuFont family selector.
fontsizeNested menuFont size selector.
blocksNested menuBlock format selector (paragraph, headings, etc.).
alignNested menuText alignment selector.
stylesNested menuCustom styles selector.
forecolorSplit buttonText color picker.
hilitecolorSplit buttonHighlight color picker.
backcolorSplit buttonBackground color picker.

Built-in Menu Items

These items are registered by default and can be used in the menu option.

Menu ItemDescription
newdocumentClears the editor content.
undoUndoes the last action.
redoRedoes the last undone action.
copyCopies the selection.
cutCuts the selection.
pastePastes from the clipboard.
selectallSelects all content.
printOpens the print dialog.
boldToggles bold.
italicToggles italic.
underlineToggles underline.
strikethroughToggles strikethrough.
subscriptToggles subscript.
superscriptToggles superscript.
codeformatToggles inline code formatting.
removeformatRemoves formatting.
hrInserts a horizontal rule.
alignText alignment submenu.
blocksBlock format submenu.
fontfamilyFont family submenu.
fontsizeFont size submenu.
stylesCustom styles submenu.
pastetextToggles paste-as-plain-text.
visualaidToggles visual aids.