Rich text / foldkit-richtext-dom

Position toolbars, block handles, and menus

The editor renders only its host element. foldkit-richtext-dom/toolbar provides plain views and Mount positioning helpers for surrounding chrome, while foldkit-mixins-richtext provides fully restyleable Slot versions.

Marks and floating toolbars

marksToolbar({ state, toMessage, marks? }) renders buttons for the standard marks (or a custom list) using markActive(state, mark) to reflect caret/selection state. Pair it with selectionAnchor to float over a text range (assuming model holds { document, editor } and h is Foldkit's HtmlBuilder):

ts
import * as RichText from 'foldkit-richtext'
import { marksToolbar, selectionAnchor } from 'foldkit-richtext-dom/toolbar'

RichText.coversText(model.editor.selection)
  ? h.div(
      [h.OnMount(selectionAnchor({ hostId: 'body', gap: 8 }))],
      [
        marksToolbar({
          state: { ...model.editor, document: model.document },
          toMessage: mark => edited(Message.ToggledMark({ mark })),
        })(h),
      ],
    )
  : h.empty

selectionAnchor writes position: fixed, top, left, and data-placement (top or bottom) whenever the selection, scroll, or viewport changes. Pass caret: true to anchor below a collapsed caret (for example, for a slash menu).

Anchor and drag blocks

  • blockAnchor({ hostId, node, gap, side? }) positions an element beside block node (defaulting to the left edge, or 'above' / 'inside-end' for block toolbars), updating on DOM patches, scrolls, resizes, and element size changes.
  • blockDrag({ hostId, node }) makes the mounted element drag block node to any target allowed by RichText.moveTargets. It marks [data-richtext-drop] at the drop edge, sets data-richtext-dragging on the dragged block, auto-scrolls within 48 px of the viewport edge, cancels cleanly on Escape or cancelled pointers, and dispatches MovedBlock on release.

Because Mounts read arguments once on insertion, key anchored elements by h.Key(node) whenever node can change.

Slash menu helpers

foldkit-richtext-dom/editor exports slashEntries, slashQuery, and slashMenu so your menu view and the Bundle's Enter handler agree on matches and order:

ts
import { slashEntries, slashMenu, slashQuery } from 'foldkit-richtext-dom/editor'

slashQuery('see /head') // 'head'
slashMenu(slashEntries, 'see /head', 0)?.highlighted?.label // 'Heading 1'

menuTextOf(view) from foldkit-richtext-dom/editor-bundle reads the active query text while respecting Message.DismissedSlash (Escape), keeping the menu closed until a new / query begins.