# 富文本编辑器

## 安装

::: 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` 基于 Lexical。你还需要根据所用功能自行安装 `lexical` 和相应的 `@lexical/*` 子包，版本统一为 `0.52.0`。

## 创建编辑器

在应用入口引入一次样式，然后渲染 `RichTextEditor`。`label` 是必填属性，用于标识编辑器的无障碍名称。

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

<template>
  <RichTextEditor label="发布内容" placeholder="请输入正文..." />
</template>
```

通过 `#toolbar` 插槽加入 `RichTextEditorToolbar`，即可获得撤销、块格式、行内样式和链接操作。工具栏空间不足时会横向滚动。

### 带工具栏的编辑器

使用 #toolbar 插槽添加格式工具栏。

```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>
```

## 保存并展示内容

编辑器和只读视图使用同一份 Lexical 状态 JSON：

1. 监听编辑器的 `@change`，将 `editorState.toJSON()` 保存到后端。
2. 再次编辑时，把保存的 JSON 字符串传给 `defaultValue`。该属性只在挂载时读取。
3. 只读展示时，把同一个字符串传给 `RichTextView` 的 `value`，不要再渲染一个只读编辑器。

如果通过 `nodes` 注册自定义节点，编辑器和只读视图必须注册同一组节点，否则内容无法完整往返。

### 只读展示已保存的内容

将保存的 Lexical 状态 JSON 交给 RichTextView 进行只读渲染。

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

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

## 键盘操作

| 操作 | 按键 |
| --- | --- |
| 在代码块中换行 | <kbd>Enter</kbd> |
| 在表格单元格之间移动 | 方向键 |
| 在代码块或表格末尾创建并进入下方段落 | <kbd>ArrowDown</kbd> |
| 打开或关闭链接对话框 | <kbd>Cmd/Ctrl</kbd> + <kbd>K</kbd> |
| 离开整个编辑器 | 先按 <kbd>Escape</kbd>，再按 <kbd>Tab</kbd>；反向移动使用 <kbd>Shift</kbd> + <kbd>Tab</kbd> |

光标位于代码块或表格末尾时，<kbd>ArrowDown</kbd> 会在下方没有内容，或者下方紧邻另一个代码块或表格时插入段落。这样可以在这两类块之间补充正文；下方已经是普通内容时，方向键沿用 Lexical 的原生导航。

## 自动链接与服务端转换

将 `RichTextEditorAutoLinkPlugin` 放入编辑器默认插槽，可以把输入的网址和邮箱自动转换为链接；通过 `matchers` 可以扩展匹配规则。

### 自动识别网址和邮箱

在默认插槽中启用自动链接插件。

```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>
```

需要在服务端读写 Markdown 时，从 `@astryx-vue/richtext/markdown` 导入 `markdownToEditorStateJSON` 和 `editorStateJSONToMarkdown`。这两个函数不依赖浏览器 DOM。

完整的属性、事件和插槽见[属性文档](/components/rich-text-editor/props)。

## `` 属性

| 属性名 | 类型 | 默认值 | 说明 |
| :--- | :--- | :--- | :--- |
| `label` | `string` | 必填 | 编辑器标签文字，供无障碍读屏使用。 |
| `isLabelHidden` | `boolean` | `false` | 视觉上隐藏标签，读屏软件仍可读取。 |
| `description` | `string` | - | 标签与编辑区之间的说明文字。 |
| `isOptional` | `boolean` | `false` | 标记为选填（与 `isRequired` 互斥）。 |
| `isRequired` | `boolean` | `false` | 标记为必填（与 `isOptional` 互斥）。 |
| `defaultValue` | `string` | - | 初始内容 JSON 字符串，仅在挂载时读取一次。 |
| `placeholder` | `string` | - | 内容为空时的提示文字。 |
| `isReadOnly` | `boolean` | `false` | 是否只读。不可编辑但文字清晰，仍可通过 Tab 键聚焦。 |
| `isDisabled` | `boolean` | `false` | 是否禁用。不可编辑、颜色变淡并移出 Tab 顺序；与 `isReadOnly` 均设为 true 时以禁用为准。 |
| `status` | `RichTextEditorStatus` | - | 校验状态 `{ type, message? }`，决定边框颜色并在下方提示。 |
| `statusVariant` | `'attached' \| 'detached' \| 'tooltip'` | `'attached'` | 状态提示的呈现位置。 |
| `width` | `number \| string` | - | 字段宽度，数字为像素，字符串为 CSS 长度。 |
| `minHeight` | `number \| string` | `'4.5rem'` | 编辑区最小高度，内容超出自动增高。 |
| `labelTooltip` | `string` | - | 标签右侧提示图标中的文字。 |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | 编辑器尺寸，影响内部内边距。 |
| `nodes` | `ReadonlyArray<Klass>` | - | 额外注册的 Lexical 节点类型。 |
| `extensions` | `ReadonlyArray` | - | 额外的 Lexical 扩展插件数组。 |
| `markdownExtensions` | `ReadonlyArray` | - | 要识别的 Markdown 插件扩展数组。 |
| `hasMarkdownShortcuts` | `boolean` | `true` | 是否开启 Markdown 快捷输入（如 `# ` 变标题）。 |
| `transformers` | `ReadonlyArray` | 默认转换器 | 自定义 Markdown 转换规则。 |
| `hasAutoFocus` | `boolean` | `false` | 挂载后是否自动聚焦编辑区。 |
| `tabEscapeHint` | `string` | 本地化文案 | 读屏提示：按 Escape 再按 Tab 移出焦点；传 `""` 可隐藏。 |
| `maxLength` | `number` | - | 纯文字字数上限，超出后计数显示错误样式。 |
| `namespace` | `string` | `'astryx-editor'` | 底层 Lexical 实例命名空间。 |

- **事件**：`@change="(editorState, editor) => void"` 在内容改变时触发，用于保存状态。
- **插槽**：`#toolbar` 顶部工具栏插槽；`#default` 内部插件插槽（如放置 ``）。
- **模板引用方法**：`focus()` 聚焦；`clear()` 清空；`getEditorState()` 获取状态；`getMarkdown()` 导出 Markdown；`getHTML()` 导出 HTML；`getEditor()` 获取底层 Lexical 实例。

### `` 属性与插槽

| 属性名 | 类型 | 默认值 | 说明 |
| :--- | :--- | :--- | :--- |
| `label` | `string` | 本地化文案 | 工具栏无障碍名称。 |
| `headingLevels` | `ReadonlyArray<'h1' \| 'h2' \| 'h3'>` | `['h1', 'h2', 'h3']` | 块格式下拉中包含的标题级别。 |
| `size` | `'sm' \| 'md' \| 'lg'` | `'sm'` | 工具栏尺寸。 |
| `hasLink` | `boolean` | `true` | 是否显示链接按钮及启用 Cmd/Ctrl+K 快捷键。 |
| `promptForUrl` | `(selectedText: string) => string \| null \| undefined` | - | 自定义获取网址的回调函数。 |
| `hasNewTabLinks` | `boolean` | `true` | 插入的链接是否在新标签页打开。 |

- **插槽**：`#end` 尾部自定义插槽，渲染在默认按钮右侧。

### `` 属性、事件与插槽

| 属性名 | 类型 | 默认值 | 说明 |
| :--- | :--- | :--- | :--- |
| `value` | `string` | 必填 | 要渲染的编辑器状态 JSON 字符串。 |
| `nodes` | `ReadonlyArray<Klass>` | - | 额外注册的 Lexical 节点，需与写入时一致。 |
| `markdownExtensions` | `ReadonlyArray` | - | 要识别的 Markdown 插件扩展数组。 |
| `namespace` | `string` | `'astryx-view'` | 底层 Lexical 实例命名空间。 |

- **事件**：`@parseError="(error: Error) => void"` 在内容解析失败时触发。
- **插槽**：`#errorFallback` 解析失败时的降级显示内容；`#default` 内部扩展插槽。
