vue中使用vue-quill-editor富文本编辑器,自定义toolbar修改工具栏options

本文介绍如何使用Vue和Webpack配置并自定义quill-editor,包括安装、基本配置及工具栏功能选择,帮助读者实现个性化的富文本编辑需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于webpack和vue

一、npm 安装 vue-quill-editor
二、在main.js中引入

import  VueQuillEditor from 'vue-quill-editor'
// require styles 引入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

Vue.use(VueQuillEditor)

三、在模块中引用

<template>
     <quill-editor 
      v-model="content" 
      ref="myQuillEditor" 
      :options="editorOption" 
      @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
      @change="onEditorChange($event)">
    </quill-editor>
</template> 
<script>
    import { quillEditor } from 'vue-quill-editor'
    export default{
        data(){
            return {
                content:null,
                editorOption:{}
            }
        },
        methods:{
            onEditorBlur(){//失去焦点事件
            },
            onEditorFocus(){//获得焦点事件
            },
            onEditorChange(){//内容改变事件
            }
        }
    }
</script>   

这样引入后你会得到这样一个编辑器vue-quill-editor
那么你如果不需要那么多的工具栏功能要怎么办呢;应该是通过options来修改但是他的默认值是什么的
我在百度找了一圈也没找到方法
最后在https://quilljs.com/docs/themes/这个官方文档里面看到了类似的方法
这里写图片描述
初始值的设置应该是一样的吧
所以我就照着toolbar部分去修改了options

<script>
    import { quillEditor } from 'vue-quill-editor'
    export default{
        data(){
            return {
                content:null,
                editorOption:{
                    modules:{
                        toolbar:[
                          ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
                           ['blockquote', 'code-block']
                        ]
                    }
                }
            }
        },
        methods:{
            onEditorBlur(){//失去焦点事件
            },
            onEditorFocus(){//获得焦点事件
            },
            onEditorChange(){//内容改变事件
            }
        }
    }
</script>   

果然 生效了 只显示了我写在toolbar里面的模块
这里写图片描述

那么toolbar工具栏对应功能的模块名是什么呢 我继续往下看文档 发现大致上有以下的功能

背景颜色 - background
加粗- bold
颜色 - color
字体 - font
内联代码 - code
斜体 - italic
链接 - link
大小 - size
删除线 - strike
上标/下标 - script
下划线 - underline
引用- blockquote
标题 - header
缩进 - indent
列表 - list
文本对齐 - align
文本方向 - direction
代码块 - code-block
公式 - formula
图片 - image
视频 - video
清除字体样式- clean

然而我试着直接引入发现有部分的图标并没有显示;
然后我发现他有些如list这种列表应该是有默认值,我在很后面的文档里发现了这个默认格式规范 这个官方文档也是个坑 内容不写到一块的 还好我聪明机智;

这里写图片描述

大致上分为这几类:

1.只需要填写功能名的
加粗 - bold;
斜体 - italic
下划线 - underline
删除线 - strike
引用- blockquote
代码块 - code-block
公式 - formula
图片 - image
视频 - video
清除字体样式- clean
这一类的引用 直接['name','name']这种格式就好了

2.需要有默认值的
标题 - header  
[{ 'header': 1 }, { 'header': 2 }] 值字体大小

列表 - list 
[{ 'list': 'ordered'}, { 'list': 'bullet' }], 值ordered,bullet

上标/下标 - script 
 [{ 'script': 'sub'}, { 'script': 'super' }],  值sub,super

缩进 - indent
[{ 'indent': '-1'}, { 'indent': '+1' }], 值-1,+1等

文本方向 - direction
[{ 'direction': 'rtl' }],    值rtl

这部分如图所示会填写的内容对应提供的值展示功能的图标 如果多个值他家就显示多个图标
这里写图片描述

3.有多个值 以下拉列表形式显示

大小 - size
 [{ 'size': ['small', false, 'large', 'huge'] }],  

标题 - header
[{ 'header': [1, 2, 3, 4, 5, 6, false] }],

这部分显示如图所示 以下拉列形式显示
这里写图片描述

4.有系统默认值的功能只需填写一个空数组 就会有出现默认的选项
颜色 - color
背景颜色 - background
字体 - font
文本对齐 - align
他们的格式都是
[{ 'color': [] }, { 'background': [] }], 
[{ 'font': [] }],
[{ 'align': [] }]
这种格式

效果如下 会有默认值出现
这里写图片描述

toolbar自定义工具栏就是这样咯 剩下的就是根据填写功能到options的modules里就可以了
这里写图片描述

### 实现富文本编辑器的二次封装 #### 自定义配置与基础设置 对于 UEditor 这样的富文本编辑器,在进行自定义配置时,可以调整工具栏中的按钮、字体样式以及其他基本属性。通过修改配置文件或初始化参数来定制编辑器界面和行为[^1]。 ```javascript // JavaScript 初始化UEditor实例并应用个性化设置 var ue = UE.getEditor('container', { toolbars: [ ['bold', 'italic', 'underline'] // 只保留加粗、斜体、下划线三个功能 ], initialFrameWidth: null, initialFrameHeight: 300 }); ``` #### 扩展新功能 当现有功能无法满足特定需求时,则需考虑向编辑器内添加新的特性。以 UEditor为例,可以通过编写插件的形式引入额外的操作选项,比如增加一个新的按钮用于执行特殊命令[^3]。 ```html <!-- HTML 添加自定义按钮到UEditor --> <div id="editor"> <script type="text/plain" id="myEditor"></script> </div> <script src="/path/to/ueditor.config.js"></script> <script src="/path/to/ueditor.all.min.js"></script> <script> (function(){ var editor = UE.getEditor('myEditor'); // 注册自定义命令 UE.commands['customCommand'] = { execCommand : function(cmdName){ alert('Custom command executed!'); } }; // 创建自定义按钮 editor.addListener('ready',function(){ editor.execCommand('insertHtml','<button onclick="UE.getEditor(\'myEditor\').execCommand(\'customCommand\')">Click Me!</button>'); }); })(); </script> ``` #### 使用现代框架简化集成过程 如果采用像 Quill 或 wangEditor 这样更现代化且易于扩展的库,则可通过其提供的 API 更便捷地完成相同任务。例如,在 Vue 组件中利用 Quill 的 `format()` 方法快速实现内容格式化操作[^4]。 ```vue <template> <quill-editor v-model="content"/> </template> <script> export default { data () { return { content: '' }; }, methods: { formatTitle (title) { const quill = this.$refs.editor.quill; let index = quill.getLength(); quill.insertText(index, title); quill.setSelection(index + title.length); quill.format("header", 2); // 设置为H2标题 } } } </script> ```
评论 34
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值