// 在main.js中添加实列
/* 富文本编辑器 */
import VueQuillEditor from 'vue-quill-editor';
import 'quill/dist/quill.core.css'; // import styles
import 'quill/dist/quill.snow.css'; // for snow theme
import 'quill/dist/quill.bubble.css'; // for bubble theme
Vue.use(VueQuillEditor /* { default global options } */);
在组件中配置富文本
// 引入Qill插件
import Quill from 'quill'
import quillEmoji from 'quill-emoji'
import 'quill-emoji/dist/quill-emoji.css'
Quill.register('modules/quillEmoji', quillEmoji)
// 自定义文字大小
const fontSize = [false, '10px', '12px', '14px', '16px', '18px', '20px', '24px']
var Font = Quill.imports['attributors/style/size']
Font.whitelist = fontSize
Quill.register(Font, true)
// 设置文本对齐方式
var Align = Quill.import('attributors/style/align')
Align.whitelist = ['right', 'center', 'justify'],
Quill.register(Align, true)
// 设置文本方向
var Direction = Quill.import('attributors/style/direction')
Direction.whitelist = ['rtl']
Quill.register(Direction, true)
// 源码中是import直接倒入,这里要用Quill.import引入
const Link = Quill.import('formats/link')
// 自定义a链接
class FileBlot extends Link {
// 继承Link Blot
static create(value) {
let node
if (value && !value.href) {
// 适应原本的Link Blot
node = super.create(value)
} else {
// 自定义Link Blot
node = super.create(value.href)
node.href = value.href
node.innerHTML = value.innerText
node.name = value.name
}
return node
}
}
FileBlot.blotName = 'link' // 这里不用改,如果需要也可以保留原来的,这里用个新的blot
FileBlot.tagName = 'A'
Quill.register(FileBlot) // 注册link
// 自定义首行缩进
const Block = Quill.import('blots/block')
class WordBox extends Block {
static create(value) {
const node = super.create(value)// 设置需要的样式
node.setAttribute('style', 'text-indent: 2em;')
return node
}
}
WordBox.blotName = 'wordBox' // 工具栏中的名字与此名字需保持一致
WordBox.tagName = 'div' // 需要添加的标签名,p标签的话无法取消样式export default { WordBox };
Quill.register(WordBox)// 注册div
const toolbarOptions = [
['wordBox', '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 }], // 字体大小-----[{ size: ['small', false, 'large', 'huge'] }]
[{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题-----[{ header: [1, 2, 3, 4, 5, 6, false] }]
[{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色-----[{ color: [] }, { background: [] }]
[{ font: [] }], // 字体种类-----[{ font: [] }]
[{ align: [] }], // 对齐方式-----[{ align: [] }]
['clean'], // 清除文本格式-----['clean']
['emoji'], // 表情包
['insertMetric', 'image', 'video'] // 链接、图片、视频-----['link', 'image', 'video']
]
在页面加载时初始化自定义内容
// 初始化自定义
let refs = this.toRef
let editorButton = document.querySelector('.' + refs + ' .ql-insertMetric')
editorButton.innerHTML = '<i class="el-icon-link" style="font-size: 18px;color:black"></i>'
let wordBox = document.querySelector('.' + refs + ' .ql-wordBox')
wordBox.innerHTML = '<i class="icons icon-wordBox"></i>'
设置鼠标悬浮时工具的提示
// toolbar标题
const titleConfig = [
{ Choice: '.ql-wordBox', title: '首行缩进' },
{ 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: '两端对齐' }
]
//在页面初始化时加载标题
this.$nextTick(() => {
for (const item of titleConfig) {
const tip = document.querySelector('.quill-editor ' + item.Choice)
if (!tip) continue
tip.setAttribute('title', item.title)
}
})
没有找到formats/xxx 转换style的方法,无奈手动将class样式替换成style
// 将class内容转换成sytle
const convertClassToStyle = (html) => {
let classObj = {
'ql-syntax': 'background-color: #23241f; color: #f8f8f2; overflow: visible;',
'ql-font-serif': 'font-family: Georgia, Times New Roman, serif',
'ql-font-monospace': 'font-family: Monaco, Courier New, monospace',
'ql-emojiblot': 'display: inline-block;vertical-align: text-top;',
'ql-video': 'display: block; max-width: 100%;'
};
let tempDiv = document.createElement('div');
tempDiv.innerHTML = html;
let elementsWithClass = tempDiv.querySelectorAll('[class]');
elementsWithClass.forEach((element) => {
let classValue = element.getAttribute('class');
let classNames = classValue.split(' ');
classNames.forEach((className) => {
let styleValue = '';
if (classObj[className]) {
styleValue = classObj[className];
} else if (className.indexOf('ql-indent-') > -1) {
let index = className.split('ql-indent-')[1]
styleValue = `padding-left: ${index * 3}em;`
if (elementsWithClass.tagName === 'LI') {
styleValue += `padding-left: ${index * 3 + 1.5}em;`;
}
}
let existingStyle = element.getAttribute('style') || '';
if (existingStyle || styleValue) {
element.setAttribute('style', existingStyle + styleValue);
}
});
});
return tempDiv.innerHTML
}
// 转换富文本内容
convertClassToStyle(this.newsText)
配置富文本编辑器
editorOption: {
// 富文本编辑器配
placeholder: '请输入',
theme: 'snow', // or 'bubble'
modules: {
'emoji-toolbar': true,
'emoji-shortname': true,
toolbar: {
container: toolbarOptions, // 工具栏
handlers: {
insertMetric: this.insertLink,
image: this.handleImageClick
}
}
}
}
// 设置自定义超链接内容
insertLink(data) {
// 获取富文本组件实例
const vm = this;
const quill = this.$refs[vm.toRef].quill
const { label, type, text } = data
const range = quill.getSelection(true)
quill.insertEmbed(range.index, 'link', { href: label, innerText: text, name: type }, 'api')
quill.setSelection(range.index + text.length) // 调整光标到最后
},
// 将图片内容添加到富文本
handleImageClick() {
const vm = this;
const handleFileChange = (event) => {
let fileList = event.target.files;
let fileItem = fileList[0]
// 此处调取接口获得图片地址
let { url } = await that.handleUploadImg(fileItem);
let imgHtml = `<img src="${vm.$webUrl + url}">`
const quill = vm.$refs[vm.toRef].quill
const range = quill.getSelection()
quill.clipboard.dangerouslyPasteHTML(range.index, imgHtml)
}
const input = document.createElement('input')
input.type = 'file'
input.accept = 'image/*'
input.multiple = true
input.onchange = (event) => handleFileChange(event)
input.click()
}