Skip to content

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.

举报邮箱:abuse@worktile.com