Custom toolbar menu button
This example shows you how to add a simple menu button to TinyMCE’s toolbar. This demo inserts text at the cursor. The live demo’s JS tab also shows you how to add sub-menus to a menu item.
-
TinyMCE
-
HTML
-
JS
-
Edit on CodePen
<textarea id="custom-toolbar-menu-button">
<p><img style="display: block; margin-left: auto; margin-right: auto;" title="Tiny Logo" src="https://www.tiny.cloud/docs/images/logos/android-chrome-256x256.png" alt="TinyMCE Logo" width="128" height="128" /></p>
<h2 style="text-align: center;">Welcome to the TinyMCE editor demo!</h2>
<p>Select a menu item from the listbox above and it will insert contents into the editor at the caret position.</p>
<h2>Got questions or need help?</h2>
<ul>
<li>Our <a href="https://www.tiny.cloud/docs/">documentation</a> is a great resource for learning how to configure TinyMCE.</li>
<li>Have a specific question? Visit the <a href="https://support.tiny.cloud">Support Portal</a>.</li>
<li>We also offer enterprise grade support as part of <a href="https://www.tiny.cloud/pricing">TinyMCE premium plans</a>.</li>
</ul>
<h2>Found a bug?</h2>
<p>If you think you have found a bug please create an issue on the <a href="https://github.com/tinymce/tinymce/issues">GitHub repo</a> to report it to the developers.</p>
<h2>Finally ...</h2>
<p>Don't forget to check out our other product <a href="http://www.plupload.com" target="_blank">Plupload</a>, your ultimate upload solution featuring HTML5 upload support.</p>
<p>Thanks for supporting TinyMCE! We hope it helps you and your users create great content.
<br>All the best from the TinyMCE team.</p>
</textarea>
tinymce.init({
selector: 'textarea#custom-toolbar-menu-button',
height: 500,
toolbar: 'mybutton',
setup: function (editor) {
/* Menu items are recreated when the menu is closed and opened, so we need
a variable to store the toggle menu item state. */
var toggleState = false;
/* example, adding a toolbar menu button */
editor.ui.registry.addMenuButton('mybutton', {
text: 'My button',
fetch: function (callback) {
var items = [
{
type: 'menuitem',
text: 'Menu item 1',
onAction: function () {
editor.insertContent(' <em>You clicked menu item 1!</em>');
}
},
{
type: 'nestedmenuitem',
text: 'Menu item 2',
icon: 'user',
getSubmenuItems: function () {
return [
{
type: 'menuitem',
text: 'Sub menu item 1',
icon: 'unlock',
onAction: function () {
editor.insertContent(' <em>You clicked Sub menu item 1!</em>');
}
},
{
type: 'menuitem',
text: 'Sub menu item 2',
icon: 'lock',
onAction: function () {
editor.insertContent(' <em>You clicked Sub menu item 2!</em>');
}
}
];
}
},
{
type: 'togglemenuitem',
text: 'Toggle menu item',
onAction: function () {
toggleState = !toggleState;
editor.insertContent(' <em>You toggled a menuitem ' + (toggleState ? 'on' : 'off') + '</em>');
},
onSetup: function (api) {
api.setActive(toggleState);
return function() {};
}
}
];
callback(items);
}
});
},
content_style: 'body { font-family:Helvetica,Arial,sans-serif; font-size:14px }'
});