Skip to content

Describe new API related to triggers#46

Open
serhiipylypchuk1991 wants to merge 13 commits into
sp-next-v-2-1from
sp-next-v-SVAR-1033
Open

Describe new API related to triggers#46
serhiipylypchuk1991 wants to merge 13 commits into
sp-next-v-2-1from
sp-next-v-SVAR-1033

Conversation

@serhiipylypchuk1991
Copy link
Copy Markdown
Contributor

No description provided.

- 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
Copy link
Copy Markdown

@kullias kullias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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})`);
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  editor.api.on("insert-token", ({ data, trigger, showTrigger }) => {
      console.log(`Inserted ${trigger}${data.label} (id: ${data.id})`);
  });

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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})`);
});
~~~
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  editor.api.on("insert-token", ({ data, trigger, showTrigger }) => {
      console.log(`Inserted ${trigger}${data.label} (id: ${data.id})`);
  });

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Aligned the guide handler to the same ({ data, trigger, showTrigger }) form.

Comment thread docs/api/config/triggers.md Outdated
| ((query: string) =>
Array<{ id?: string; label?: string; url?: string }>
| Promise<Array<{ id?: string; label?: string; url?: string }>>),
showTrigger?: boolean
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also add action?: (item) => void - that's the same custom action that in "insert-token"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Added action?: (item) => void to the triggers Usage block and a matching field description, plus a Custom action section.

Comment thread docs/api/events/show-suggest.md Outdated
"show-suggest": ({
trigger: string,
query: string,
items: Array<{ id: string, label: string, url: string }>,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Copy link
Copy Markdown

@DmitriySlabodchikov DmitriySlabodchikov Jun 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where:

  • @ is trigger
  • alice is id
  • mailto:alice@example.com is url
  • @Alice - combination of trigger and label, in case of showTrigger: false it would be just Alice


#### 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:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 === "@") {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really required, u can use custom template (for example: triggerTemplate: template(({data}) => data.id))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants