Mentioning teammates
An @name inline Atom, typing “@” for a list at the caret, a “/” item with a search popover and labels through Tool options.
A mention is an inline Atom holding a person's id. It is started by
typing “@”: a list of teammates opens at the caret, and picking one replaces the typed @query
with the Atom and a space after it. The same Atom is inserted from “/”: the item opens a popover with
a search over the list.
Try it
Type “@”, start typing a name and press Enter. Or type “/” and choose “Mention a teammate”.
Type ada@mail.com: inside a word the list does not open.
How it works
The Trigger character
The Trigger character is set by the Tool's trigger field (char,
items, onSelect), which opens the same list as “/” but on its own character. The kit renders the
list: search by what is typed, arrow keys, Enter, Escape and a “Nothing found” row.
items(query) supplies the entries, synchronously or as a promise; the Tool filters them itself.
The list opens when “@” appears at the start of a Block or after a space; the query runs up to the next
space. Inside a word (ada@mail.com), in a code block and in inline code it stays closed, and in
readOnly it never opens.
onSelect and range
The kit does not delete what was typed. onSelect(item, { editor, range, query }) receives range,
the typed text together with “@”, and replaces it with the Atom and a space through insertContentAt
in one transaction, so Ctrl/⌘+Z brings back @query in a single step. The kit closes the list after
the call.
Insert item with a popover
The Insert item (insertItems) stays as a fallback: it carries popover
instead of run, “/” is removed and the popover appears where it was. The Atom lands right at the
caret, in one transaction together with the space. The Slash menu ("insertMenu") is not in the
basic Preset: the example adds it to basic, or the item would not appear in “/”.
The id is a list attribute
Only data-acme_id is in the Document; an id that is not on the list is dropped, both on paste
and on the server. The node view renders the name, and the
Atom text (renderText) supplies @Grace Hopper to getText(),
copying, Markdown and maxLength.
Labels are Tool options
The kit's message catalog (UiProvider) has no strings for custom Tools, so the Tool keeps its
labels in its options. defineTool sets their defaults in defaults, and
tool(definition, options) overrides them for a screen. Here the screen replaces one label.
The tools array is built once per module
The tools array is built at module level, not in render: the Editor reads it when it creates the
engine.
In a real app the people list comes from the server. A list Schema rule won't fit then — use a
pattern for your id format and check permissions on the server. The trigger's entries can come
from the server too: items may return a promise.