quill-editor 富文本插件
使用
一、npm 安裝 vue-quill-editor
二、在main.js中引入
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>
<quill-editor
v-model="content"
ref="myQuillEditor"
:options="editorOption"
@blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
@change="onEditorChange($event)">
</quill-editor>
</template>
<script>
import { quillEditor } from 'vue-quill-editor'
export default{
data(){
const toolbarOptions = [['bold', 'italic', 'underline', 'image']];
return {
content:null,
editorOption:{
modules:{
toolbar:toolbarOptions,
theme:'snow',
}
}
}
},
methods:{
onEditorBlur(){//失去焦點事件
},
onEditorFocus(){//獲得焦點事件
},
onEditorChange(){//內容改變事件
}
}
}
</script>
全部工具欄
const toolbarOptions = [
['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'] //上傳圖片、上傳視頻
];
更改圖片按鈕的事件函數
由於quill插件默認插入圖片是base64格式的,若圖片較大會引起內容太大無法保存到數據庫里面,這樣為了上傳圖片或者插入網絡圖片,要先更改圖片按鈕的事件下函數,具體是在editorOption->modules->toolbar中加入handlers對象,並插入名稱為image的函數,函數的內容設定為自身所需內容,如下
data () {
return {
content: '',
editorOption: {
...
modules: {
toolbar: {
container: toolbarOptions,
handlers: {
'image': function (value) {
if (value) {
// 自定義內容
} else {
this.quill.format('image', false)
}
}
}
}
}
}
}
修改配置,使圖片能夠上傳到服務器
富文本編輯器中的圖片上傳是將圖片轉為base64格式的,如果需要上傳圖片到服務器,需要修改配置。
創建一個quill-config文件
/*富文本編輯圖片上傳配置*/
const uploadConfig = {
action: '', // 必填參數 圖片上傳地址
methods: 'POST', // 必填參數 圖片上傳方式
token: '', // 可選參數 如果需要token驗證,假設你的token有存放在sessionStorage
name: 'img', // 必填參數 文件的參數名
size: 500, // 可選參數 圖片大小,單位為Kb, 1M = 1024Kb
accept: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon' // 可選 可上傳的圖片格式
};
// toolbar工具欄的工具選項(默認展示全部)
const toolOptions = [
['bold', 'italic', 'underline', 'strike'],
['blockquote', 'code-block'],
[{'header': 1}, {'header': 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'],
['link', 'image', 'video']
];
const handlers = {
image: function image() {
var self = this;
var fileInput = this.container.querySelector('input.ql-image[type=file]');
if (fileInput === null) {
fileInput = document.createElement('input');
fileInput.setAttribute('type', 'file');
// 設置圖片參數名
if (uploadConfig.name) {
fileInput.setAttribute('name', uploadConfig.name);
}
// 可設置上傳圖片的格式
fileInput.setAttribute('accept', uploadConfig.accept);
fileInput.classList.add('ql-image');
// 監聽選擇文件
fileInput.addEventListener('change', function () {
// 創建formData
var formData = new FormData();
formData.append(uploadConfig.name, fileInput.files[0]);
formData.append('object','product');
// 如果需要token且存在token
if (uploadConfig.token) {
formData.append('token', uploadConfig.token)
}
// 圖片上傳
var xhr = new XMLHttpRequest();
xhr.open(uploadConfig.methods, uploadConfig.action, true);
// 上傳數據成功,會觸發
xhr.onload = function (e) {
if (xhr.status === 200) {
var res = JSON.parse(xhr.responseText);
let length = self.quill.getSelection(true).index;
//這里很重要,你圖片上傳成功后,img的src需要在這里添加,res.path就是你服務器返回的圖片鏈接。
self.quill.insertEmbed(length, 'image', res.path);
self.quill.setSelection(length + 1)
}
fileInput.value = ''
};
// 開始上傳數據
xhr.upload.onloadstart = function (e) {
fileInput.value = ''
};
// 當發生網絡異常的時候會觸發,如果上傳數據的過程還未結束
xhr.upload.onerror = function (e) {
};
// 上傳數據完成(成功或者失敗)時會觸發
xhr.upload.onloadend = function (e) {
// console.log('上傳結束')
};
xhr.send(formData)
});
this.container.appendChild(fileInput);
}
fileInput.click();
}
};
export default {
placeholder: '',
theme: 'snow', // 主題
modules: {
toolbar: {
container: toolOptions, // 工具欄選項
handlers: handlers // 事件重寫
}
}
};
引用
<template>
<div id="Test">
<quill-editor ref="myQuillEditor" v-model="content" :options="quillOption">
</quill-editor>
</div>
</template>
<script>
import { quillEditor } from 'vue-quill-editor'
import quillConfig from './quill-config.js'
export default {
components: {
quillEditor
},
data () {
return {
content: '<h1>hello quill-editor</h1>',
quillOption: quillConfig,
}
}
}
</script>
<style>
</style>