richtext
richtext 通过 iframe 方式嵌入富文本内容,提供富文本渲染器和编辑器的创建能力。
导入:
typescript
import { richtext } from '@pc-nexus/capabilities';内置方法:
| API | 描述 |
|---|---|
createRenderer | 用于创建富文本渲染器 |
createEditor | 用于创建富文本编辑器 |
toHtml | 将富文本内容转换为 HTML 字符串 |
toPlainText | 将富文本内容转换为纯文本字符串 |
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;
}参数
| 名称 | 描述 |
|---|---|
iframe | 用于承载富文本内容的 iframe 元素 |
options | 初始化选项,类型为 RichtextRendererOptions (见下方详细说明) |
RichtextRendererOptions 类型定义如下:
| 名称 | 描述 |
|---|---|
content | 初始渲染内容 |
返回值
返回值类型为 RichtextRendererRef ,表示渲染器的引用对象:
| 名称 | 描述 |
|---|---|
iframe | 渲染器所在的 iframe 元素 |
update | 更新渲染内容的方法 |
示例
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;
}参数
| 名称 | 描述 |
|---|---|
iframe | 用于承载富文本编辑器的 iframe 元素 |
options | 初始化选项,类型为 RichtextEditorOptions (见下方详细说明) |
RichtextEditorOptions 类型定义如下:
| 名称 | 描述 |
|---|---|
content | 初始编辑内容 |
onContentChange | 内容变更回调函数 |
返回值
返回值类型为 RichtextEditorRef ,表示编辑器的引用对象:
| 名称 | 描述 |
|---|---|
iframe | 编辑器所在的 iframe 元素 |
update | 更新编辑内容的方法 |
destroy | 销毁编辑器实例,清理资源 |
示例
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>参数
| 名称 | 描述 |
|---|---|
| content | 富文本内容 |
返回值
返回 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>参数
| 名称 | 描述 |
|---|---|
| content | 富文本内容 |
返回值
返回 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.