如何配置Strapi默认富文本编辑器工具栏以隐藏含媒体上传的部分选项
精简Strapi默认富文本编辑器并隐藏媒体上传选项
可以通过自定义配置或扩展默认组件实现,无需第三方插件,以下是两种可行方案:
方案1:通过配置文件自定义工具栏(Strapi v4.10+适用)
Strapi v4.10及以上版本支持直接通过配置文件修改默认编辑器的工具栏选项:
- 在项目根目录的
config/custom.js文件中(没有则新建)添加以下配置,仅保留需要的工具栏按钮,剔除所有媒体相关选项:
module.exports = { richText: { editor: { toolbar: [ 'bold', 'italic', 'underline', 'strikethrough', 'link', 'heading-1', 'heading-2', 'heading-3', 'quote', 'ordered-list', 'unordered-list', // 移除image、video、audio等媒体相关按钮 ], }, }, };
- 重启Strapi服务,配置立即生效。
方案2:扩展默认RichText字段组件(全v4版本适用)
如果你的Strapi版本低于4.10,或需要更精细的控制,可通过扩展默认组件实现:
- 在
src/admin/components下创建CustomRichTextEditor/index.js文件,内容如下:
import React from 'react'; import { RichTextEditor } from '@strapi/rich-text-editor'; const CustomRichTextEditor = (props) => { // 自定义工具栏,移除所有媒体相关按钮 const customToolbar = [ ['bold', 'italic', 'underline', 'strikethrough'], ['link'], ['heading-1', 'heading-2', 'heading-3'], ['quote'], ['ordered-list', 'unordered-list'], ]; return <RichTextEditor {...props} toolbar={customToolbar} />; }; export default CustomRichTextEditor;
- 在
src/admin/app.js中注册自定义组件,替换默认的RichText字段:
import { registerFieldComponent } from '@strapi/helper-plugin'; import CustomRichTextEditor from './components/CustomRichTextEditor'; function bootstrap(app) { registerFieldComponent('richtext', CustomRichTextEditor); } export default bootstrap;
- 执行
npm run build构建admin面板,再重启Strapi服务,所有富文本字段将使用自定义工具栏。
注意事项
- 方案1仅支持v4.10+版本,升级前请做好数据备份
- 配置工具栏时,确保不包含
image、video、audio、media-library等媒体相关按钮 - 方案2修改后必须重新构建admin面板才会生效
内容的提问来源于stack exchange,提问作者rushikesh chaskar
相关产品推荐
相关产品推荐

