---
url: /reference/interface/capabilities/richtext.md
---

# richtext

`richtext` 通过 iframe 方式嵌入富文本内容，提供富文本渲染器和编辑器的创建能力。

导入：

```typescript
import { richtext } from '@pc-nexus/capabilities';
```

内置方法：

## createRenderer

`createRenderer` 创建富文本渲染器。

### 函数签名

```typescript
function createRenderer(iframe: HTMLIFrameElement, options?: RichtextRendererOptions): Promise<RichtextRendererRef>;

interface RichtextRendererOptions {
    content?: RichtextContent;
}

interface RichtextRendererRef {
    readonly iframe: HTMLIFrameElement;
    readonly update: (content: RichtextContent) => void;
}

type RichtextContent = RichtextDescendant[];

type RichtextDescendant = RichtextElement;

interface RichtextText {
    text: string;
    [key: string]: unknown;
}

interface RichtextElement {
    type: string;
    children: (RichtextDescendant | RichtextText)[];
    [key: string]: unknown;
}
```

### 参数

`RichtextRendererOptions` 类型定义如下：

### 返回值

返回值类型为 `RichtextRendererRef` ，表示渲染器的引用对象：

### 示例

```typescript
import { richtext, type RichtextContent } from "@pc-nexus/capabilities";

const content: RichtextContent = [
  {
    type: "paragraph",
    children: [{ text: "Hello, this is a sample richtext document." }],
  },
];

const iframe = document.getElementById("richtext-renderer") as HTMLIFrameElement;

const rendererRef = await richtext.createRenderer(iframe, { content });

// 更新渲染内容
rendererRef.update(newContent);
```

## createEditor

`createEditor` 创建富文本编辑器。

### 函数签名

```typescript
function createEditor(iframe: HTMLIFrameElement, options?: RichtextEditorOptions): Promise<RichtextEditorRef>;
  
interface RichtextEditorOptions extends RichtextRendererOptions {
    onContentChange?: (content: RichtextContent) => void;
}

interface RichtextEditorRef extends RichtextRendererRef {
    destroy: () => void;
}

```

### 参数

`RichtextEditorOptions` 类型定义如下：

### 返回值

返回值类型为 `RichtextEditorRef` ，表示编辑器的引用对象：

### 示例

```typescript
import { richtext, type RichtextContent } from "@pc-nexus/capabilities";

let content: RichtextContent = [
  {
    type: "paragraph",
    children: [{ text: "Editable document" }],
  },
];

const editorIframe = document.getElementById("richtext-editor") as HTMLIFrameElement;

const editorRef = await richtext.createEditor(editorIframe, {
  content,
  onContentChange: (doc) => {
    content = doc ?? [];
  },
});

// 组件卸载或页面离开时销毁编辑器
editorRef.destroy();
```

## toHtml

`toHtml` 转换为 HTML 格式的文本内容。

### 函数签名

```typescript
function toHtml(content: RichtextContent): Promise<string>
```

### 参数

### 返回值

返回 `Promise<string>` 。

### 示例

```typescript
import { richtext, type RichtextContent } from "@pc-nexus/capabilities";

const content: RichtextContent = [
  {
    type: "paragraph",
    children: [{ text: "Hello, this is a sample richtext document." }],
  },
];

richtext.toHtml(content);
// <p>Hello, this is a sample richtext document.</p>


```

## toPlainText

`toPlainText` 转换为纯文本内容。

### 函数签名

```typescript
function toPlainText(content: RichtextContent): Promise<string>
```

### 参数

### 返回值

返回 `Promise<string>` 。

### 示例

```typescript
import { richtext, type RichtextContent } from "@pc-nexus/capabilities";

const content: RichtextContent = [
  {
    type: "paragraph",
    children: [{ text: "Hello, this is a sample richtext document." }],
  },
];

richtext.toPlainText(content);
// Hello, this is a sample richtext document.

```
