npm install vue-quill-editor --save
import Vue from 'vue'
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>
<div>
<quill-editor
v-model="content"
ref="myQuillEditor"
:options="editorOption"
@blur="onEditorBlur($event)"
@focus="onEditorFocus($event)"
@change="onEditorChange($event)">
</div>
</template>
<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor';
export default {
data(){
return{
content: '',
editorOption: {
placeholder: '编辑文章内容'
},
}
},
components: {
quillEditor
},
methods:{
onEditorReady(editor) {// 准备编辑器
},
onEditorBlur(){// 失去焦点事件
},
onEditorFocus(){// 获得焦点事件
},
onEditorChange(){// 内容改变事件
},
}
}
</script>
//编辑器禁用
onEditorFocus(val,editor){ // 富文本获得焦点时的事件
console.log(val); // 富文本获得焦点时的内容
editor.enable(false); // 在获取焦点的时候禁用
}
背景颜色 - background
加粗- bold
颜色 - color
字体 - font
内联代码 - code
斜体 - italic
链接 - link
大小 - size
删除线 - strike
上标/下标 - script
下划线 - underline
引用- blockquote
标题 - header
缩进 - indent
列表 - list
文本对齐 - align
文本方向 - direction
代码块 - code-block
公式 - formula
图片 - image
视频 - video
清除字体样式- clean
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': ['small', false, 'large', 'huge'] }], // 字体大小
[{ 'header': [1, 2, 3, 4, 5, 6, false] }], //几级标题
[{ 'color': [] }, { 'background': [] }], // 字体颜色,字体背景颜色
[{ 'font': [] }], //字体
[{ 'align': [] }], //对齐方式
['clean'], //清除字体样式
['image','video'] //上传图片、上传视频
]
},
theme:'snow'
}
}
const titleConfig = {
'ql-bold': '加粗',
'ql-color': '颜色',
'ql-font': '字体',
'ql-code': '插入代码',
'ql-italic': '斜体',
'ql-link': '添加链接',
'ql-background': '背景颜色',
'ql-size': '字体大小',
'ql-strike': '删除线',
'ql-script': '上标/下标',
'ql-underline': '下划线',
'ql-blockquote': '引用',
'ql-header': '标题',
'ql-indent': '缩进',
'ql-list': '列表',
'ql-align': '文本对齐',
'ql-direction': '文本方向',
'ql-code-block': '代码块',
'ql-formula': '公式',
'ql-image': '图片',
'ql-video': '视频',
'ql-clean': '清除字体样式'
}
export function addQuillTitle () {
const oToolBar = document.querySelector('.ql-toolbar'),
aButton = oToolBar.querySelectorAll('button'),
aSelect = oToolBar.querySelectorAll('select')
aButton.forEach(function (item) {
if (item.className === 'ql-script') {
item.value === 'sub' ? item.title = '下标' : item.title = '上标'
} else if (item.className === 'ql-indent') {
item.value === '+1' ? item.title = '向右缩进' : item.title = '向左缩进'
} else {
item.title = titleConfig[item.classList[0]]
}
})
aSelect.forEach(function (item) {
item.parentNode.title = titleConfig[item.classList[0]]
})
}
import { addQuillTitle } from './quillTitle.js'
export default {
components: {
quillEditor
},
mounted(){
addQuillTitle();
},
data() {
return {
content: '<h2>freddy</h2>',
}
}
}
配置设置 按Ctrl+Enter可换行 enter为换行,在官网手册模块-键盘(keyboard中详细设置)
editorOption: {
modules: {
imageDrop: true,
keyboard: {
bindings: {
// This will overwrite the default binding also named 'tab'
tab1: {
key: 13,
handler: this.saveOver
},
tab2: {
key: 13,//enter键
ctrlKey: true,//ctrl键
handler: this.setEnterCtrl
}
}
}
},
theme: 'snow', // snow是有工具栏的 bubble文本域
placeholder: '请输入内容...'
},
改写ctrl+enter换行事件
setEnterCtrl () {
console.log('ctrl + enter')
let quill = this.$refs.myQuillEditor.quill
// 获取编辑器光标位置
let length = quill.selection.savedRange.index
// 插入图片至光标位置,look为图片地址
quill.insertText(length, `\n`)
// 移动光标至图片后面
quill.setSelection(length + 1)
},
// 插入表情
addExpression (look) {
console.log(look.length, '插入长度为')
// this.content+=look
// 获取编辑器对象
let quill = this.$refs.myQuillEditor.quill
// 获取编辑器光标位置
let length = quill.selection.savedRange.index
// 插入图片至光标位置,look为图片地址
quill.insertText(length, look)
// 移动光标至图片后面
quill.setSelection(length + look.length)
},
// 光标位置插入图片
addBlock (imgSrc) {
// 获取编辑器对象
let quill = this.$refs.myQuillEditor.quill
// 获取编辑器光标位置
let length = quill.selection.savedRange.index
// 插入图片至光标位置,imgUrl为图片地址
quill.insertEmbed(length, 'image', imgSrc)
// 移动光标至图片后面
quill.setSelection(length + 1)
},
this.editor.getContents()获取数据格式如下:
saveHtml: function (event) {
if (this.content === undefined || this.content.trim() === '') {
this.$message.warning('发送的消息不能为空')
this.$emit('resetSendState')
return
}
// 获取内容数据 this.editor.getContents()获取数据放在ops中
let data = this.editor.getContents().ops
}
}
this.content = ''
this.$emit('resetSendState')
},
中文文档地址:https://www.kancloud.cn/liuwave/quill/1409369#getContents_21
github地址:[https://github.com/surmon-china/vue-quill-editor](