Extensions

callout

Node 0.64 kB gzipped

A callout — a Notion block, an admonition, an aside with a colour.

Holds blocks, so a callout can carry a list or a code sample, and carries a type for the colour and an optional emoji for the icon. The icon is outside the editable content, so the caret cannot land in it.

1 · Install

One package for every extension · there is no @matrajs/callout 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, callout } from '@matrajs/core'

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

3 · Use it

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

editor.commands.toggleCallout()
editor.focus()

From the keyboard: Enter .

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

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

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

const read = () => button.setAttribute('aria-pressed', String(editor.isActive('callout')))
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.

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
toggleCallout editor.commands.toggleCallout(type?: CalloutType)
setCalloutType editor.commands.setCalloutType(type: CalloutType)
setCalloutEmoji editor.commands.setCalloutEmoji(emoji: string | null)
exitCallout editor.commands.exitCallout()

Keyboard shortcuts

Mod is Command on a Mac and Control everywhere else. To rebind one, put your own extension after this in the array · later keys win.

Styling

This one needs CSS to look right, and ships the minimum as a string rather than a file · nothing loads unless you load it, and you can ignore it and write your own.

import { calloutCSS } from '@matrajs/core'

const style = document.createElement('style')
style.textContent = calloutCSS
document.head.append(style)

In the document

Attribute Default
type "info"
emoji none

← All 83 extensions

Edit this page on GitHub