Describe new API related to triggers#46
Conversation
- new API reference page for the hide-suggest event - documents when the dropdown closes (selection, Escape, cursor leaves trigger, empty result)
- new API reference page for the insert-token event - documents callback fields (id, label, url, trigger, showTrigger)
- new API reference page for the show-suggest event - documents callback fields (trigger, query, items, pos) and intercept example
- new API reference page for the triggers property - covers static array, sync function, async function data sources - documents suggestion item fields and rendered token CSS targeting
- new API reference page for the triggerTemplate property - documents callback parameters (data, trigger) and template usage - includes tip on overriding the dropdown anchor width
- new guide covering trigger characters, data source forms, token rendering, and event handling - examples for static array, sync function, async function, and custom dropdown templates
- list hide-suggest, insert-token, and show-suggest in the events table
- list triggers and triggerTemplate in the properties table
- list triggers in the property overview with a link to the Mentions and tags guide
- update anchor in whats_new from #custom-toolbar-controls to #add-custom-toolbar-controls to match the heading in configuration.md
- add hide-suggest, insert-token, show-suggest under events - add triggers and trigger-template under properties - add mentions_and_tags under guides
kullias
left a comment
There was a problem hiding this comment.
the code changes somewhat in a side branch on custom actions, I added comments, for source code DM either me or the author of the code, we'll point to the branch
| url: string, | ||
| trigger: string, | ||
| showTrigger?: boolean | ||
| }) => boolean | void; |
There was a problem hiding this comment.
data: ISuggestionItem;
trigger: string;
showTrigger?: boolean;
action?: (config: ISuggestionItem) => void;
//where
export interface ISuggestionItem {
id?: TID;
label?: string;
url?: string;
[key: string]: any;
}action added a couple of days ago, the rest not sure when changed, but changed
There was a problem hiding this comment.
Done. Updated the Usage block: id/label/url are now wrapped in a data object (all optional, plus a note for custom fields), and action? was added.
| - `url` - the url associated with the item (becomes the `href` attribute of the inserted token) | ||
| - `trigger` - the trigger character that opened the dropdown (for example, `"@"` or `"#"`) | ||
| - `showTrigger` - when `false`, RichText inserts only `label`; otherwise it also shows the trigger character (default) | ||
|
|
There was a problem hiding this comment.
descriptions are fine, just update the structure (data carries some of them)
and add action - custom action that executes when the option is chosen
also worth noting that showTrigger can't be used together with action - it's either or. action is prioritized. is action is set, showTrigger is assumed false. the command is executed, the /sometext is removed.
There was a problem hiding this comment.
Done. data now carries the item fields, and added action with a note that it takes priority over showTrigger: when action is set, the typed text (trigger + query) is removed and showTrigger has no effect.
| editor.api.on("insert-token", (obj) => { | ||
| console.log(obj); | ||
| console.log(`Inserted ${obj.trigger}${obj.label} (id: ${obj.id})`); | ||
| }); |
There was a problem hiding this comment.
editor.api.on("insert-token", ({ data, trigger, showTrigger }) => {
console.log(`Inserted ${trigger}${data.label} (id: ${data.id})`);
});There was a problem hiding this comment.
Done. Example handler now uses ({ data, trigger, showTrigger }) and logs data.label / data.id.
| editor.api.on("insert-token", ({ trigger, label, id }) => { | ||
| console.log(`Inserted ${trigger}${label} (id: ${id})`); | ||
| }); | ||
| ~~~ |
There was a problem hiding this comment.
editor.api.on("insert-token", ({ data, trigger, showTrigger }) => {
console.log(`Inserted ${trigger}${data.label} (id: ${data.id})`);
});There was a problem hiding this comment.
Done. Aligned the guide handler to the same ({ data, trigger, showTrigger }) form.
| | ((query: string) => | ||
| Array<{ id?: string; label?: string; url?: string }> | ||
| | Promise<Array<{ id?: string; label?: string; url?: string }>>), | ||
| showTrigger?: boolean |
There was a problem hiding this comment.
also add action?: (item) => void - that's the same custom action that in "insert-token"
There was a problem hiding this comment.
Done. Added action?: (item) => void to the triggers Usage block and a matching field description, plus a Custom action section.
| "show-suggest": ({ | ||
| trigger: string, | ||
| query: string, | ||
| items: Array<{ id: string, label: string, url: string }>, |
There was a problem hiding this comment.
this one now allows also arbitrary keys. in TS that would be [key: string]: any - custom fields used by triggerTemplate examples like image, name. and the ones you already have here are all optional.
There was a problem hiding this comment.
Done. items is now typed with optional id/label/url plus arbitrary custom keys, and the description mentions custom fields like image/name used by triggerTemplate.
- add `action` field to triggers config with Custom action section (emoji insertion and slash-style command menu examples) - update insert-token event payload: id/label/url replaced by `data` object carrying the picked item plus custom fields; add `action` - note that `action` takes priority over `showTrigger` - show-suggest items now documented as optional fields + custom keys - extend mentions guide with custom action examples - fix typos, a broken anchor, and align wording across pages
- destructure ({ data, trigger, showTrigger }) to match the
insert-token event docs and the PR review suggestion
|
|
||
| - `id` - (optional) unique identifier saved on the inserted token. If omitted, RichText generates an ID automatically | ||
| - `label` - (required) the text shown in the dropdown and inserted into the document | ||
| - `url` - (optional) URL associated with the item. RichText stores the inserted token URL as the `href` attribute. |
There was a problem hiding this comment.
ctrl-click to open this link
| When a user selects an item in the dropdown, RichText inserts a non-editable token element into the document: | ||
|
|
||
| ~~~html {} | ||
| <a data-token="@" data-token-id="alice" href="mailto:alice@example.com">@Alice</a> |
There was a problem hiding this comment.
Where:
@istriggeraliceisidmailto:alice@example.comisurl@Alice- combination oftriggerandlabel, in case ofshowTrigger: falseit would be justAlice
|
|
||
| #### Add emoji | ||
|
|
||
| A common use case is inserting an emoji from a `:` trigger, where each item contains a custom `code` field. Pair `action` with [`triggerTemplate`](api/config/trigger-template.md) so the dropdown shows the emoji itself instead of just its label: |
There was a problem hiding this comment.
maybe it's better to use this snippet code with categories (it is also a good example of custom data filtering): https://snippet.dhtmlx.com/r16rmt4m
| new richtext.Richtext("#root", { | ||
| triggers: [{ trigger: "@", data: people }], | ||
| triggerTemplate: template(obj => { | ||
| if (obj.trigger === "@") { |
There was a problem hiding this comment.
maybe add one more trigger, because otherwise there is no point in checking for equality to a "@"
| - `trigger` - the trigger character that opened the dropdown (`"@"`, `"#"`, etc.) | ||
|
|
||
| :::tip | ||
| The dropdown anchor has a fixed default width of `160px`. To make room for a wider template, override the width from a parent stylesheet (`!important` is needed because the widget sets the default via its own scoped CSS): |
There was a problem hiding this comment.
u can just make bit more complex css rule, like .wx-editor .wx-suggest-anchor instead of !important
| Each item in the `data` object (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 automatically | ||
| - `label` - (required) the text shown in the dropdown and inserted into the document |
There was a problem hiding this comment.
not really required, u can use custom template (for example: triggerTemplate: template(({data}) => data.id))
No description provided.