- 基本使用
// 安装
1. yarn add vue-quill-editor
2. yarn add quill-image-resize-module
- 使用
quill-image-resize-module启动会报错!解决办法:需在vue.config.js中配置
// vue.config.js
const webpack = require('webpack') // 引入
configureWebpack: {
plugins: [
new webpack.ProvidePlugin({
'window.Quill': 'quill/dist/quill.js',
'Quill': 'quill/dist/quill.js'
})
]
},
- 创建自定义customEditor.js(下面组件中会引用)
import { quillEditor, Quill } from 'vue-quill-editor'
import ImageResize from 'quill-image-resize-module' // 改变编辑器图片大小插件
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 调整上传图片大小
Quill.register('modules/imageResize', ImageResize)
// 自定义视频
const ATTRIBUTES = ['width', 'height', 'poster', 'data-src']
const BlockEmbed = Quill.import('blots/block/embed')
class VideoBlot extends BlockEmbed {
static create(value) {
const node = super.create()
node.setAttribute('src', value.url)
node.setAttribute('controls', value.controls)
node.setAttribute('width', value.width)
node.setAttribute('height', value.height)
node.setAttribute('poster', value.poster)
node.setAttribute('data-src', value.media_id)
node.setAttribute('webkit-playsinline', true)
node.setAttribute('playsinline', true)
node.setAttribute('x5-playsinline', true)
return node
}
static formats(domNode) {
return ATTRIBUTES.reduce((formats, attribute) => {
if (domNode.hasAttribute(attribute)) {
formats[attribute] = domNode.getAttribute(attribute)
}
return formats
}, {})
}
static value(node) {
return {
url: node.getAttribute('src'),
controls: node.getAttribute('controls'),
width: node.getAttribute('width'),
height: node.getAttribute('height'),
poster: node.getAttribute('poster')
}
}
format(name, value) {
if (ATTRIBUTES.indexOf(name) > -1) {
if (value) {
this.domNode.setAttribute(name, value)
} else {
this.domNode.removeAttribute(name)
}
} else {
super.format(name, value)
}
}
}
VideoBlot.blotName = 'simpleVideo'
VideoBlot.tagName = 'video'
Quill.register(VideoBlot)
// 自定义字体大小
const Size = Quill.import('attributors/style/size')
const fontSize = ['10px', '12px', '14px', '16px', '18px', '20px', '24px', '28px', '30px']
Size.whitelist = fontSize
Quill.register(Size, true)
// 自定义字体类型
const fonts = [
'sans-serif',
'Microsoft-YaHei',
'SimSun',
'SimHei',
'KaiTi',
'FangSong',
'Arial'
]
const Font = Quill.import('formats/font')
Font.whitelist = fonts
Quill.register(Font, true)
// 对齐方式改为style绑定而非class类名
const Align = Quill.import('attributors/style/align')
Align.whitelist = ['right', 'center', 'justify']
Quill.register(Align, true)
export { quillEditor, Quill, fonts, fontSize }
- 创建自定义tools.js文件(组件中会引用)
import { fonts, fontSize } from './customEditor'
// toolbar标题
export 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: '两端对齐' }
]
// 工具栏
export const container = [
['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: fontSize }], // 字体大小
[{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
[{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
[{ font: fonts }], // 字体种类
[{ lineheight: ['initial', '1', '1.5', '1.75', '2', '3', '4', '5'] }], // 自定义行高
[{ align: [] }], // 对齐方式
['clean'], // 清除文本格式
['link', 'image', 'video'] // 链接、图片、视频
]
// 改变图片大小
export const imageResize = {
displayStyles: {
backgroundColor: 'black',
border: 'none',
color: 'white'
},
modules: ['Resize', 'DisplaySize', 'Toolbar']
}
- 组件中使用
在项目组件中引用:
<template>
<div>
<quill-editor
ref="quillEditor"
v-model="content"
:options="editorOption"
@ready="quillOnReady($event)"
/>
</div>
</template>
<script>
import { quillEditor, Quill } from './utils/customEditor' // 上面js中已经定义
import { titleConfig, container, imageResize } from './utils/tools' // 上面js中已经定义
import { lineHeightStyle } from './utils/lineHeight'
export default {
name: 'RichTextEditor',
components: { quillEditor },}
},
data() {
return {
content: '',
editorOption: {
placeholder: '请在这里输入内容...',
theme: 'snow',
modules: {
toolbar: {
container,
handlers: {
video: (val) => {
// 劫持原来的视频点击按钮事件
document.querySelector('#uploadFileVideo').click()
},
image: (val) => {
// 劫持原来的图片点击按钮事件
if (val) {
document.querySelector('#uploadFileImg').click()
}
},
lineheight: (value) => {
if (value) {
const quill = this.$refs.quillEditor.quill
// console.log('quill======>', quill)
quill.format('lineHeight', value)
}
}
}
},
imageResize
}
}
},
mounted() {
// 给工具栏加中文提示
addToolbarTitle() {
for (const item of titleConfig) {
const tip = document.querySelector('.quill-editor ' + item.Choice)
if (!tip) continue
tip.setAttribute('title', item.title)
}
}
},
methods: {
// 图片/视频插入编辑器
insertEle(type, url) {
const quill = this.$refs.quillEditor.quill
const length = quill.getSelection().index
if (type === 'video') {
// 插入视频
quill.insertEmbed(length, 'simpleVideo', {
url: url.url,
controls: 'controls',
width: '100%',
height: 'auto',
poster: url.poster,
media_id: url.media_id // 自定义属性
})
} else {
// 插入图片
quill.insertEmbed(length, type, url)
}
// 调整光标到最后
quill.setSelection(length + 1)
},
changeImg(file) {
const fileObj = file.target.files[0]
this.insertEle('image', res.url) // 插入编辑器视频和图片的方法
},
quillOnReady() {
Quill.register({ 'formats/lineHeight': lineHeightStyle }, true)
}
}
}
</script>
> 以上是vue-quill-editor插件基本用法 以及如何自定义字体/字体大小/调整图片大小等
如需自定义其他还去专门配置,一下是自定义行高的配置
- 编写 lineHeight.js 并在组件中引用
// lineHeight.js
import { Quill } from 'vue-quill-editor'
const Parchment = Quill.import('parchment')
// 自定义行高
class LineHeightAttributor extends Parchment.Attributor.Style { }
const lineHeightStyle = new LineHeightAttributor('lineHeight', 'line-height', {
scope: Parchment.Scope.INLINE,
whitelist: ['1', '1.5', '1.75', '2', '3', '4', '5']
})
export { lineHeightStyle }
重点:不要忘记自定义css样式文件,否则以上自定义配置不生效!!!
.ql-snow .ql-picker.ql-header .ql-picker-label::before,
.ql-snow .ql-picker.ql-header .ql-picker-item::before {
content: '标题';
}
.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';
}
.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';
}
.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';
}
.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';
}
.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';
}
.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';
}
.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: '宋体';
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: '黑体';
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: '微软雅黑';
font-family: 'Microsoft YaHei';
}
.ql-snow .ql-picker.ql-font .ql-picker-label[data-value='YouYuan']::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value='YouYuan']::before {
content: '幼圆';
font-family: 'YouYuan';
}
.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: '楷体';
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: '仿宋';
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';
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';
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';
font-family: 'sans-serif';
}
.ql-font-SimSun {
font-family: 'SimSun';
}
.ql-font-SimHei {
font-family: 'SimHei';
}
.ql-font-YouYuan {
font-family: 'YouYuan';
}
.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-container {
font-size: 16px;
}
.ql-snow .ql-picker.ql-header {
width: 70px;
}
.ql-snow .ql-picker.ql-size, .ql-snow .ql-picker.ql-font {
width: 88px;
}
.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';
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';
font-size: 12px;
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value='14px']::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value='14px']::before {
content: '14px';
font-size: 14px;
}
.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';
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';
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';
font-size: 20px;
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value='24px']::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value='24px']::before {
content: '24px';
font-size: 24px;
}
.ql-snow .ql-picker.ql-size .ql-picker-label[data-value='28px']::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value='28px']::before {
content: '28px';
font-size: 28px;
}
.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';
font-size: 30px;
}
.ql-snow .ql-picker.ql-lineheight .ql-picker-label::before {
content: '行高';
}
.ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value='1']::before,
.ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value='1']::before {
content: '行高1';
}
.ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value='1.5']::before,
.ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value='1.5']::before {
content: '行高1.5';
}
.ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value='1.75']::before,
.ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value='1.75']::before {
content: '行高1.75';
}
.ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value='2']::before,
.ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value='2']::before {
content: '行高2';
}
.ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value='3']::before,
.ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value='3']::before {
content: '行高3';
}
.ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value='4']::before,
.ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value='4']::before {
content: '行高4';
}
.ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value='5']::before,
.ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value='5']::before {
content: '行高5';
}
.ql-snow .ql-picker.ql-lineheight {
width: 70px;
}