triggers
Description
Optional. Defines dropdown triggers for inserting mentions, tags, and other tokens
When a user types a configured character (for example, @ or #), RichText opens a dropdown with predefined items. When the user selects an item, RichText inserts it into the document as a non-editable token (<a data-token="..." data-token-id="...">).
Usage
triggers?: Array<{
trigger: string,
data: Array<{ id?: string | number; label?: string; url?: string }>
| ((query: string) =>
Array<{ id?: string | number; label?: string; url?: string }>
| Promise<Array<{ id?: string | number; label?: string; url?: string }>>),
showTrigger?: boolean,
action?: (item) => void
}>;
Parameters
Each entry of the triggers array accepts the following fields:
trigger- (required) the character that opens the suggestion dropdown (for example,"@","#","/","$")data- (required) the data source for the dropdown; can be an array, a sync function, or an async function. See Data source formsshowTrigger- (optional) whentrue(default), RichText keeps the trigger character in the inserted token (for example,@Alice); whenfalse, RichText inserts onlylabel(for example,Alice)action- (optional) a custom callback called when a user selects an item. When set, RichText removes the typed trigger text (the trigger character plus the query) and callsaction(item)instead of inserting a token. The callback receives the picked item and can insert any content instead of the selected one. Theactionparameter takes priority overshowTrigger, which has no effect whenactionis set. See Custom action
Data source forms
- Static array — RichText filters the array automatically by matching the query against
label(case-insensitive,startsWith):
new richtext.Richtext("#root", {
triggers: [{
trigger: "@",
data: [
{ id: "alice", label: "Alice" },
{ id: "bob", label: "Bob" }
]
}]
});
- Sync function — RichText calls your function with the current
querystring; you do the filtering and return the matching array:
new richtext.Richtext("#root", {
triggers: [{
trigger: "#",
data: query => tags.filter(t =>
t.label.toLowerCase().startsWith(query.toLowerCase())
)
}]
});
- Async function — RichText calls your function with the current
querystring; return aPromisethat resolves to the matching array. Useful for server-side search:
new richtext.Richtext("#root", {
triggers: [{
trigger: "+",
data: async query => {
const res = await fetch(`/api/users?q=${encodeURIComponent(query)}`);
const users = await res.json();
return users.map(u => ({ id: String(u.id), label: u.name, url: u.website }));
}
}]
});
Suggestion item fields
Each item in data (or each item returned by a function) has the following fields:
id- (optional) unique identifier saved on the inserted token. If omitted, RichText generates an ID automaticallylabel- (optional) the text shown in the dropdown and inserted into the document. Required only for the default rendering; with a customtriggerTemplateyou can render items from other fields (for example,template(({ data }) => data.id)) and omitlabelurl- (optional) URL associated with the item. RichText stores the URL as the inserted token'shrefattribute.Ctrl+Clickon the token opens the link
An item may also include any number of custom fields beyond id, label, and url (for example, code for an emoji, or image and name for an avatar). These extra fields are passed through to the triggerTemplate callback and to the action callback.
Rendered token
When a user selects an item in the dropdown, RichText inserts a non-editable token element into the document:
<a data-token="@" data-token-id="alice" href="mailto:alice@example.com">@Alice</a>
@(indata-token="@") - the item'striggeralice(indata-token-id="alice") - the item'sidmailto:alice@example.com(inhref="mailto:alice@example.com") - the item'surl@Alice- the combination oftriggerandlabel; withshowTrigger: falseit would be justAlice
Use the data-token and data-token-id attributes to target tokens with CSS, for example, to highlight all mentions of a user:
.wx-editor-content a[data-token="@"][data-token-id="alice"] {
background: #fb8500;
color: #fff;
}