Extensions

mention

Node takes options 0.21 kB gzipped

A mention, as a node rather than styled text.

An atom: the caret steps over it, backspace removes the whole thing, and it cannot be half-deleted into `@Nahi`. That is the entire argument for making it a node — styled text looks identical until someone edits it, and then it quietly stops being a reference to anybody.

The id travels with it, so the document keeps a reference rather than a name that was true when it was typed.

1 · Install

One package for every extension · there is no @matrajs/mention to install.

npm i @matrajs/core

2 · Add it to the editor

Import it and put it in the extensions array. That array is the whole feature list: what is not in it is not in your bundle and not in the document's schema.

import { createEditor, document, paragraph, text, mention } from '@matrajs/core'

const editor = createEditor({
  extensions: [document, paragraph, text, mention({ … })],
  element: document.querySelector('#editor'),
})

3 · Use it

Run a command, and give focus back · a click on a button takes it away.

editor.commands.insertMention()
editor.focus()

Wired to a button, with the pressed state kept in step:

const button = document.querySelector('#insertMention')

button.addEventListener('click', () => {
  editor.commands.insertMention()
  editor.focus()
})

const read = () => button.setAttribute('aria-pressed', String(editor.isActive('mention')))
editor.on('selectionChange', read)
editor.on('change', read)
read()

Both events, not one. selectionChange covers the caret moving into it; change covers the command that changed it without moving anything. A toolbar wired to only the first goes stale the moment someone uses the keyboard.

Options

mention is a function · call it with an options object.

Option Type What it does
render (attrs: { id: string What appears in the document. Defaults to `@label`.
label required string —
name string Node name, if you need two kinds of mention in one editor.

Commands

Every one returns true if it changed the document and false if it could not · a command that does not apply where the caret is says so rather than throwing.

Command Call
insertMention editor.commands.insertMention()

In the document

Attribute Default
id none
label ""

← All 83 extensions

Edit this page on GitHub