Aa 0.14.1

Decorations

A decoration draws a class over a run of the text without writing anything into the Markdown. Comments, find, AI’s changes and other people’s selections are all decorations, each on a layer of its own; a product adds its own the same way.

Find, a decoration layer

Press ⌘F and look for “square”: each match is a range on the find layer, and the Markdown stays the same.

What it stores

editor.decorate([{ from: 0, to: 5, className: 'spelling', label: 'Check this word' }], 'spelling')
type Decoration = {  from: number  to: number  className: string  label?: string  attributes?: Record<string, string>}
FieldWhat it does
from, toThe run, as offsets into the Markdown. from equal to to is a caret.
classNameOne class or several, separated by spaces, put on each span the run is drawn in.
labelThe span’s title. On a caret, the name its avatar is lettered from.
attributesAttributes written on each span. Where two runs cover the same words, their values stand side by side, separated by a space.

The run is drawn as spans inside the words it covers, cut where other runs begin and end, so runs may overlap. Syntax that is never shown, and atoms, are not decorated.

decorate(ranges, layer) replaces everything on layer with ranges, and leaves the other layers as they are. decorate([], layer) clears one. Without a layer, the ranges go on external.

LayerWhoseClass
externalThe product’s, when no layer is namedIts own
commentsThe comments store’s threadsaa-comment, with aa-comment--open, aa-comment--unread, aa-comment--resolved and aa-comment--draft
findFind and replaceaa-find-match
aiThe words an answer from AI changed or wroteaa-ai-changed
presenceOther people’s selections, in shared editingaa-peer-selection, with the person’s name as label

A product names its own layer for each kind of thing it marks, and leaves Aa’s alone: a range given on comments replaces the threads’ marks.

Comments, a decoration layer

The commented words are drawn by the comments layer. Press them to open the thread.

What it stores

Decorations stand at the offsets they were given. When the text changes, give them again from onChange, mapped through the change. textChange(before, after) finds the smallest replacement between two versions, and mapRange(range, edit) moves a range through it, or returns null when the change took all of its words, as comments do:

import { createEditor, textChange, mapRange } from '@0001-labs/aa'let flagged: { from: number; to: number } | null = { from: 10, to: 18 }let before = textconst editor = createEditor(host, {  value: text,  onChange(after) {    if (flagged) flagged = mapRange(flagged, textChange(before, after))    before = after    editor.decorate(flagged ? [{ ...flagged, className: 'flagged' }] : [], 'flags')  },})editor.decorate([{ from: 10, to: 18, className: 'flagged' }], 'flags')

Only the blocks whose decorations changed are drawn again, and none while a composition is under way: they are drawn once it ends, so typing with an input method is never cut off.

A decoration with from equal to to is drawn as a caret: an uneditable span.aa-peer-caret carrying its className, a hairline in --aa-accent with the person’s initial in a round avatar over it, drawn from label. Shared editing draws other people’s carets this way.

editor.decorate([{ from: 42, to: 42, className: 'peer', label: 'Ann Lund' }], 'cursors')

aa.css draws aa-find-match in the highlight’s yellow, the comment classes in orange’s pastel, the carets, and aa-peer-selection in a faint fill of the accent. aa-ai-changed carries no style of its own: the product gives it one if it wants it seen.

.aa-ai-changed { background: color-mix(in srgb, var(--ds-zenith-blue) 30%, transparent); }.spelling { text-decoration: underline wavy red; }

See Styling for --aa-highlight, --aa-comment-fill and --aa-accent.

stats() returns { words, characters, blocks }:

CountWhat it counts
wordsThe words in every block but code, the Markdown’s marks set aside, counted by the language’s own word breaks where the browser has Intl.Segmenter, and by spaces otherwise.
charactersThe characters of the whole Markdown, syntax included.
blocksThe blocks.

The block menu ends with the words and characters. See Methods and events.