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编辑器之旅吧!如果有任何问题,记得查阅官方文档或加入社区讨论。
登录后查看全文
热门项目推荐
相关项目推荐
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00- QQwen3-Coder-Next2026年2月4日,正式发布的Qwen3-Coder-Next,一款专为编码智能体和本地开发场景设计的开源语言模型。Python00
xw-cli实现国产算力大模型零门槛部署,一键跑通 Qwen、GLM-4.7、Minimax-2.1、DeepSeek-OCR 等模型Go06
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
KuiklyUI基于KMP技术的高性能、全平台开发框架,具备统一代码库、极致易用性和动态灵活性。 Provide a high-performance, full-platform development framework with unified codebase, ultimate ease of use, and dynamic flexibility. 注意:本仓库为Github仓库镜像,PR或Issue请移步至Github发起,感谢支持!Kotlin08
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00
热门内容推荐
最新内容推荐
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
532
3.75 K
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
336
178
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
886
596
Ascend Extension for PyTorch
Python
340
405
暂无简介
Dart
772
191
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
12
1
openJiuwen agent-studio提供零码、低码可视化开发和工作流编排,模型、知识库、插件等各资源管理能力
TSX
986
247
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
416
4.21 K
React Native鸿蒙化仓库
JavaScript
303
355