Aa 0.14.1

Commands

A product adds its own actions in three places: commands in the slash menu, blockCommands in the block menu, and toolbar actions on the bar over selected words. Each is { id, label, icon, run(editor) }, and run is handed the editor to act through.

The slash menu

Press Enter at the end of the line and type /, then a word such as list. Press ⌘/ for the block’s own commands.

What it stores

import { createEditor, type Command } from '@0001-labs/aa'const today: Command = {  id: 'today',  label: 'Today’s date',  icon: 'text',  keywords: 'date day now',  run(editor) {    const at = editor.selection()    if (at) editor.replace(at.start, at.end, new Date().toLocaleDateString())  },}createEditor(host, { commands: [today] })
type ToolbarAction = {  id: string  label: string  icon: string  /** Named in the button's tooltip after its label, as ⌘B. */  shortcut?: string  run(editor: Editor): void}
FieldWhat it does
idNames the action, written on its button as data-action.
labelThe button’s accessible name, and its tooltip.
iconOne of the icons, drawn one square.
shortcutWords for the tooltip after the label. It does not bind a key: the product listens for the key itself.
runCalled with the editor, the selected words selected again first.

Give the actions as toolbar. They stand after Aa’s own buttons: Text colour, Bold, Italic, Underline, Strikethrough, Link, Code and Equation, then Comment where the editor has a comments store.

createEditor(host, {  toolbar: [{    id: 'quote-it',    label: 'Quote',    icon: 'quote',    run: editor => editor.turnInto('quote'),  }],})

An array turns the toolbar on, an empty one too. See Toolbar.

type Command = ToolbarAction & {  /** More words the slash menu finds it by. */  keywords?: string  /** One of TEXT_COLORS: its row's icon is drawn in that colour. */  ink?: string  /** Whether it is offered: asked each time the menu opens. */  enabled?(editor: Editor): boolean}
FieldWhat it does
keywordsWords the slash menu finds the command by, beside its label. What is typed after / must stand in one or the other, ignoring case.
inkDraws the row’s icon in one of the text colours, 'gray', 'red', 'orange', 'green', 'blue' or 'purple', as the colour rows are.
enabledLeaves the command out of the menu when it returns false.

commands follow the block kinds, what the atoms insert (see Atoms) and Image or file (with onFiles). Typing / and a word narrows them all by label and keywords. Chosen, the typed / and its word are taken out and the command run, as one step to undo. On touch the commands are under More in the Insert block sheet.

blockCommands follow Aa’s own in the block menu that ⌘/ opens: the block kinds, Duplicate, Delete, Move up, Move down, Indent and Outdent in a list, and a table’s rows and columns. The menu ends with the document’s counts. The commands act on the block the menu was opened for, the caret’s. With a pointer it is selected while the menu is open; on touch the caret stays in it.

createEditor(host, {  blockCommands: [{    id: 'copy-block',    label: 'Copy as Markdown',    icon: 'duplicate',    run(editor) {      const block = editor.activeBlock()      if (block) navigator.clipboard.writeText(editor.value.slice(block.start, block.end))    },  }],})

A command does not run in a read-only editor. See Slash and block menus.

icon names one of the Design system 1 icons Aa carries. A name it does not carry leaves the cell empty.

IconNames
Marksbold, italic, underline, strike, code, link, color, equation, mention
Blockstext, h1, h2, h3, bullet, numbered, todo, quote, code-block, callout, toggle, divider, table, image
Movingup, down, indent, outdent, handle, chevron-up, chevron-down
Actionsplus, duplicate, delete, close, check, search, send, comment, note, slash, connect