本指南将逐步引导您在 Next.js 应用中构建 Plate 编辑器。
创建第一个编辑器
首先在项目中添加核心的 Editor 组件:
pnpm dlx shadcn@latest add https://platejs.org/r/editor
接下来创建一个基础编辑器页面。这个示例在 EditorContainer
中设置了一个简单编辑器。
'use client';
import { Plate, usePlateEditor } from 'platejs/react';
import { Editor, EditorContainer } from '@/components/ui/editor';
export default function MyEditorPage() {
const editor = usePlateEditor(); // 初始化编辑器实例
return (
<Plate editor={editor}> {/* 提供编辑器上下文 */}
<EditorContainer> {/* 设置编辑器区域样式 */}
<Editor placeholder="输入您精彩的内容..." />
</EditorContainer>
</Plate>
);
}
usePlateEditor
会创建一个记忆化的编辑器实例,确保在重新渲染时保持稳定。如需非记忆化版本,请使用 createPlateEditor
。
Loading...
添加基础标记
通过文本格式化增强编辑器功能。添加 基础节点套件、FixedToolbar 和 MarkToolbarButton 组件:
pnpm dlx shadcn@latest add https://platejs.org/r/basic-nodes-kit https://platejs.org/r/fixed-toolbar https://platejs.org/r/mark-toolbar-button
basic-nodes-kit
包含所有基础插件(加粗、斜体、下划线、标题、引用块等)及其组件,我们将在后续步骤中使用。
更新编辑器页面以包含这些组件和基础标记插件。 此示例添加了加粗、斜体和下划线功能。
'use client';
import * as React from 'react';
import type { Value } from 'platejs';
import {
BoldPlugin,
ItalicPlugin,
UnderlinePlugin,
} from '@platejs/basic-nodes/react';
import {
Plate,
usePlateEditor,
} from 'platejs/react';
import { Editor, EditorContainer } from '@/components/ui/editor';
import { FixedToolbar } from '@/components/ui/fixed-toolbar';
import { MarkToolbarButton } from '@/components/ui/mark-toolbar-button';
const initialValue: Value = [
{
type: 'p',
children: [
{ text: '你好!试试' },
{ text: '加粗', bold: true },
{ text: '、' },
{ text: '斜体', italic: true },
{ text: '和' },
{ text: '下划线', underline: true },
{ text: '格式。' },
],
},
];
export default function MyEditorPage() {
const editor = usePlateEditor({
plugins: [BoldPlugin, ItalicPlugin, UnderlinePlugin], // 添加标记插件
value: initialValue, // 设置初始内容
});
return (
<Plate editor={editor}>
<FixedToolbar className="justify-start rounded-t-lg">
<MarkToolbarButton nodeType="bold" tooltip="加粗 (⌘+B)">B</MarkToolbarButton>
<MarkToolbarButton nodeType="italic" tooltip="斜体 (⌘+I)">I</MarkToolbarButton>
<MarkToolbarButton nodeType="underline" tooltip="下划线 (⌘+U)">U</MarkToolbarButton>
</FixedToolbar>
<EditorContainer>
<Editor placeholder="输入您精彩的内容..." />
</EditorContainer>
</Plate>
);
}
Loading...
添加基础元素
通过自定义组件引入块级元素如标题和引用块。
'use client';
import * as React from 'react';
import type { Value } from 'platejs';
import {
BlockquotePlugin,
BoldPlugin,
H1Plugin,
H2Plugin,
H3Plugin,
ItalicPlugin,
UnderlinePlugin,
} from '@platejs/basic-nodes/react';
import {
Plate,
usePlateEditor,
} from 'platejs/react';
import { BlockquoteElement } from '@/components/ui/blockquote-node';
import { Editor, EditorContainer } from '@/components/ui/editor';
import { FixedToolbar } from '@/components/ui/fixed-toolbar';
import { H1Element, H2Element, H3Element } from '@/components/ui/heading-node';
import { MarkToolbarButton } from '@/components/ui/mark-toolbar-button';
import { ToolbarButton } from '@/components/ui/toolbar'; // 通用工具栏按钮
const initialValue: Value = [
{
children: [{ text: '标题' }],
type: 'h3',
},
{
children: [{ text: '这是一段引用。' }],
type: 'blockquote',
},
{
children: [
{ text: '包含一些' },
{ bold: true, text: '加粗' },
{ text: '文字用于强调!' },
],
type: 'p',
},
];
export default function MyEditorPage() {
const editor = usePlateEditor({
plugins: [
BoldPlugin,
ItalicPlugin,
UnderlinePlugin,
H1Plugin.withComponent(H1Element),
H2Plugin.withComponent(H2Element),
H3Plugin.withComponent(H3Element),
BlockquotePlugin.withComponent(BlockquoteElement),
],
value: initialValue,
});
return (
<Plate editor={editor}>
<FixedToolbar className="flex justify-start gap-1 rounded-t-lg">
{/* 元素工具栏按钮 */}
<ToolbarButton onClick={() => editor.tf.h1.toggle()}>H1</ToolbarButton>
<ToolbarButton onClick={() => editor.tf.h2.toggle()}>H2</ToolbarButton>
<ToolbarButton onClick={() => editor.tf.h3.toggle()}>H3</ToolbarButton>
<ToolbarButton onClick={() => editor.tf.blockquote.toggle()}>引用</ToolbarButton>
{/* 标记工具栏按钮 */}
<MarkToolbarButton nodeType="bold" tooltip="加粗 (⌘+B)">B</MarkToolbarButton>
<MarkToolbarButton nodeType="italic" tooltip="斜体 (⌘+I)">I</MarkToolbarButton>
<MarkToolbarButton nodeType="underline" tooltip="下划线 (⌘+U)">U</MarkToolbarButton>
</FixedToolbar>
<EditorContainer>
<Editor placeholder="输入您精彩的内容..." />
</EditorContainer>
</Plate>
);
}
Loading...
组件注册
注意我们如何使用 Plugin.withComponent(Component)
将组件与其对应的插件注册。这是将 React 组件与 Plate 插件关联的推荐方式。
如需快速开始使用预配置的常用插件和组件,可使用 editor-basic
区块:
pnpm dlx shadcn@latest add https://platejs.org/r/editor-basic
这会为您处理大部分样板代码。
处理编辑器值
为了使编辑器内容持久化,我们集成 localStorage
来在客户端保存和加载编辑器值。
'use client';
import * as React from 'react';
import type { Value } from 'platejs';
import {
BlockquotePlugin,
BoldPlugin,
H1Plugin,
H2Plugin,
H3Plugin,
ItalicPlugin,
UnderlinePlugin,
} from '@platejs/basic-nodes/react';
import {
Plate,
usePlateEditor,
} from 'platejs/react';
import { BlockquoteElement } from '@/components/ui/blockquote-node';
import { Editor, EditorContainer } from '@/components/ui/editor';
import { FixedToolbar } from '@/components/ui/fixed-toolbar';
import { H1Element, H2Element, H3Element } from '@/components/ui/heading-node';
import { MarkToolbarButton } from '@/components/ui/mark-toolbar-button';
import { ToolbarButton } from '@/components/ui/toolbar';
const initialValue: Value = [
{
children: [{ text: '标题' }],
type: 'h3',
},
{
children: [{ text: '这是一段引用。' }],
type: 'blockquote',
},
{
children: [
{ text: '包含一些' },
{ bold: true, text: '加粗' },
{ text: '文字用于强调!' },
],
type: 'p',
},
];
export default function MyEditorPage() {
const editor = usePlateEditor({
plugins: [
BoldPlugin,
ItalicPlugin,
UnderlinePlugin,
H1Plugin.withComponent(H1Element),
H2Plugin.withComponent(H2Element),
H3Plugin.withComponent(H3Element),
BlockquotePlugin.withComponent(BlockquoteElement),
],
value: () => {
if (typeof window !== 'undefined') {
const savedValue = localStorage.getItem('installation-next-demo');
if (savedValue) {
return JSON.parse(savedValue);
}
}
return initialValue;
},
});
// 防止 SSR 渲染编辑器
if (typeof window === 'undefined') {
return null;
}
return (
<Plate
editor={editor}
onChange={({ value }) => {
localStorage.setItem('installation-next-demo', JSON.stringify(value));
}}
>
<FixedToolbar className="flex justify-start gap-1 rounded-t-lg">
<ToolbarButton onClick={() => editor.tf.h1.toggle()}>H1</ToolbarButton>
<ToolbarButton onClick={() => editor.tf.h2.toggle()}>H2</ToolbarButton>
<ToolbarButton onClick={() => editor.tf.h3.toggle()}>H3</ToolbarButton>
<ToolbarButton onClick={() => editor.tf.blockquote.toggle()}>引用</ToolbarButton>
<MarkToolbarButton nodeType="bold" tooltip="加粗 (⌘+B)">B</MarkToolbarButton>
<MarkToolbarButton nodeType="italic" tooltip="斜体 (⌘+I)">I</MarkToolbarButton>
<MarkToolbarButton nodeType="underline" tooltip="下划线 (⌘+U)">U</MarkToolbarButton>
<div className="flex-1" />
<ToolbarButton
className="px-2"
onClick={() => editor.tf.setValue(initialValue)}
>
重置
</ToolbarButton>
</FixedToolbar>
<EditorContainer>
<Editor placeholder="输入您精彩的内容..." />
</EditorContainer>
</Plate>
);
}
Loading...
后续步骤
恭喜!您已在 Next.js 中构建了一个基础的 Plate 编辑器。
要进一步增强编辑器: