Plate快速入门:5分钟搭建专业编辑器
2026-02-04 04:50:58作者:仰钰奇
还在为React富文本编辑器而烦恼吗?想要快速搭建一个功能强大、界面美观的编辑器却不知从何下手?Plate(PlateJS)为你提供了完美的解决方案!本文将带你用5分钟时间,从零开始搭建一个专业的富文本编辑器。
什么是Plate?
Plate是一个基于Slate.js构建的现代化React富文本编辑器框架。它提供了:
- 🚀 开箱即用的插件系统 - 无需复杂配置
- 🎨 美观的UI组件 - 基于shadcn/ui设计
- 🔧 模块化架构 - 按需引入功能
- 📦 TypeScript支持 - 完整的类型安全
环境准备
在开始之前,确保你的系统已安装:
- Node.js 18+
- npm、yarn或pnpm包管理器
- React 18+ 项目
5分钟快速开始
步骤1:创建React项目
如果你还没有React项目,先创建一个:
# 使用Vite创建React项目
npm create vite@latest my-editor-app -- --template react-ts
cd my-editor-app
npm install
步骤2:安装Plate UI
Plate推荐使用CLI方式快速安装:
# 安装Plate UI核心依赖
npx shadcn@latest add https://platejs.org/r/plate-ui
步骤3:安装基础编辑器组件
# 安装编辑器核心组件
npx shadcn@latest add https://platejs.org/r/editor
npx shadcn@latest add https://platejs.org/r/editor-container
步骤4:创建你的第一个编辑器
在src/App.tsx中创建基础编辑器:
import { Plate, usePlateEditor } from 'platejs/react';
import { Editor, EditorContainer } from '@/components/ui/editor';
export default function App() {
const editor = usePlateEditor();
return (
<Plate editor={editor}>
<EditorContainer>
<Editor placeholder="开始输入你的内容..." />
</EditorContainer>
</Plate>
);
}
步骤5:运行项目
npm run dev
恭喜!你已经成功创建了一个基础的Plate编辑器。现在打开浏览器访问http://localhost:5173,就能看到你的编辑器了。
添加常用功能
文本格式化功能
让我们为编辑器添加粗体、斜体、下划线等基本格式功能:
# 安装基础节点套件和工具栏组件
npx shadcn@latest add https://platejs.org/r/basic-nodes-kit
npx shadcn@latest add https://platejs.org/r/fixed-toolbar
npx shadcn@latest add https://platejs.org/r/mark-toolbar-button
更新你的编辑器代码:
import * as React from 'react';
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';
export default function App() {
const editor = usePlateEditor({
plugins: [BoldPlugin, ItalicPlugin, UnderlinePlugin],
});
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>
);
}
标题和引用功能
添加标题和引用块功能:
# 安装标题和引用组件
npx shadcn@latest add https://platejs.org/r/heading-node
npx shadcn@latest add https://platejs.org/r/blockquote-node
npx shadcn@latest add https://platejs.org/r/toolbar-button
更新代码添加标题和引用功能:
import * as React from 'react';
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';
export default function App() {
const editor = usePlateEditor({
plugins: [
BoldPlugin,
ItalicPlugin,
UnderlinePlugin,
H1Plugin.withComponent(H1Element),
H2Plugin.withComponent(H2Element),
H3Plugin.withComponent(H3Element),
BlockquotePlugin.withComponent(BlockquoteElement),
],
});
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>
);
}
功能对比表
下表展示了Plate与其他流行编辑器的功能对比:
| 功能特性 | Plate | Slate | Draft.js | Quill |
|---|---|---|---|---|
| React集成 | ✅ 原生支持 | ✅ 需要适配 | ✅ 原生支持 | ⚠️ 需要包装 |
| TypeScript | ✅ 完整支持 | ✅ 支持 | ⚠️ 部分支持 | ❌ 不支持 |
| 插件系统 | ✅ 模块化 | ✅ 灵活 | ❌ 有限 | ✅ 丰富 |
| 现代化UI | ✅ 基于shadcn | ❌ 需要自定义 | ❌ 需要自定义 | ✅ 内置 |
| 学习曲线 | ⭐⭐ 简单 | ⭐⭐⭐⭐ 复杂 | ⭐⭐⭐ 中等 | ⭐⭐ 简单 |
| 社区生态 | ✅ 活跃 | ✅ 活跃 | ⚠️ 维护中 | ✅ 活跃 |
进阶功能配置
数据持久化
添加localStorage数据持久化功能:
import * as React from 'react';
import type { Value } from 'platejs';
// ... 其他导入
const initialValue: Value = [
{
type: 'p',
children: [{ text: '欢迎使用Plate编辑器!' }],
},
];
export default function App() {
const editor = usePlateEditor({
plugins: [
// ... 插件配置
],
value: () => {
const savedValue = localStorage.getItem('plate-editor-content');
return savedValue ? JSON.parse(savedValue) : initialValue;
},
});
return (
<Plate
editor={editor}
onChange={({ value }) => {
localStorage.setItem('plate-editor-content', JSON.stringify(value));
}}
>
{/* ... 工具栏和编辑器 */}
</Plate>
);
}
自定义主题配置
Plate支持自定义CSS变量来调整主题:
/* 在全局CSS文件中添加 */
:root {
--brand: oklch(0.623 0.214 259.815);
--editor-border: 1px solid #e5e7eb;
--editor-radius: 0.5rem;
}
.dark {
--brand: oklch(0.707 0.165 254.624);
--editor-border: 1px solid #374151;
}
常见问题解答
Q: Plate支持哪些浏览器?
A: Plate支持所有现代浏览器,包括Chrome、Firefox、Safari和Edge。
Q: 如何添加图片上传功能?
A: 使用媒体插件:
npx shadcn@latest add https://platejs.org/r/media-kit
Q: 是否支持协同编辑?
A: 是的,Plate支持通过Yjs实现实时协同编辑。
Q: 如何自定义工具栏布局?
A: 工具栏使用Flex布局,可以通过className自定义样式和排列方式。
开发路线图
flowchart TD
A[基础编辑器搭建] --> B[文本格式化功能]
B --> C[块级元素支持]
C --> D[媒体内容处理]
D --> E[高级功能扩展]
E --> F[生产环境优化]
subgraph 高级功能
E1[表格功能]
E2[代码块高亮]
E3[数学公式]
E4[AI集成]
end
F --> G[性能优化]
G --> H[无障碍访问]
H --> I[移动端适配]
总结
通过本文的5分钟快速入门指南,你已经掌握了:
- 环境搭建 - 创建React项目并安装Plate
- 基础编辑器 - 创建第一个Plate编辑器实例
- 功能扩展 - 添加文本格式化和块级元素
- 数据管理 - 实现编辑内容持久化
- 主题定制 - 了解如何自定义编辑器样式
Plate作为一个现代化的React富文本编辑器框架,提供了出色的开发体验和丰富的功能扩展能力。无论是简单的文本编辑还是复杂的内容管理系统,Plate都能满足你的需求。
现在就开始你的Plate编辑器之旅吧!如果有任何问题,记得查阅官方文档或加入社区讨论。
登录后查看全文
热门项目推荐
相关项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5-w4a8GLM-5-w4a8基于混合专家架构,专为复杂系统工程与长周期智能体任务设计。支持单/多节点部署,适配Atlas 800T A3,采用w4a8量化技术,结合vLLM推理优化,高效平衡性能与精度,助力智能应用开发Jinja00
jiuwenclawJiuwenClaw 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。Python0192- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
AtomGit城市坐标计划AtomGit 城市坐标计划开启!让开源有坐标,让城市有星火。致力于与城市合伙人共同构建并长期运营一个健康、活跃的本地开发者生态。01
awesome-zig一个关于 Zig 优秀库及资源的协作列表。Makefile00
项目优选
收起
deepin linux kernel
C
27
12
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
601
4.04 K
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
69
21
Ascend Extension for PyTorch
Python
441
531
AscendNPU-IR是基于MLIR(Multi-Level Intermediate Representation)构建的,面向昇腾亲和算子编译时使用的中间表示,提供昇腾完备表达能力,通过编译优化提升昇腾AI处理器计算效率,支持通过生态框架使能昇腾AI处理器与深度调优
C++
112
170
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.46 K
823
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
922
770
暂无简介
Dart
846
204
React Native鸿蒙化仓库
JavaScript
321
375
openGauss kernel ~ openGauss is an open source relational database management system
C++
174
249