Aa 0.14.1

Atoms

An atom is a piece of the text drawn as one thing rather than letter by letter: a picture, an equation, a mention, a link shown as its title. The Markdown keeps the atom’s words exactly as written; only the drawing changes, and the caret steps over it whole.

Atoms in a line

Move the caret along the lines: it steps over the picture whole, and opens the equation as TeX.

What it stores

A picture, ![name](address), is an atom out of the box, and so is an equation. A product names its own in atoms.

type Atom = {  /** The atom's kind: `data-atom` on its element. */  name: string  /** How many characters an atom of this kind takes at `at` in a line, or 0 when none starts there. */  match(line: string, at: number): number  /** A fenced code block in this language is drawn whole as this atom: `diagram` for a diagram block. */  fence?: string  /** Fills the element Aa gives it; what it returns is called once the atom leaves the page. */  draw?(element: HTMLElement, source: string): void | (() => void)  /** A block that starts one, offered in the slash menu. */  insert?: { label: string; icon: string; source: string; keywords?: string }}
FieldWhat it does
nameNames the kind, and is written on its element as data-atom.
matchFinds the atom in a line of a block’s Markdown. Atoms are tried in the order given, the product’s before the equation, and the first that matches takes the characters. Nothing is matched inside a link or code.
fenceDraws a whole closed fenced block as this atom when the first word after its opening fence is this, ignoring case. Such an atom’s match returns 0.
drawFills the element with the drawing. Without it, the atom shows its own words.
insertAdds the atom to the slash menu, and on touch to the toolbar, as label with icon (see Icons), found by its label or keywords. It writes source in place of an empty block or after the caret’s block, and puts the caret at the start of its second line (of a one-line source, at its start).
import { createEditor, imageAtom, atomPattern, type Atom } from '@0001-labs/aa'// @[Ann Lund] in the Markdown, drawn as the name alone.const mention: Atom = {  ...atomPattern('mention', /@\[[^\]\n]+\]/),  draw(element, source) {    element.classList.add('mention')    element.textContent = source.slice(2, -1)  },}createEditor(host, { atoms: [imageAtom, mention] })

atoms replaces the default, [imageAtom]: keep imageAtom in the array to keep pictures.

Aa makes each atom’s element, an uneditable span.aa-atom, and hands it to draw:

AttributeWhat it is
class="aa-atom"Every atom.
data-atomThe atom’s name.
data-sourceThe atom’s words, exactly as written. This is what reads back into the Markdown.
contenteditable="false"The caret stands beside the atom, never in it.

An atom drawn once is kept when its block is drawn again for the same words, so a picture does not load again as the caret moves past it. When it leaves the page, what draw returned is called: stop a timer, an observer or a request there.

const clock: Atom = {  ...atomPattern('clock', /\{now\}/),  draw(element) {    const tick = () => { element.textContent = new Date().toLocaleTimeString() }    tick()    const timer = setInterval(tick, 1000)    return () => clearInterval(timer)  },}

atomElement(name, source) makes the same element, for a product drawing an atom outside the editor.

atomPattern(name, pattern, accept?) makes name and match from a regular expression, matched where the line is being read. accept can refuse a match:

// A ticket number, of three digits or more.const ticket = atomPattern('ticket', /#\d+/, found => /^#\d{3,}$/.test(found[0]))

IMAGE is the picture’s own pattern, and DEFAULT_ATOMS the patterns a reader of the Markdown uses unless it is given others: the picture and the equation.

imageAtom draws ![name](address) as the picture, with its name as an editable caption under it (right-click the picture and choose Add a caption when it is empty). An address ending in mp4, webm or mov, or a data:video/ address, is drawn as a video with its controls; mp3, wav, ogg or m4a, or a data:audio/ address, as audio. See Pictures and media.

An atom can be changed where it is drawn, as a diagram is on its board. Mark the element inside it that takes the keys and presses with data-aa-interactive:

  • The editor leaves the selection there alone, and the block drag and the selection across blocks do not start from it.
  • For an atom with a fence, the caret coming into its block hands the focus to its first [data-aa-interactive] element, which must be focusable (a tabindex).
  • While the focus is in it, undo and redo draw the change again without opening the atom as words.

Captions (.aa-caption) are left alone the same way.

An atom talks to the editor with events dispatched from inside its element, bubbling:

EventdetailWhat the editor does
aa-atom-change{ source: string }Writes source as the atom’s whole block, one step to undo, and calls onChange. The caret stays where it was, so the atom is not opened as words. Refused in a read-only editor.
aa-atom-leave{ forward?: boolean }Gives the keys back: the caret goes to the start of the next block, or with forward: false to the end of the one before.
aa-atom-prompt{ prompt, text, signal }Sets detail.available to whether the editor has prompt, and detail.answer to the answer, a promise of Markdown, asked with kind: 'diagram'.
draw(element, source) {  const board = document.createElement('div')  board.tabIndex = 0  board.setAttribute('data-aa-interactive', '')  board.addEventListener('keydown', event => {    if (event.key === 'Escape') board.dispatchEvent(new CustomEvent('aa-atom-leave', { bubbles: true, detail: { forward: true } }))  })  // A change made on the board, handed back as the block's new Markdown.  const write = (next: string) => board.dispatchEvent(new CustomEvent('aa-atom-change', { bubbles: true, detail: { source: next } }))  element.append(board)}

source in aa-atom-change is the whole block’s Markdown, fences included: for a fenced atom that is the atom itself.

Diagrams · Pictures and media · Equations

personAtom(people, colour) makes @Name an atom for the names people() gives. The document keeps the words @Name, so nothing changes in what is stored, and a name typed by hand becomes a tag as well.

import { createEditor, imageAtom, personAtom } from '@0001-labs/aa'const editor = createEditor(host, {  atoms: [imageAtom, personAtom(() => ['Ann Lund', 'Bo Ek'], name => colourOf(name))],})

One Backspace removes a tagged person, the space after them included, and Delete just before them does the same. Every atom is removed in one press; tag: true on an atom of your own adds the space.