Rich text / foldkit-richtext-dom

Embed the low-level interpreter directly

To host the editable subtree outside the Bundle, call mount and attach (or mountInto / releaseMount from foldkit-richtext-dom/host):

ts
import { mount } from 'foldkit-richtext-dom'
import { attach } from 'foldkit-richtext-dom/events'
import * as RichText from 'foldkit-richtext'

function mountRawEditor(
  host: HTMLElement,
  content: RichText.Document,
  onCommand: (command: RichText.Command) => void,
) {
  let dom = mount(host.ownerDocument, content)
  host.append(dom.root)

  const attachment = attach(dom, { onIntent: onCommand })
  return {
    sync: (next: RichText.EditorState, changeSet: RichText.ChangeSet) => {
      attachment.sync(next, changeSet)
      dom = attachment.current()
    },
    detach: () => attachment.detach(),
  }
}
  • mount / adopt / patch / repair: Build, reuse server-rendered, incrementally update, or recover the DOM subtree indexed by data-block and data-run.
  • attach: Intercepts beforeinput, keydown, composition, copy, cut, and paste so the browser cannot mutate content directly, queues state patches during IME composition, paints span[data-decoration="selection"] when focus moves to external controls, and scrolls the caret into view honoring scroll-padding.
  • positionToRange / rangeToPosition: Translate between semantic Position values and DOM Range objects.
  • parseHtml(html, { kit, mint }) (foldkit-richtext-dom/html): Sanitizes pasted HTML through a strict whitelist and safeUrl check.

Current behavior and limits

  • Intent reporting only: attach reports user intent and waits for the caller to run commands and pass back the new state and ChangeSet.
  • Container re-rendering: When a container's list of child items changes, the container re-renders in place rather than diffing each child item individually.
  • Single host per ID: Each hostId identifies one editor on a page, so repeating rows inside a single form cannot yet host separate richTextInput editors.