前言:
整理下常用的富文本编辑器工具。
vue3:
实现效果:

实现步骤:
1、安装插件, 编辑器核心插件 @vueup/vue-quill
yarn add
pnpm i
npm i
cnpm i
@vueup/vue-quill
@vueup/vue-quill
复制
2、安装选择性插件 ,缩放图片插件
quill-blot-formatter
复制
3、新建组件
1)引入基础编辑器插件
// 引入富文本编辑器与样式 import { Quill, QuillEditor } from '@vueup/vue-quill' import '@vueup/vue-quill/dist/vue-quill.snow.css'
复制
2)引入图片缩放插件
// 引入缩放图片的插件 import BlotFormatter from 'quill-blot-formatter' Quill.register('modules/blotFormatter', BlotFormatter)
复制
3)编辑器的配置
4)增加一个划过头部的提示效果
5)增加一个复制粘贴的效果
customEditor.vue 源码
<template> <div class="editor"> <!-- 这两个都是获取值的必要条件: v-model:content contentType="html" --> <quill-editor ref="editorRef" v-model:content="content" :options="options" contentType="html" ></quill-editor> </div> </template> <script setup lang="ts"> // 引入富文本编辑器与样式 import { Quill, QuillEditor } from '@vueup/vue-quill' import '@vueup/vue-quill/dist/vue-quill.snow.css' // 引入缩放图片的插件 import BlotFormatter from 'quill-blot-formatter' Quill.register('modules/blotFormatter', BlotFormatter) const content = ref('') const editorRef = ref(null) // 富文本配置 const options = ref({ theme: 'snow', // 使用snow主题 modules: { // 富文本头部栏的功能配置 toolbar: { container: [ ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线 [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色 [{ align: [] }], // 对齐方式 [{ size: ['small', false, 'large', 'huge'] }], // 字体大小 [{ font: [] }], // 字体种类 [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题 [{ direction: 'ltl' }], // 文本方向 [{ direction: 'rtl' }], // 文本方向 [{ indent: '-1' }, { indent: '+1' }], // 缩进 [{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表 [{ script: 'sub' }, { script: 'super' }], // 上标/下标 ['blockquote', 'code-block'], // 引用 代码块 ['clean'], // 清除文本格式 ['link', 'image', 'video'], // 链接、图片、视频 ], handlers: { image: imageHandler, // 点击图片触发事件 }, }, // 图片缩放 blotFormatter: { // 可以在这里设置缩放样式 // overlay: { // style: { // border: '2px solid red', // } // }, toolbar: { mainClassName: 'blot-formatter__toolbar' } } } }) // !!!如果方法报错,把imageHandler 方法放到options的上面 // 处理富文本图片上传 const imageHandler = () => { // 创建一个文件输入元素 const input = document.createElement('input'); input.setAttribute('type', 'file'); input.setAttribute('accept', 'image/*'); // 模拟点击,打开文件选择对话框 input.click(); // 当用户选择文件后触发的事件 input.onchange = async () => { // 获取用户选择的文件 const file = input.files ? input.files[0] : null; if (file) { // 创建一个 FormData 对象,用于文件上传 const formData = new FormData(); formData.append('file', file); try { /** * @todo 可以选中图片,然后把file文件给后端,后端给存到文件服务器,然后返回一个线上地址 * 这里的abc替换成你的请求接口方法,也可以使用 axios 发送 POST 请求 * */ // todo // 使用 axios 发送 POST 请求,将文件上传到服务器,这里的abc替换成你的请求接口方法 // 可以选中图片,然后把file文件给后端,后端给存到文件服务器,然后返回一个线上地址 const res = await abc(formData); // 确保获取到 Quill 编辑器实例 const quill = toRaw(editorRef.value).getQuill() if (quill) { // 获取当前光标位置 const range = quill.getSelection(true); // 在当前光标位置插入上传的图片 quill.insertEmbed(range.index, 'image', res.data); } } catch (error) { alert('图片上传失败') } } }; } // toolbar标题,划过富文本头部提示信息 const titleConfig = [ { Choice: '.ql-insertMetric', title: '跳转配置' }, { Choice: '.ql-bold', title: '加粗' }, { Choice: '.ql-italic', title: '斜体' }, { Choice: '.ql-underline', title: '下划线' }, { Choice: '.ql-header', title: '段落格式' }, { Choice: '.ql-strike', title: '删除线' }, { Choice: '.ql-blockquote', title: '块引用' }, { Choice: '.ql-code', title: '插入代码' }, { Choice: '.ql-code-block', title: '插入代码段' }, { Choice: '.ql-font', title: '字体' }, { Choice: '.ql-size', title: '字体大小' }, { Choice: '.ql-list[value="ordered"]', title: '编号列表' }, { Choice: '.ql-list[value="bullet"]', title: '项目列表' }, { Choice: '.ql-direction', title: '文本方向' }, { Choice: '.ql-header[value="1"]', title: 'h1' }, { Choice: '.ql-header[value="2"]', title: 'h2' }, { Choice: '.ql-align', title: '对齐方式' }, { Choice: '.ql-color', title: '字体颜色' }, { Choice: '.ql-background', title: '背景颜色' }, { Choice: '.ql-image', title: '图像' }, { Choice: '.ql-video', title: '视频' }, { Choice: '.ql-link', title: '添加链接' }, { Choice: '.ql-formula', title: '插入公式' }, { Choice: '.ql-clean', title: '清除字体格式' }, { Choice: '.ql-script[value="sub"]', title: '下标' }, { Choice: '.ql-script[value="super"]', title: '上标' }, { Choice: '.ql-indent[value="-1"]', title: '向左缩进' }, { Choice: '.ql-indent[value="+1"]', title: '向右缩进' }, { Choice: '.ql-header .ql-picker-label', title: '标题大小' }, { Choice: '.ql-header .ql-picker-item[data-value="1"]', title: '标题一' }, { Choice: '.ql-header .ql-picker-item[data-value="2"]', title: '标题二' }, { Choice: '.ql-header .ql-picker-item[data-value="3"]', title: '标题三' }, { Choice: '.ql-header .ql-picker-item[data-value="4"]', title: '标题四' }, { Choice: '.ql-header .ql-picker-item[data-value="5"]', title: '标题五' }, { Choice: '.ql-header .ql-picker-item[data-value="6"]', title: '标题六' }, { Choice: '.ql-header .ql-picker-item:last-child', title: '标准' }, { Choice: '.ql-size .ql-picker-item[data-value="small"]', title: '小号' }, { Choice: '.ql-size .ql-picker-item[data-value="large"]', title: '大号' }, { Choice: '.ql-size .ql-picker-item[data-value="huge"]', title: '超大号' }, { Choice: '.ql-size .ql-picker-item:nth-child(2)', title: '标准' }, { Choice: '.ql-align .ql-picker-item:first-child', title: '居左对齐' }, { Choice: '.ql-align .ql-picker-item[data-value="center"]', title: '居中对齐' }, { Choice: '.ql-align .ql-picker-item[data-value="right"]', title: '居右对齐' }, { Choice: '.ql-align .ql-picker-item[data-value="justify"]', title: '两端对齐' } ] // 给富文本框工具栏加上鼠标悬浮中文提示 const initTitle = () => { for (let item of titleConfig) { // .editor 是富文本编辑器的类名 let tip = document.querySelector('.editor ' + item.Choice); if (tip) { tip.setAttribute('title', item.title); } } } // 自定义粘贴事件 const customPaste=(e)=>{ // 获取当前最新时间 改名啥的可以用 let newTime = new Date().getTime() const clipboardData = e.clipboardData // 粘贴信息 const types = clipboardData.types // 当前文件类型 if (types.includes('Files')) { e.preventDefault(); e.clipboardData.files.forEach(file=>{ // 在这里可以拿到粘贴后的图片与文件信息 // 在这里做操作 }) } } onMounted(()=>{ nextTick(()=>{ // 给富文本框工具栏加上鼠标悬浮中文提示 initTitle() }) // 给富文本增加粘贴事件 editorRef.value.getQuill().root.addEventListener('customPaste', customPaste, false) }) </script> <style scoped lang="scss"> .editor{ width: 100%; :deep(.ql-editor) { min-height: 200px; } } </style>
复制
vue2
实现步骤:
1、安装插件
基础编辑器插件
npm install vue-quill-editor --save
复制
进阶图片版本插件:拖拽缩放图片
npm install quill-image-drop-module -S //允许粘贴图像并将其拖放到编辑器中。 npm install quill-image-resize-module -S //允许调整图像大小
复制
2、界面中使用
1)全局引入插件,main.js中
import VueQuillEditor from 'vue-quill-editor' // 引入相关css import 'quill/dist/quill.core.css' import 'quill/dist/quill.snow.css' import 'quill/dist/quill.bubble.css' Vue.use(VueQuillEditor)
复制
2)局部引入插件,直接在你自己封装的vue组件内部加
import 'quill/dist/quill.core.css' import 'quill/dist/quill.snow.css' import 'quill/dist/quill.bubble.css' import { quillEditor, Quill } from 'vue-quill-editor' //引入插件 export default { components: { quillEditor } }
复制
3)如果装了图片拖拽以后的引入
vue.config.js 中
chainWebpack: (config) => { config.plugin('provide').use(webpack.ProvidePlugin,[{ 'window.Quill': 'quill/dist/quill.js', 'Quill': 'quill/dist/quill.js' }]) }
复制
组件内部引入、全局就main.js中引入
import 'quill/dist/quill.core.css' import 'quill/dist/quill.snow.css' import 'quill/dist/quill.bubble.css' import { quillEditor, Quill } from 'vue-quill-editor' // 新增部分 import imageResize from 'quill-image-resize-module' Quill.register('modules/imageResize', imageResize ) import { ImageDrop } from 'quill-image-drop-module' Quill.register('modules/imageDrop', ImageDrop)
复制
4)具体使用
html部分,引入插件后,注册后,直接用,以子组件的形式,v-model 监听就跟正常的一样
js部分
注意,如果不需要图片拖拽,就不用看第三步,把配置的直接删除就行了
customEditor.vue 源码
<template> <div class="editor"> <quill-editor class="ql-editor" v-model="content" ref="myQuillEditor" :options="editorOption" @blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @change="onEditorChange($event)"> </quill-editor> </div> </template> <script> import 'quill/dist/quill.core.css' import 'quill/dist/quill.snow.css' import 'quill/dist/quill.bubble.css' import { quillEditor, Quill } from 'vue-quill-editor' // 如果要图片拖拽功能,增加这两个插件 import imageResize from 'quill-image-resize-module' Quill.register('modules/imageResize', imageResize ) import { ImageDrop } from 'quill-image-drop-module' Quill.register('modules/imageDrop', ImageDrop) export default { name: "customEditor", components: { quillEditor }, data(){ return{ content:'', // 富文本功能配置 editorOption:{ modules:{ // 如果想增加事件控制,比如图片的事件,参考我上面vue3的配置,是一样的用法 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': ['small', false, 'large', 'huge'] }], // 字体大小 [{ 'header': [1, 2, 3, 4, 5, 6, false] }], //几级标题 [{ 'color': [] }, { 'background': [] }], // 字体颜色,字体背景颜色 [{ 'font': [] }], //字体 [{ 'align': [] }], //对齐方式 ['clean'], //清除字体样式 ['image','video'] //上传图片、上传视频 ] }, // 如果要图片拖拽功能,增加下面内容配置 imageDrop: true, // 拖动加载图片组件。 imageResize: { //调整大小组件。 displayStyles: { backgroundColor: 'black', border: 'none', color: 'white' }, modules: [ 'Resize', 'DisplaySize', 'Toolbar' ] } }, } }, methods:{ // 失去焦点事件 onEditorBlur(e){ console.log(e, '失去焦点事件'); }, // 获得焦点事件 onEditorFocus(e){ console.log(e, '获得焦点事件'); }, // 内容改变事件 // html是带标签,text是纯文本 onEditorChange({ quill, html, text }){ this.content = html }, }, mounted() { this.content = '<h1>我是个默认测试数据</h1>' } } </script> <style scoped lang="scss"> .editor{ width: 100%; :deep(.ql-editor) { min-height: 200px; } } </style>
复制
目前好用的其他工具:
monaco-editor 能实现跟vscode编辑器一样的在线编辑代码效果,还能代码比较
官网api地址:点我
其他资料地址:
vue2/vue3中使用的富文本编辑器vue-quill-CSDN博客文章浏览阅读2k次,点赞21次,收藏25次。整理下常用的富文本编辑器工具。_vue-quillhttps://blog.csdn.net/weixin_44727080/article/details/143771729?spm=1001.2014.3001.5501