# Rich Text Editor

## Install

::: code-group
```sh [pnpm]
pnpm add @astryx-vue/richtext
```

```sh [npm]
npm install @astryx-vue/richtext
```

```sh [yarn]
yarn add @astryx-vue/richtext
```

```sh [bun]
bun add @astryx-vue/richtext
```
:::

`RichTextEditor` is built on Lexical. Install `lexical` and the `@lexical/*` packages required by the features you use, all pinned to `0.52.0`.

## Create an editor

Import the stylesheet once in your application entry, then render `RichTextEditor`. The required `label` prop provides the editor's accessible name.

```vue
<script setup lang="ts">
import { RichTextEditor } from '@astryx-vue/richtext'
import '@astryx-vue/richtext/richtext.css'
</script>

<template>
  <RichTextEditor label="Publish content" placeholder="Write something..." />
</template>
```

Add `RichTextEditorToolbar` through the `#toolbar` slot to provide undo, block formats, inline styles, and link actions. The toolbar scrolls horizontally when space is limited.

### Editor with a toolbar

Add a formatting toolbar through the #toolbar slot.

```vue
<script setup lang="ts">
import { RichTextEditor, RichTextEditorToolbar } from '@astryx-vue/richtext'
import { seed } from './shared'
</script>

<template>
  <RichTextEditor label="Release notes" :default-value="seed" :min-height="200">
    <template #toolbar>
      <RichTextEditorToolbar />
    </template>
  </RichTextEditor>
</template>
```

## Save and display content

The editor and read-only view use the same Lexical state JSON:

1. Listen to the editor's `@change` event and save `editorState.toJSON()` to your backend.
2. To edit the content again, pass the saved JSON string to `defaultValue`. This prop is read only when the editor mounts.
3. For read-only display, pass the same string to `RichTextView` through `value` instead of rendering a read-only editor.

When you register custom nodes through `nodes`, register the same set on both the editor and the read-only view so content round-trips without data loss.

### Display saved content

Render saved Lexical state JSON with RichTextView.

```vue
<script setup lang="ts">
import { RichTextView } from '@astryx-vue/richtext'
import { seed } from './shared'
</script>

<template>
  <RichTextView :value="seed" />
</template>
```

## Keyboard commands

| Action | Keys |
| --- | --- |
| Insert a newline inside a code block | <kbd>Enter</kbd> |
| Move between table cells | Arrow keys |
| Create and move to a paragraph below a code block or table | <kbd>ArrowDown</kbd> at the block's end |
| Open or close the link dialog | <kbd>Cmd/Ctrl</kbd> + <kbd>K</kbd> |
| Leave the editor | Press <kbd>Escape</kbd>, then <kbd>Tab</kbd>; use <kbd>Shift</kbd> + <kbd>Tab</kbd> for the reverse direction |

At the end of a code block or table, <kbd>ArrowDown</kbd> inserts a paragraph when no content follows or when another code block or table immediately follows. This lets you place prose between these blocks. When ordinary content already follows, the arrow keys retain Lexical's native navigation.

## Automatic links and server-side conversion

Place `RichTextEditorAutoLinkPlugin` in the editor's default slot to turn typed URLs and email addresses into links. Use `matchers` to add custom matching rules.

### Recognize URLs and email addresses

Enable the automatic link plugin in the default slot.

```vue
<script setup lang="ts">
import { RichTextEditor, RichTextEditorAutoLinkPlugin, RichTextEditorToolbar } from '@astryx-vue/richtext'
</script>

<template>
  <RichTextEditor label="Notes" placeholder="输入网址或邮箱，会自动变成链接" :min-height="120">
    <template #toolbar>
      <RichTextEditorToolbar />
    </template>
    <RichTextEditorAutoLinkPlugin />
  </RichTextEditor>
</template>
```

To convert Markdown on the server, import `markdownToEditorStateJSON` and `editorStateJSONToMarkdown` from `@astryx-vue/richtext/markdown`. Neither function requires a browser DOM.

See the [props documentation](/en/components/rich-text-editor/props) for all properties, events, and slots.

## `` props

| Prop | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| `label` | `string` | Required | Editor label text for accessibility and screen readers. |
| `isLabelHidden` | `boolean` | `false` | Visually hides the label while keeping it accessible to screen readers. |
| `description` | `string` | - | Helper text displayed between the label and the editing area. |
| `isOptional` | `boolean` | `false` | Marks the field as optional (mutually exclusive with `isRequired`). |
| `isRequired` | `boolean` | `false` | Marks the field as required (mutually exclusive with `isOptional`). |
| `defaultValue` | `string` | - | Initial content JSON string, read only once on mount. |
| `placeholder` | `string` | - | Placeholder text shown when content is empty. |
| `isReadOnly` | `boolean` | `false` | Whether the editor is read-only. Non-editable, text stays clear, and still focusable via Tab. |
| `isDisabled` | `boolean` | `false` | Whether the editor is disabled. Non-editable, colors fade, and removed from Tab order. Takes precedence when both `isReadOnly` and `isDisabled` are true. |
| `status` | `RichTextEditorStatus` | - | Validation status `{ type, message? }`. Sets border color and displays a message below. |
| `statusVariant` | `'attached' \| 'detached' \| 'tooltip'` | `'attached'` | Display placement of the status message. |
| `width` | `number \| string` | - | Field width. Numbers are in pixels; strings are CSS lengths. |
| `minHeight` | `number \| string` | `'4.5rem'` | Minimum height of the editing area. Expands automatically with content. |
| `labelTooltip` | `string` | - | Tooltip text for the info icon next to the label. |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Editor size, affecting internal padding. |
| `nodes` | `ReadonlyArray<Klass>` | - | Additional Lexical node types to register. |
| `extensions` | `ReadonlyArray` | - | Additional Lexical extension plugin array. |
| `markdownExtensions` | `ReadonlyArray` | - | Markdown extension plugins to recognize. |
| `hasMarkdownShortcuts` | `boolean` | `true` | Whether to enable Markdown shortcuts (such as `# ` for headings). |
| `transformers` | `ReadonlyArray` | Default transformers | Custom Markdown transformation rules. |
| `hasAutoFocus` | `boolean` | `false` | Whether to automatically focus the editing area on mount. |
| `tabEscapeHint` | `string` | Localized text | Screen reader hint to press Escape then Tab to leave; pass `""` to hide. |
| `maxLength` | `number` | - | Character limit for plain text. Exceeding it shows an error style on the counter. |
| `namespace` | `string` | `'astryx-editor'` | Namespace for the underlying Lexical instance. |

- **Events**: `@change="(editorState, editor) => void"` fires when content changes, used for saving state.
- **Slots**: `#toolbar` top toolbar slot; `#default` internal plugin slot (such as placing ``).
- **Template ref methods**: `focus()` focuses; `clear()` clears; `getEditorState()` gets state; `getMarkdown()` exports Markdown; `getHTML()` exports HTML; `getEditor()` gets underlying Lexical instance.

### `` props and slots

| Prop | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| `label` | `string` | Localized text | Accessible name for the toolbar. |
| `headingLevels` | `ReadonlyArray<'h1' \| 'h2' \| 'h3'>` | `['h1', 'h2', 'h3']` | Heading levels included in the block format dropdown. |
| `size` | `'sm' \| 'md' \| 'lg'` | `'sm'` | Toolbar size. |
| `hasLink` | `boolean` | `true` | Whether to show the link button and enable Cmd/Ctrl+K shortcut. |
| `promptForUrl` | `(selectedText: string) => string \| null \| undefined` | - | Custom callback function to prompt for a URL. |
| `hasNewTabLinks` | `boolean` | `true` | Whether inserted links open in a new tab. |

- **Slots**: `#end` custom slot at the end, rendered to the right of default buttons.

### `` props, events and slots

| Prop | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| `value` | `string` | Required | Editor state JSON string to render. |
| `nodes` | `ReadonlyArray<Klass>` | - | Additional Lexical nodes to register, matching those used when saving. |
| `markdownExtensions` | `ReadonlyArray` | - | Markdown extension plugins to recognize. |
| `namespace` | `string` | `'astryx-view'` | Namespace for the underlying Lexical instance. |

- **Events**: `@parseError="(error: Error) => void"` fires when parsing content fails.
- **Slots**: `#errorFallback` fallback content when parsing fails; `#default` internal extension slot.
