docs/(guides)/static.cn.mdx
<PlateStatic> 是一个快速、只读的 React 组件,用于渲染 Plate 内容,针对服务端或 React Server Component(RSC)环境进行了优化。它避免了客户端编辑逻辑,并对节点渲染进行记忆化处理,相比在只读模式下使用 <Plate> 具有更好的性能。
它是 serializeHtml 用于 HTML 导出的核心部分,非常适合任何需要非交互式、展示性 Plate 内容视图的服务端或 RSC 场景。
_memo 和结构检查,仅重新渲染已更改的节点。<PlateStatic>启用静态渲染最快的方式是使用 BaseEditorKit,它包含预配置的基础插件,可与服务端渲染无缝配合。
import { createSlateEditor } from 'platejs';
import { PlateStatic } from 'platejs/static';
import { BaseEditorKit } from '@/components/editor/editor-base-kit';
const editor = createSlateEditor({
plugins: BaseEditorKit,
value: [
{ type: 'h1', children: [{ text: 'Server-Rendered Title' }] },
{ type: 'p', children: [{ text: 'This content is rendered statically.' }] },
],
});
// Render statically
export default function MyStaticPage() {
return <PlateStatic editor={editor} />;
}
查看完整的服务端静态渲染示例:
<ComponentSource name="slate-to-html" /> </Steps>使用 createSlateEditor 初始化 Slate 编辑器实例,配置所需的插件和组件。这类似于为交互式 <Plate> 组件使用 usePlateEditor。
import { createSlateEditor } from 'platejs';
// Import your desired base plugins (e.g., BaseHeadingPlugin, MarkdownPlugin)
// Ensure you are NOT importing from /react subpaths for server environments.
const editor = createSlateEditor({
plugins: [
// Add your list of base plugins here
// Example: BaseHeadingPlugin, MarkdownPlugin.configure({...})
],
value: [ // Example initial value
{
type: 'p',
children: [{ text: 'Hello from a static Plate editor!' }],
},
],
});
如果您的交互式编辑器使用客户端组件(例如带有 use client 或事件处理器的组件),您必须创建静态的、服务端安全的等效组件。这些组件应该渲染纯 HTML,不包含浏览器特定的逻辑。
import React from 'react';
import type { SlateElementProps } from 'platejs/static';
export function ParagraphElementStatic(props: SlateElementProps) {
return (
<SlateElement {...props}>
{props.children}
</SlateElement>
);
}
为标题、图片、链接等创建类似的静态组件。
创建一个将插件键或节点类型映射到相应静态 React 组件的对象,然后将其传递给编辑器。
import { ParagraphElementStatic } from './ui/paragraph-node-static';
import { HeadingElementStatic } from './ui/heading-node-static';
// ... import other static components
export const staticComponents = {
p: ParagraphElementStatic,
h1: HeadingElementStatic,
// ... add mappings for all your element and leaf types
};
<PlateStatic>使用 <PlateStatic> 组件,提供配置了组件的 editor 实例。
import { createSlateEditor } from 'platejs';
import { PlateStatic } from 'platejs/static';
// import { BaseHeadingPlugin, ... } from '@platejs/basic-nodes'; // etc.
import { staticComponents } from '@/components/static-components';
export default async function MyStaticPage() {
// Example: Fetch or define editor value
const initialValue = [
{ type: 'h1', children: [{ text: 'Server-Rendered Title' }] },
{ type: 'p', children: [{ text: 'Content rendered statically.' }] },
];
const editor = createSlateEditor({
plugins: [/* your base plugins */],
components: staticComponents,
value: initialValue,
});
return (
<PlateStatic
editor={editor}
style={{ padding: 16 }}
className="my-plate-static-content"
/>
);
}
<PlateStatic> 通过记忆化提升性能:
<ElementStatic> 和 <LeafStatic> 都被 React.memo 包裹。_memo 字段: 在元素或叶子上设置 node._memo = true(或任何稳定值)可以强制 Plate 跳过重新渲染该特定节点,即使其内容发生变化。这对于细粒度控制更新非常有用。PlateView对于需要最小交互性的静态内容场景,请使用 <PlateView>。该组件包裹了 <PlateStatic> 并添加了客户端事件处理器以支持用户交互,同时保持静态渲染的性能优势。
import { createStaticEditor, PlateStatic } from 'platejs/static';
import { BaseEditorKit } from '@/components/editor/editor-base-kit';
import { InteractiveViewer } from './interactive-viewer';
export default async function DocumentPage() {
const content = await fetchDocument(); // Your document data
// Server-side static editor
const editor = createStaticEditor({
plugins: BaseEditorKit,
value: content,
});
return (
<div className="grid grid-cols-2 gap-4">
<div>
<h2>Static View (Server Rendered)</h2>
<PlateStatic editor={editor} />
</div>
<div>
<h2>Interactive View</h2>
<InteractiveViewer value={content} />
</div>
</div>
);
}
'use client';
import { usePlateViewEditor } from 'platejs/react';
import { PlateView } from 'platejs/react';
import { BaseEditorKit } from '@/components/editor/editor-base-kit';
export function InteractiveViewer({ value }) {
const editor = usePlateViewEditor({
plugins: BaseEditorKit,
value,
});
return <PlateView editor={editor} />;
}
PlateView 的主要特性'use client' 指令PlateStatic 进行渲染usePlateViewEditor 配合使用:创建为仅查看 React 组件优化的静态编辑器ViewPluginPlateStatic vs. PlateView vs. Plate + readOnly| 方面 | <PlateStatic> | <PlateView> | <Plate> + readOnly |
|---|---|---|---|
| 运行环境 | 服务端/客户端(SSR/RSC 安全) | 仅客户端 | 仅客户端 |
| 交互性 | 无 | 最小(选择、复制、工具栏等) | 完整交互功能(仅浏览器) |
| 浏览器 API | 不使用 | 最小(事件处理器) | 完全使用 |
| 性能 | 最佳 - 仅静态 HTML | 良好 - 静态渲染 + 事件委托 | 较重 - 完整编辑器内部逻辑 |
| 包体积 | 最小 | 小 | 最大 |
| 使用场景 | 服务端渲染、HTML 导出 | 需要基本交互的客户端内容 | 需要所有功能的完整只读编辑器 |
| 建议 | 无任何交互的 SSR/RSC | 需要轻量交互的客户端内容 | 有复杂交互需求的客户端 |
在 Next.js App Router(或类似的 RSC 环境)中,<PlateStatic> 可以直接在服务器组件中使用:
import { createSlateEditor } from 'platejs';
import { PlateStatic } from 'platejs/static';
// Example base plugins (ensure non-/react imports)
// import { BaseHeadingPlugin } from '@platejs/basic-nodes';
import { staticComponents } from '@/components/static-components'; // Your static components mapping
export default async function Page() {
// Fetch or define content server-side
const serverContent = [
{ type: 'h1', children: [{ text: 'Rendered on the Server! 🎉' }] },
{ type: 'p', children: [{ text: 'This content is static and server-rendered.' }] },
];
const editor = createSlateEditor({
// plugins: [BaseHeadingPlugin, /* ...other base plugins */],
plugins: [], // Add your base plugins
components: staticComponents,
value: serverContent,
});
return (
<PlateStatic
editor={editor}
className="my-static-preview-container"
/>
);
}
这会在服务器上将内容渲染为 HTML,PlateStatic 本身不需要客户端 JavaScript 包。
serializeHtml 配合使用要生成完整的 HTML 字符串(例如用于电子邮件、PDF 或外部系统),请使用 serializeHtml。它内部使用了 <PlateStatic>。
import { createSlateEditor } from 'platejs';
import { serializeHtml } from 'platejs/static';
import { staticComponents } from '@/components/static-components';
// import { BaseHeadingPlugin, ... } from '@platejs/basic-nodes';
async function getDocumentAsHtml(value: any[]) {
const editor = createSlateEditor({
plugins: [/* ...your base plugins... */],
components: staticComponents,
value,
});
const html = await serializeHtml(editor, {
// editorComponent: PlateStatic, // Optional: Defaults to PlateStatic
props: { className: 'prose max-w-none' }, // Example: Pass props to the root div
});
return html;
}
// Example Usage:
// const mySlateValue = [ { type: 'h1', children: [{ text: 'My Document' }] } ];
// getDocumentAsHtml(mySlateValue).then(console.log);
更多详情请参阅 HTML 序列化指南。
<PlateStatic> Propsimport type React from 'react';
import type { Descendant } from 'slate';
import type { PlateEditor } from 'platejs/core'; // Adjust imports as per your setup
interface PlateStaticProps extends React.HTMLAttributes<HTMLDivElement> {
/**
* The Plate editor instance, created via `createSlateEditor`.
* Must include plugins and components relevant to the content being rendered.
*/
editor: PlateEditor;
/**
* Optional Plate `Value` (array of `Descendant` nodes).
* If provided, this will be used for rendering instead of `editor.children`.
*/
value?: Descendant[];
/** Inline CSS styles for the root `div` element. */
style?: React.CSSProperties;
// Other HTMLDivElement attributes like `className`, `id`, etc., are also supported.
}
editor:使用 createSlateEditor 创建的 PlateEditor 实例,包含组件配置。value:可选。如果提供,将渲染此 Descendant 节点数组,覆盖 editor.children 中的当前内容。