vue使用富文本编辑器vue-quill-editor

article/2025/8/22 19:21:06

问题描述:

我们在开发过程中经常会遇到使用富文本编辑器进行操作,当前插件市场上关于富文本的编辑器挺多的,我们就不一一个介绍了,现在我们主要讲解一些vue-quill-editor富文本编辑器的使用操作和注意事项。

效果图

在这里插入图片描述

具体操作使用

1. 安装

npm install vue-quill-editor -S

2. 引入到项目中

有两种挂载方式: 全局挂载 和 在组件中挂载,根据自己的项目需求选择,一般用到富文本编辑都是在某一个项目中,我们这里为了方便大家选择,两种引用方案都写下来以便大家参考:

(1),页面中引用

import { quillEditor } from 'vue-quill-editor'import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'export default {components: {quillEditor}
}

(2),全局中引用

import Vue from 'vue'
import VueQuillEditor from 'vue-quill-editor'// 引入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'Vue.use(VueQuillEditor, /* { 默认全局 } */)

3. 页面上面具体实现

<template><quill-editor class="ql-editor"v-model="content" ref="myQuillEditor" :options="editorOption" @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"@change="onEditorChange($event)"></quill-editor>
</template><script>
export default {data() {return {content: `<p>这是 vue-quill-editor 的内容!</p>`, //双向数据绑定数据// 富文本编辑器配置editorOption: {modules: {toolbar: [['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线['blockquote', 'code-block'], // 引用  代码块[{ header: 1 }, { header: 2 }], // 1、2 级标题[{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表[{ script: 'sub' }, { script: 'super' }], // 上标/下标[{ indent: '-1' }, { indent: '+1' }], // 缩进[{ direction: 'rtl' }], // 文本方向[{ size: ['12px', false, '16px', '18px', '20px', '30px'] }], // 字体大小[{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题[{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色[{ font: [false, 'SimSun', 'SimHei', 'Microsoft-YaHei', 'KaiTi', 'FangSong', 'Arial'] }], // 字体种类[{ align: [] }], // 对齐方式['clean'], // 清除文本格式['link', 'image', 'video'] // 链接、图片、视频},placeholder: '请输入正文'} }},methods: {// 失去焦点事件onEditorBlur(quill) {console.log('editor blur!', quill)},// 获得焦点事件onEditorFocus(quill) {console.log('editor focus!', quill)},// 准备富文本编辑器onEditorReady(quill) {console.log('editor ready!', quill)},// 内容改变事件onEditorChange({ quill, html, text }) {console.log('editor change!', quill, html, text)this.content = html}}
}
</script>

到这里一个默认的富文本编辑器已经导入使用了,如下图所视!
在这里插入图片描述

4.为了更好配合使用,样式上面进行优化,使用中文标识方便查看

(1),重新自定义字体类型

import Quill from 'quill' // 引入编辑器
// 自定义字体大小
const Size = Quill.import('attributors/style/size')
Size.whitelist = ['10px', '12px', '16px', '18px', '20px', '30px', '32px']
Quill.register(Size, true)// 自定义字体类型
var fonts = ['SimSun', 'SimHei', 'Microsoft-YaHei', 'KaiTi', 'FangSong', 'Arial', 'sans-serif']
var Font = Quill.import('formats/font')
Font.whitelist = fonts
Quill.register(Font, true)

(2),自定义对应样式

// 给文本内容加高度,滚动条
.quill-editor /deep/ .ql-container {min-height: 220px;}.ql-container {min-height: 230px;}/deep/ {.ql-snow .ql-tooltip [data-mode="link"]::before {content: "请输入链接地址:";left: 0;}.ql-snow .ql-tooltip.ql-editing {left: 0 !important;}.ql-snow .ql-tooltip {left: 0 !important;}.ql-snow .ql-editor {max-height: 300px;}.ql-snow .ql-tooltip.ql-editing a.ql-action::after {border-right: 0px;content: "保存";padding-right: 0px;}.ql-snow .ql-tooltip[data-mode="video"]::before {content: "请输入视频地址:";}.ql-snow .ql-picker.ql-size .ql-picker-label::before, .ql-snow .ql-picker.ql-size .ql-picker-item::before {content: "14px" !important;font-size: 14px;}.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="10px"]::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="10px"]::before {content: "10px" !important;font-size: 10px;}.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="12px"]::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="12px"]::before {content: "12px" !important;font-size: 12px;}.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="16px"]::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="16px"]::before {content: "16px" !important;font-size: 16px;}.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="18px"]::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="18px"]::before {content: "18px" !important;font-size: 18px;}.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="20px"]::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="20px"]::before {content: "20px" !important;font-size: 20px;}.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="30px"]::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="30px"]::before {content: "30px" !important;font-size: 30px;}.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="32px"]::before, .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="32px"]::before {content: "32px" !important;font-size: 32px;}.ql-snow .ql-picker.ql-header .ql-picker-label::before, .ql-snow .ql-picker.ql-header .ql-picker-item::before {content: "文本" !important;}.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before, .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {content: "标题1" !important;}.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before, .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {content: "标题2" !important;}.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before, .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {content: "标题3" !important;}.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before, .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {content: "标题4" !important;}.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before, .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {content: "标题5" !important;}.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before, .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {content: "标题6" !important;}.ql-snow .ql-picker.ql-font .ql-picker-label::before, .ql-snow .ql-picker.ql-font .ql-picker-item::before {content: "标准字体" !important;}.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before, .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {content: "衬线字体" !important;}.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before, .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before {content: "等宽字体" !important;}.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="SimSun"]::before, .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="SimSun"]::before {content: "宋体" !important;font-family: "SimSun";}.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="SimHei"]::before, .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="SimHei"]::before {content: "黑体" !important;font-family: "SimHei";}.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="Microsoft-YaHei"]::before, .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="Microsoft-YaHei"]::before {content: "微软雅黑" !important;font-family: "Microsoft YaHei";}.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="KaiTi"]::before, .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="KaiTi"]::before {content: "楷体" !important;font-family: "KaiTi";}.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="FangSong"]::before, .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="FangSong"]::before {content: "仿宋" !important;font-family: "FangSong";}.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="Arial"]::before, .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="Arial"]::before {content: "Arial" !important;font-family: "Arial";}.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="Times-New-Roman"]::before, .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="Times-New-Roman"]::before {content: "Times New Roman" !important;font-family: "Times New Roman";}.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="sans-serif"]::before, .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="sans-serif"]::before {content: "sans-serif" !important;font-family: "sans-serif";}.ql-font-SimSun {font-family: "SimSun";}.ql-font-SimHei {font-family: "SimHei";}.ql-font-Microsoft-YaHei {font-family: "Microsoft YaHei";}.ql-font-KaiTi {font-family: "KaiTi";}.ql-font-FangSong {font-family: "FangSong";}.ql-font-Arial {font-family: "Arial";}.ql-font-Times-New-Roman {font-family: "Times New Roman";}.ql-font-sans-serif {font-family: "sans-serif";}.ql-snow.ql-toolbar .ql-formats .ql-revoke {background-image: url("../../../../assets/images/icons8-rotate-left-18.png");width: 20px;height: 20px;filter: grayscale(100%);opacity: 1;}.ql-snow.ql-toolbar .ql-formats .ql-revoke:hover {background-image: url("../../../../assets/images/icons8-rotate-left-18.png");width: 20px;height: 20px;filter: none;opacity: 0.9;}img {filter: grayscale(100%);opacity: 1;}img:hover {filter: none;opacity: 0.9;}/*加上height和滚动属性就可以,滚动条样式是系统默认样式,可能不同*/.ql-toolbar.ql-snow .ql-picker.ql-expanded .ql-picker-options {border-color: #ccc;height: 125px;overflow: auto;}}

(3),注意点:富文本里面的下拉框默认是不滚动的,想要滚动效果,加上下面的css

/*加上height和滚动属性就可以,滚动条样式是系统默认样式,可能不同*/
.ql-toolbar.ql-snow .ql-picker.ql-expanded .ql-picker-options {border-color: #ccc;height: 125px;overflow: auto;
}

在这里插入图片描述

5.上传图片到七牛云

vue-quill-editor 默认的是以 base64 保存图片,会直接把图片 base64 和内容文本一起以字符串的形式提交到后端。这样小图片还行,如果要上传大图片会提示上传失败,优秀的前端打字员显然不会这样做。

思路
可以先将图片上传至服务器,再将图片链接插入到富文本中显示
图片上传可以自定义一个组件或者使用 element-ui 的上传图片的组件(我在项目中使用的是自定义的组件,这里演示使用 element-ui 组件上传)
上传图片的组件需要隐藏,点击图片上传时调用element-ui 的图片上传,上传成功后返回图片链接。
在编辑器项中配置配置项


<el-uploadv-show="false"class="avatar-uploader":data='fileUpload':show-file-list="false":http-request="onUploadHandler">
</el-upload>

在option中配置上传操作,之前的option就耀稍作修改

handlers: {'image': function (value) {if (value) { // value === truedocument.querySelector('.avatar-uploader input').click()} else {this.quill.format('image', false)}}}
 点击富文本上的上传图片,就会触发这里的handlers,将操作引到upload的函数上,在这个函数里面需要做的操作是,将图片上传到七牛云,并拿到返回的在线链接,然后将图片链接插入到页面对应位置上。这里我的上传是自己封装了函数。
async onUploadHandler(e) {const imageUrl = 上传七牛云后返回来的一串在线链接// 获取光标所在位置let quill = this.$refs.myQuillEditor.quilllet length = quill.getSelection().index// 插入图片  quill.insertEmbed(length, 'image', imageUrl)// 调整光标到最后quill.setSelection(length + 1)// this.content += url}

6.自定义控制图片大小

(1),安装插件

 npm i quill-image-resize-module -S

(2),在文件中导入包

import Quill from 'quill' 
import ImageResize from 'quill-image-resize-module' 
Quill.register('modules/imageResize', ImageResize)  

(3),在原本的配置项上添加(与toolbar平级进行配置)

toolbar: {},
// 调整图片大小imageResize: {displayStyles: {backgroundColor: 'black',border: 'none',color: 'white'},modules: [ 'Resize', 'DisplaySize', 'Toolbar' ]}

效果:
在这里插入图片描述

7.自定义toobar样式设计

//在quill中使用
toolbar: {container: toolbarOptions,handlers: {**report: this.openWorkReport**}}
// 方法中使用openWorkReport () {this.$emit('openWorkReport')},// 样式
/* 自定义toobar样式设计 */
/* 工作汇报弹窗 */
.ql-snow.ql-toolbar .ql-formats .ql-report{background: url("../images/meeting/report.png") no-repeat;background-size: contain;display: inline-block;height: 18px;margin: 3px 5px;width: 28px;
}

效果

在这里插入图片描述
有关视频上传参考:视频上传


http://chatgpt.dhexx.cn/article/3ALqORLa.shtml

相关文章

LayUI - 富文本编辑器

一个做后端的猿&#xff0c;难免用到LayUI&#xff0c;首先在这里&#xff0c;不推荐使用&#xff0c;坑多 我这里用的是layui-v2.5.7版本 一、富文本编辑器 缺点&#xff1a;功能太少&#xff0c;只能满足简单需求&#xff0c;只有下面这几个功能 废话少说&#xff0c;直接丢…

最佳文本编辑器

原文&#xff1a; donationcoder.com  译者&#xff1a; xbeta善用佳软  说明&#xff1a;仅做翻译&#xff0c;忠实原文。不代表同意文中观点&#xff08;xbeta认为最好的编辑器为VIM&#xff09;。 最佳文本编辑器 当前&#xff0c;好用的文本编辑器比比皆是——无论商…

Qt实现文本编辑器(一)

在Qt中QMainWindow是一个为用户提供主窗口程序的类&#xff0c;包含了&#xff1a;菜单栏、工具栏、锚接部件、状态栏以及一个中部件。今天我就来通过实现一个简单的文本编辑器讲解下对QMainWindow的各种功能讲解。 想要完整的实现一个编辑器&#xff0c;所需要的功能还是比较…

文本编辑器推荐

对于程序员或者开发者来说&#xff0c;可以通过电脑文本编辑器来完成语言的编译或输入&#xff0c;那么文本编辑器哪个好呢&#xff0c;其实使用系统自带的文本框就不错&#xff0c;当然还有不少其他软件可用。 文本编辑器哪个好&#xff1a; 一、记事本 1、这是系统自带的文…

富文本编辑器汇总

富文本编辑器&#xff1a;&#xff08;Rich Text Editor&#xff0c;RTE&#xff09;是一种可内嵌于浏览器&#xff0c;所见即所得的文本编辑器。它提供类似于Office Word 的编辑功能&#xff0c;方便那些不太懂HTML用户使用&#xff0c;富文本编辑器的应用非常广泛&#xff0c…

十五种文本编辑器

很多时候比如编程查看代码或者打开各种文档下我们都会用到文本编辑器&#xff0c;Windows自带的记事本功能很简陋并且打开大文件很慢&#xff0c;因此很多童鞋都会有自己喜欢的一款文本编辑器。在这里&#xff0c;西西挑选前15个最佳的文本编辑器&#xff0c;这些编辑器实际上主…

强烈呼吁弃用Notepad++,优秀替代品献上

Notepad作为一款开源文本编辑软件&#xff0c;无可厚非是一款优秀的软件, 但是由于“种种原因”, 坚决弃用。 禁用NotePad 推荐三款优秀的免费替代品&#xff1a; 可以直接去官网下载&#xff0c;也可以在下面的网盘地址下载&#xff08;分别提供了安装版&#xff08;分32位和…

adreno性能天梯图_深度学习之GPU显卡性能天梯图

在深度学习的显卡市场&#xff0c;英伟达的地位还是暂时无人能够撼动的。专业卡暂不纳入考虑&#xff0c;毕竟性价比太低了。大家平时使用的还是老黄的游戏卡&#xff0c;性能排第一的就是Titan RTX了&#xff0c;具备24G大显存&#xff0c;然而售价也高达两万块。接下来就是大…

2013台式计算机,显卡天梯图 2013最新台式机显卡天梯图

在聊完笔记本显卡的性能后,小编当然要和大家聊聊重头戏了:台式机的显卡天梯图。台式电脑一直是玩大型3D游戏的必选机型,虽然没有笔记本的便携性,但是台式机可以让电脑显卡性能得到充分的发挥,主要是独立显卡的散热不像笔记本那样存在空间狭小的问题。 台式机显卡性能一直…

mx350显卡天梯图_显卡天梯图2020年终整理发布

购买显卡如何选择&#xff1f;当然是有参考才能更好的选择&#xff0c;可是我们要拿什么参考呢&#xff1f;没错&#xff0c;通过笔者一年的时间收集整理&#xff0c;我们将为大家带来本年度英伟达显卡和AMD显卡天梯图。 AMD显卡天梯图&#xff1a; 英伟达显卡天梯图&#xff1…

2020电脑服务器cpu性能天梯图,CPU性能天梯图[202002版]

CPU与显卡还不太一样&#xff0c;CPU有线程&#xff0c;线程多&#xff0c;那性能会强。以前我们对CPU的认识有一个误区&#xff0c;老以为i5就比i3强&#xff0c;或者i7就比i5强&#xff0c;其实不一定。您以后就别再听电脑城那些人乱说&#xff0c;“我给你配置的是i7 的CPU&…

服务器单核性能天梯图,台式机cpu性能排行(cpu单核性能天梯图)

【科技犬】 鲁大师发布 2021 年 Q1 季度电脑处理器性能排行榜&#xff0c;霸榜两年的 AMD Ryzen Threadripper 3990X 依然是第一名。最新发布的 Threadripper PRO 3995WX 数据太少达不到上榜要求。 如上图所示&#xff0c;鲁大师台式机处理器排行榜前四名都是 AMD 处理器&#…

mx350显卡天梯图_五月显卡性能排行 台式显卡天梯图2020年5月最新版

显卡天梯图(更新至2020年5月9日)主要用来纵向对比一下显卡性能的高低&#xff0c;这样对于我们在DIY装机过程当中&#xff0c;选购桌面显卡有个参考标准。台式桌面级显卡天梯图方便我们更直观地对比不同厂商最新显卡之间的性能高低。以下IT数码通为大家带来2020年5月最新显卡天…

显卡天梯图:2014最新显卡性能天梯图

随着电脑游戏的推广&#xff0c;很多用户都喜欢上了电脑网络游戏&#xff0c;所以组装电脑用户在装机的时候&#xff0c;会考虑电脑配置的游戏性能&#xff0c;要提高电脑配置游戏性能首要条件就是显卡性能要强&#xff0c;如果显卡性能不佳&#xff0c;那么其它方面性能再强&a…

CPU-显卡-硬盘性能天梯图排行榜源码

介绍&#xff1a; CPU天梯图和桌面显卡性能天梯图排行榜&#xff0c;硬盘性能天梯图排行榜源码分享&#xff01; CPU&#xff0c;GPU显卡&#xff0c;硬盘所有文件以进行本地化,嗨次元在线工具箱&#xff0c;CPU性能天梯表,CPU性能天梯表,CPU性能天梯图,GPU性能天梯图,显卡性…

桌面显卡天梯图2023年2月 台式机显卡天梯图2023

1、NVIDIA GeForce RTX 3090 Ti 24GB。 2、NVIDIA GeForce RTX 3090 24GB。 3、NVIDIA GeForce RTX 3080 Ti 12GB。 4、AMD Radeon RX 6900XT 16GB。 5、NVIDIA GeForce RTX 3080 12GB。 6、AMD Radeon RX 6800XT 16GB。 组装电脑怎么搭配显卡更合适这些点很重要看过你就懂了 h…

2014显卡性能天梯图,组装电脑备用

2019独角兽企业重金招聘Python工程师标准>>> 2014显卡性能天梯图&#xff0c;组装电脑备用&#xff0c;原来i7的HD4600是不如08年ATI的HD3850的(当时499元)&#xff1a; 转载于:https://my.oschina.net/lizhiling/blog/375346

英伟达显卡排名天梯图2022

英伟达显卡排名天梯图 英伟达显卡排名天梯图 英伟达显卡排名天梯图 3090ti 3070ti 3050ti 1、Fermi费米架构 费米是诺贝尔物理学奖得主&#xff0c;被称为原子能之父&#xff0c;他的实验小组建立了人类第一台可控核反应堆e69da5e6ba90323131333532363134313032313635333…

2012年10月显卡性能天梯图

2013年3月16日 1、台式机显卡天梯图 转&#xff1a;http://zgcdiy.com/article-1960.html 2、笔记本显卡天梯图 转&#xff1a;http://ideapad.zol.com.cn/59/160_583555.html 转载于:https://www.cnblogs.com/pingyidou/archive/2013/03/17/2963301.html

显卡性能排行榜2023 显卡天梯图2023年7月

一、显卡性能排名 1、NVIDIA GeForce RTX 3080 2、AMD Radeon RX 6800M 3、NVIDIA GeForce RTX 3070 笔记本选哪款好这些点很重要 http://www.adiannao.cn/dq 二、性能测试方法 1、基准测试&#xff1a;使用3DMark软件进行显卡性能测试&#xff0c;包括图形性能、物理性能…