Context
The context property for UI components in TinyMCE enables dynamic activation or deactivation of buttons and menu items based on predefined or custom conditions.
Registering a Context
Methods for adding custom contexts are available in the UI Registry section of the editor API editor.ui.registry
.
-
editor.ui.registry.addContext(identifier, predicate: (value: string) ⇒ boolean)
These methods accept two arguments:
-
identifier
- a unique name for the context -
predicate
- a function that determines whether the context condition is met. It accepts a single string parameter and returns a boolean:-
Parameter (string): The value provided in the context syntax (e.g., in 'mode:design', 'design' would be passed to the predicate).
-
Return value (boolean): Returns true if the condition is met (the component should be enabled); otherwise, returns false.
-
setup: (editor) => {
editor.ui.registry.addContext('mode', (value: string) => {
return value === editor.mode.get();
});
}
Context Syntax
Contexts are specified using the syntax: <key>:<value>
.
-
key
- the type of context, used to look up registered contexts. -
value
- the condition passed to the predicate function for evaluation.
To negate any context, place ! before the value, using the format: <key>:!<value> .
|
Built-in Contexts
TinyMCE provides several built-in contexts:
Name | Description |
---|---|
|
This context is always enabled, regardless of the editor’s state. |
|
This context is used to enable or disable UI components based on the editor’s current mode.
|
|
This context determines if a specific child element can be inserted at the current selection. It uses
|
|
This context checks if a specific format can be applied to the current selection, using
|
|
This context checks if the current selection is editable using |
Using Context in UI Components
To utilize the context in a UI component, apply the context
property.
setup: (editor) => {
editor.ui.registry.addButton('customToolbarButton', {
text: 'button',
onAction: () => {
console.log('clicked');
},
context: 'any'
});
}
State Updates
The following events trigger state updates for UI components:
During these updates, UI components re-evaluate their contexts and adjust their enabled or disabled state as needed: |
-
NodeChange
: Triggered when the selected node within the editor content changes.-
Affects: All contexts except mode, including formatting, editable, insert, and custom contexts.
-
-
SwitchMode
: Triggered when the editor’s mode is changed. -
init
: Triggered when the editor is fully initialized.