Creating custom Menu toolbar buttons
A toolbar menu button is a toolbar button that opens a menu when clicked. This menu can also contain submenus. This is useful for grouping together actions that would otherwise be several buttons on the toolbar. It can also be used to reduce visual clutter and save UI space, as menubar menu items and some toolbar buttons could be moved into a toolbar menu button. Potentially, all menubar menu items could be moved into toolbar menu buttons, allowing for the editor to be used without a menubar at all.
For example: The table plugin’s table toolbar button opens a menu similar to the menubar Table menu.
Options
| Name | Value | Requirement | Description |
|---|---|---|---|
fetch |
|
required |
Function that takes a callback and a |
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 |
search |
boolean or object |
optional |
If not false, adds a search field to the menu. For more details, see Searchable Menu Buttons |
tooltip |
string |
optional |
Text for button tooltip. |
onSetup |
|
optional |
default: |
context |
string |
optional |
default: |
API
| Name | Value | Description |
|---|---|---|
isEnabled |
|
Checks if the button is enabled. |
setEnabled |
|
Sets the button’s enabled state. |
setText |
|
Sets the text label to display. |
setIcon |
|
Sets the icon of the button. |
Menu button example and explanation
The following is a simple toolbar menu button example:
This example configures a toolbar menu button with the label My Button that opens the specified menu when clicked. The top-level menu contains two items. The first menu item inserts content when clicked and the second menu item opens a submenu containing two menu items which insert content when clicked.
The fetch function is called when the toolbar menu button’s menu is opened. It is a function that takes a callback and passes it an array of menu items to be rendered in the drop-down menu. This allows for asynchronous fetching of the menu items.
Searchable menu buttons
The button’s menu can be configured to have an input field for searching, as well as its usual items. The presence of the input field is controlled by the search field in the options. The search field can be a boolean or an object containing a single optional string placeholder. By default, search is false. If search is not false, the button’s menu will contain an input field with any specified placeholder text. As the user types into this field, the fetch function will be called with the text in the input field passed back as part of fetchContext. The fetch function is responsible for using this fetchContext to determine which items to pass to its success callback.
The fetchContext is an object containing a single string property: pattern. If the toolbar menu button has not configured search to be active, then the pattern string will be empty.
Searchable menu button example and explanation
The following is a simple toolbar menu button example, where searching has been configured:
This example configures a toolbar menu button with the label My searchable button that opens the specified menu when clicked. The menu will contain a search input field because search is not false. The input field’s placeholder attribute will be Type....
Initially, when the menu opens, the search input field will be empty, and the fetch function is called with an empty pattern for its fetchContext. In that situation, fetch passes back an array of two items to be rendered in the drop-down menu. When the user types in the input field, fetch will be called again, except this time, the pattern property in fetchContext will reflect the value typed in the input field. For illustration purposes, this example then passes back an item that contains this pattern inside the item’s text. In a more real-life example, the pattern could be used to filter which of the items are passed to the callback.
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).
|