Context menus
The context menu is a configurable component that appears when the user right clicks in the editable area. By default it does not disable the operating system’s native context menu, if there are no items to display at the cursor position the native context menu will be shown.
The context menu supports both individual menu items and dynamic context menu sections.
Options
contextmenu
The contextmenu
option allows you to specify which items appear on the context menu. The format of this option is a space separated list of items in a string.
The context menu option accepts three styles of item:
-
Any registered menu item.
-
A
"|"
pipe character to indicate a separator should be added to delineate a group of menu items. -
Context menu sections defined by a plugin (usually equal to the plugin name). Separators are automatically inserted between context menu sections.
If the same name is registered as both a context menu section and a menu item, the context menu section takes preference.
The default configuration includes the context menu sections for the following plugins: link
, linkchecker
, image
, editimage
, permanentpen
, table
, and tinymcespellchecker
.
To disable the editor’s context menu, set this option to false
.
Type: String
or false
Default value: 'link linkchecker image editimage table spellchecker configurepermanentpen'
When the TinyMCE context menu is enabled, users can still access the browser context menu, including the browser spellchecker, using the Ctrl+Right click shortcut. However if the contextmenu_never_use_native option is enabled, holding the Ctrl key will have no effect.
|
When a TinyMCE context menu is configured, a user on a mobile device can access the TinyMCE context menu by a long press. However, when a TinyMCE context menu is not configured but a TinyMCE context toolbar is, long press will instead open the context toolbar. |
The native context menu on a mobile device can still be accessed with a TinyMCE context menu configured, either by a single tap on iOS, or by a double tap on Android. However if the contextmenu_never_use_native option is enabled, neither single nor double tap will have any effect.
|
contextmenu_never_use_native
The contextmenu_never_use_native
option allows you to prevent the browser context menu from appearing within the editor.
Using this option may result in unexpected behavior when right-clicking in text areas. We advise you to consider all your options carefully before using this feature. |
Type: Boolean
Default value: false
Available context menu sections
The following table shows all context menu sections, including sections provided by plugins. Any menu item can also be added to the context menu. To retrieve a list from the editor, run the following command from the browser console:
tinymce.activeEditor.ui.registry.getAll().contextMenus
The identifier below will add a context menu section containing one or more items. It is not possible to add individual items from the following context menu sections.
identifier | Core/Plugin | Description |
---|---|---|
image |
Adds the Image… item for opening the Insert/Edit Image dialog. |
|
editimage |
Adds the Edit image item for opening the Edit Image dialog. |
|
link |
Adds the Link… item for opening the Insert/Edit Link dialog. |
|
linkchecker |
Adds the Ignore item on links marked as broken, allowing the user to instruct linkchecker to ignore the link. |
|
lists |
Adds the List properties… item for opening the List Properties dialog. |
|
configurepermanentpen |
Adds the Permanent pen properties… item for opening the Permanent Pen Properties dialog when the permanent pen is in use. |
|
spellchecker |
Adds a list suggested corrections, an Ignore item, and an Ignore all item. |
|
table |
Adds table related context menu items, including Enhanced Tables context menu items (if the Enhanced Tables plugin is enabled). |
For a list of available menu items that can be added to the context menu, see: Menu Items Available for TinyMCE.
Registering context menu sections
The structure of context menu sections is a simple query system indexed by name. We strongly recommend using the name of the plugin as the context menu name for ease of configuration.
In the menu shown to the user, sections are delineated by separators. Sections can return an empty array of menu items to indicate that section has no applicable items to the current context and should not be shown.
type ContextMenuApi = {
update: (element: Element) => string | Array<ContextMenuContents>
}
editor.ui.registry.addContextMenu(name: string, feature: ContextMenuApi);
Every time the user opens the context menu, the selected element is passed to the update function which must return either a space separated string or an array of items to display. The types of the items returned are as follows:
type ContextMenuContents = string | ContextMenuItem | SeparatorMenuItemApi | ContextSubMenu
type ContextMenuItem = {
type?: 'item';
text: string;
icon?: string;
shortcut?: string;
disabled?: boolean;
onAction: () => void;
}
type ContextSubMenu = {
type: 'submenu';
text: string;
icon?: string;
shortcut?: string;
disabled?: boolean;
getSubmenuItems: () => string | Array<ContextMenuContents>;
}
type SeparatorMenuItemApi = {
type: 'separator'
}
The most common type to use is string
, which references an existing registered menu item.
ContextMenuItem
, ContextSubMenu
and SeparatorMenuItemApi
types are intended for use by plugins with completely dynamic menu requirements, where registering each menu item is not necessary. For example, the spellchecker shows a list of suggestions specific to the selected word.
When creating a dynamic menu, the structure type
properties are used in order to support untyped API usage:
-
type
item
(default) is a regular menu item, and must have anonAction
method. -
type
submenu
must havegetSubmenuItems
, and if it has anonAction
property it is ignored. -
type
separator
ignores all other properties.