# Rich Text Input

> Lazy-loaded structured rich-text field for editorial workflows with formatting toolbar, undo/redo, protected blocks, and native form integration.

`rich_text_input()` provides a lazy-loaded, structured rich-text editor powered by ProseMirror. Pages that do not render a `rich_text_input` never download its editing bundle.

It connects directly to [`form()`](https://ui.praison.ai/docs/components/form.md) (including `onFormState` dirty tracking, `baseline_key`, and named `actions`), preserves focus across lazy load, and lets host apps declare immutable protected block types (such as diagrams, tables, embeds, or site directives) while keeping storage conversion in the host app.

{
  "type": "form_action",
  "action": "save_doc",
  "submit_label": "Save draft",
  "children": [
    {
      "type": "rich_text_input",
      "name": "doc",
      "label": "Page content",
      "value": {
        "type": "doc",
        "content": [
          {"type": "heading", "attrs": {"level": 2}, "content": [{"type": "text", "text": "Getting started"}]},
          {"type": "paragraph", "content": [{"type": "text", "text": "Edit headings, paragraphs, links, lists, and code blocks with full undo and redo."}]},
          {"type": "preserved", "attrs": {"id": "0", "label": "Mermaid diagram", "hint": "View in themed preview"}}
        ]
      }
    }
  ]
}

## Python example

```python
import praisonaiui as aiui

initial_doc = {
    "type": "doc",
    "content": [
        {"type": "heading", "attrs": {"level": 1}, "content": [{"type": "text", "text": "Welcome"}]},
        {"type": "paragraph", "content": [{"type": "text", "text": "Write structured content here."}]},
        {"type": "preserved", "attrs": {"id": "0", "label": "Diagram", "hint": "View in themed preview"}},
    ],
}

aiui.form(
    "page_editor",
    baseline_key="rev-1",
    children=[
        aiui.text_input("Page title", name="title", value="Welcome"),
        aiui.rich_text_input(
            "Page content",
            name="doc",
            value=initial_doc,
            preserved_types=[
                {"type": "preserved", "label": "Special content", "hint": "View in themed preview"},
                {"type": "diagram_block", "label": "Diagram", "hint": "Rendered in preview"},
            ],
            preserve_order=True,
        ),
    ],
    actions=[
        {"id": "save", "label": "Save draft", "variant": "default", "submit": True},
        {"id": "preview", "label": "Preview", "variant": "outline"},
    ],
)
```

When submitted, your handler (or `<BlockTree onAction={...} />`) receives both the sibling inputs and the parsed structured JSON document in one payload:

```json
{
  "action": "save",
  "title": "Welcome",
  "doc": {
    "type": "doc",
    "content": [
      {"type": "heading", "attrs": {"level": 1}, "content": [{"type": "text", "text": "Welcome"}]},
      {"type": "paragraph", "content": [{"type": "text", "text": "Write structured content here."}]},
      {"type": "preserved", "attrs": {"id": "0", "label": "Diagram", "hint": "View in themed preview"}}
    ]
  }
}
```

## React plugin example

Inside a React plugin using [`<BlockTree>`](https://ui.praison.ai/docs/features/plugins/browser-sdk.md):

```js
import { createElement as h, useState } from 'react'
import { BlockTree } from '@aiui/sdk'

export default function EditorialPage({ page, initial }) {
  const [dirty, setDirty] = useState(false)
  const [rev, setRev] = useState(initial.revision)

  return h(BlockTree, {
    page,
    node: {
      type: 'form_action',
      props: {
        action: 'save_page',
        baseline_key: String(rev),
        actions: [
          { id: 'save', label: 'Save draft', variant: 'default', submit: true, disabled: !dirty },
          { id: 'preview', label: 'Preview', variant: 'outline', disabled: dirty },
        ],
      },
      children: [
        { type: 'text_input', props: { name: 'title', label: 'Title', value: initial.title } },
        { type: 'rich_text_input', props: { name: 'doc', label: 'Content', value: initial.doc } },
      ],
    },
    onFormState: (state) => setDirty(state.dirty),
    onAction: async ({ body }) => {
      // body.doc is the structured ProseMirror JSON object
      await saveDraft(body)
      if (body.action === 'save') setRev((r) => r + 1)
      return true
    },
  })
}
```

## Documented JSON schema

`rich_text_input` edits a storage-agnostic ProseMirror JSON document (`{"type": "doc", "content": [...]}`):

| Category | Node / Mark | Attributes |
|---|---|---|
| **Blocks** | `paragraph`, `blockquote`, `horizontal_rule`, `bullet_list`, `list_item` | — |
| **Headings** | `heading` | `level` (`1`–`6`) |
| **Ordered lists** | `ordered_list` | `order` (default `1`) |
| **Code blocks** | `code_block` | `language` (default `""`) |
| **Protected blocks** | `preserved` + any host-defined `preserved_types` | `id`, `label`, `hint`, `data` |
| **Inline** | `text`, `hard_break`, `image` | `image`: `src`, `alt`, `title` |
| **Marks** | `strong`, `em`, `code`, `link` | `link`: `href`, `title` |

### Protected blocks

The generic `"preserved"` block type is always registered, and you can declare additional custom atom types via `preserved_types`. When `preserve_order=True` (the default), any edit, backspace, select-all, or paste transaction that would delete, reorder, or duplicate a protected block is automatically rejected so host-managed content (diagrams, tables, embeds, directives) stays intact.

## Keyboard shortcuts & security

- **Keyboard:** `Cmd/Ctrl+B` (Bold), `Cmd/Ctrl+I` (Italic), ``Cmd/Ctrl+` `` (Inline code), `Cmd/Ctrl+Z` (Undo), `Cmd/Ctrl+Shift+Z` / `Cmd/Ctrl+Y` (Redo), `Enter` (split list item), `Tab` / `Shift+Tab` (indent/outdent list item).
- **URL & paste sanitization:** Links and images allow only `http:`, `https:`, `mailto:`, `tel:`, or safe relative paths. Pasted HTML with `javascript:`, `vbscript:`, or `data:` URLs has unsafe links/images stripped automatically. Clicking a link inside the editor does not navigate away from the form.
- **Lazy loading:** ProseMirror lives in its own lazy chunk (`~78 kB` gzip) and is enforced out of the shell boot and `/chat` closures.

## Props

| Parameter | Type | Default | Description |
|---|---|---|---|
| `label` | `str` | `""` | Accessible field label |
| `name` | `str \| None` | `None` | Key submitted in enclosing `form()` payloads and `onFormState` |
| `value` | `dict \| None` | empty doc | Initial structured JSON document (`{"type": "doc", "content": [...]}`) |
| `placeholder` | `str` | `""` | Optional placeholder hint |
| `disabled` | `bool` | `False` | Disable editing and toolbar buttons |
| `read_only` | `bool` | `False` | Render as non-editable rich content |
| `preserved_types` | `list[dict \| str] \| None` | `None` | Extra host-defined protected atom block specs (`{type, label, hint}`) |
| `preserve_order` | `bool` | `True` | Prevent deleting, reordering, or duplicating protected blocks |
| `toolbar` | `list[str] \| None` | all | Optional subset of `style`, `bold`, `italic`, `code`, `bullet_list`, `ordered_list`, `blockquote`, `link`, `image`, `undo`, `redo` |
| `min_height` | `int \| None` | `None` | Optional minimum editor surface height in pixels |
| `auto_submit` | `bool` | `False` | Submit enclosing form on document changes |

## Related pages

- [Tabs](https://ui.praison.ai/docs/components/tabs.md)
- [Form](https://ui.praison.ai/docs/components/form.md)
- [Docs sign-in](https://ui.praison.ai/docs/features/docs-auth.md)
