Custom Basic menu items
A basic menu item triggers its onAction
function when clicked.
Options
Name | Value | Requirement | Description |
---|---|---|---|
text |
string |
optional |
Text to display if no icon is found. |
icon |
string |
optional |
Name of the icon to be displayed. Must correspond to an icon: in the icon pack, in a custom icon pack, or added using the |
value |
string |
optional |
A value to associate with the menu item. |
enabled |
boolean |
optional |
default: |
onSetup |
|
optional |
default: |
onAction |
|
required |
Function invoked when the menu item is clicked. |
shortcut |
string |
optional |
Text to display in the shortcut label. To register a shortcut, see: Add custom shortcuts to TinyMCE. |
NOTE: This feature is only available for TinyMCE 7.4 and later. |
|||
context |
string |
optional |
default: |
API
Name | Value | Description |
---|---|---|
isEnabled |
|
Checks if the menu item is enabled. |
setEnabled |
|
Sets the menu item’s enabled state. |
Example: creating a basic menu item
tinymce.init({
selector: 'textarea',
menu: {
custom: { title: 'Custom Menu', items: 'undo redo basicitem' }
},
menubar: 'file edit custom',
setup: (editor) => {
editor.ui.registry.addMenuItem('basicitem', {
text: 'My basic menu item',
onAction: () => alert('Menu item clicked')
});
}
});
Using onSetup
onSetup
is a complex property. It takes a function that is passed the component’s API and should return a callback that is passed the component’s API and returns nothing. This occurs because onSetup
runs whenever the component is rendered, and the returned callback is executed when the component is destroyed. This is essentially an onTeardown
handler, and can be used to unbind events and callbacks.
To clarify, in code onSetup
may look like this:
onSetup: (api) => {
// Do something here on component render, like set component properties or bind an event listener
return (api) => {
// Do something here on teardown, like unbind an event listener
};
};
To bind a callback function to an editor event use editor.on(eventName, callback)
. To unbind an event listener use editor.off(eventName, callback)
. Any event listeners should be unbound in the teardown callback. The only editor event which does not need to be unbound is init
e.g. editor.on('init', callback)
.
|