vue-quill-editor二次封裝,實現(xiàn)自定義文件上傳方式
更新時間:2024年03月07日 09:24:06 作者:jsmeng626
這篇文章主要介紹了vue-quill-editor二次封裝,實現(xiàn)自定義文件上傳方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
實現(xiàn)步驟
- 先將vue-quill-editor組件引入進(jìn)來,自定義配置,在自定義配置中添加upload操作,但是由于vue-quill-editor組件不支持文件上傳,該操作是無效的,邏輯需要自己去實現(xiàn)
- 給添加的upload按鈕自定義css樣式,文件上傳的圖標(biāo),大小等
- 模擬upload點擊事件,在editor的配置中,有一個handler對象,來處理自定義的upload點擊
- 點擊之后我們?nèi)プ屗|發(fā)element-ui中的文件上傳組件的上傳操作,所以我們將el-upload組件放在頁面上,然后用css隱藏起來,在點擊富文本中upload圖標(biāo)時,來模擬點擊el-upload,彈出上傳文件操作框
- 然后自定義el-upload的上傳方法,在上傳后的回調(diào)中去調(diào)用自己的接口,拿到一個文件路徑字符串
- 再將這個字符串通過quill對象插入到富文本中,也就是vue-quill-editor組件依賴的父對象,摻入a鏈接是通過繼承父類中的create方法來實現(xiàn)
- 其他不明白的可以看代碼中的注釋
完整代碼
1、quill-editor-upload自定義組件
<template> <div> <quill-editor ref="quillEditor" :options="editorOption" :content="content" @change="onEditorChange($event)" @blur="$emit('blur')"></quill-editor> <!-- 隱藏的upload --> <el-upload class="uploadFile" drag action="#" :http-request="uploadFile" :limit="1" :on-exceed="handleExceed" :on-remove="handleRemove" :file-list="fileList"> <i class="el-icon-upload"></i> <div class="el-upload__text"> 將文件拖到此處,或<em>點擊上傳</em> </div> </el-upload> </div> </template>
<script> import Quill from 'quill'; import { quillEditor } from "vue-quill-editor"; import "quill/dist/quill.core.css"; import "quill/dist/quill.snow.css"; import "quill/dist/quill.bubble.css"; import { editorUploadPath } from "@/api/excel/excel.js"; // 工具欄配置 const toolbarOptions = [ ['bold', 'italic', 'underline', 'strike'], // toggled buttons ['blockquote', 'code-block'], [{ 'header': 1 }, { 'header': 2 }], // custom button values [{ 'list': 'ordered' }, { 'list': 'bullet' }], [{ 'script': 'sub' }, { 'script': 'super' }], // superscript/subscript [{ 'indent': '-1' }, { 'indent': '+1' }], // outdent/indent [{ 'direction': 'rtl' }], // text direction [{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown [{ 'header': [1, 2, 3, 4, 5, 6, false] }], [{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme [{ 'font': [] }], [{ 'align': [] }], ['link', 'image', 'video', 'upload'], ['clean'] // remove formatting button ] // 自定義插入a鏈接 var Link = Quill.import('formats/link'); class FileBlot extends Link { // 繼承Link Blot static create(value) { let node = undefined if (value && !value.href) { // 適應(yīng)原本的Link Blot node = super.create(value); } else { // 自定義Link Blot node = super.create(value.href); node.setAttribute('download', value.innerText); // 左鍵點擊即下載 node.innerText = value.innerText; node.download = value.innerText; } return node; } } FileBlot.blotName = 'link'; FileBlot.tagName = 'A'; Quill.register(FileBlot); export default { name: "quill-editor-upload", components: { quillEditor }, // 自定義v-model,prop為props中的屬性,代表v-model傳入的值;event表示反向輸出給v-model的事件名 model: { prop: 'content', event: 'changeContent' }, // 自定義v-model, v-model傳入的值 props: { content: { type: String, default: '' } }, data() { return { fileList: [], // 文件列表 // editor配置 editorOption: { placeholder: '請輸入正文......', modules: { toolbar: { container: toolbarOptions, // 工具欄 handlers: { // 模擬upload按鈕點擊事件 'upload': (value => { if (value) { document.querySelector('.uploadFile input').click() } }) } } } } } }, methods: { // 自定義v-model,將editor選擇器change事件提供的html傳出去 onEditorChange(e) { this.$emit('changeContent', e.html) }, // 自定義文件上傳 uploadFile(res) { // console.log('上傳成功', res); let myFormData = new FormData(); myFormData.append("file", res.file); editorUploadPath(myFormData).then((response) => { if (response.code === 0) { let fileNameLength = res.file.name.length // 插入鏈接 let quill = this.$refs.quillEditor.quill let length = quill.getSelection().index; quill.insertEmbed(length, 'link', { href: response.msg, innerText: res.file.name }, "api") quill.setSelection(length + fileNameLength) this.fileList = [] } }); }, // 文件超出限制提示 handleExceed() { this.$message.warning(`當(dāng)前限制選擇 1 個文件,請刪除后更新!`); }, // 移除文件 handleRemove() { this.fileList = []; }, } } </script>
<style scoped> /* 文件上傳圖標(biāo)樣式 */ /deep/ .quill-editor .ql-snow.ql-toolbar .ql-formats .ql-upload { background: url("../assets/file.png"); background-size: 16px 16px; background-position: center center; background-repeat: no-repeat; } /* 隱藏upload上傳,用模擬事件觸發(fā) */ .uploadFile { width: 0; height: 0; display: none; } </style>
2、使用該組件
<el-form-item label="內(nèi)容" prop="content"> <quillEditorUpload v-model="form.content"></quillEditorUpload> </el-form-item>
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue-cli?npm如何解決vue項目中缺失core-js的問題
這篇文章主要介紹了vue-cli?npm如何解決vue項目中缺失core-js的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Pinia入門學(xué)習(xí)之實現(xiàn)簡單的用戶狀態(tài)管理
Vue3雖然相對于Vue2很多東西都變了,但是核心的東西還是沒有變,比如說狀態(tài)管理、路由等,再Vue3中尤大神推薦我們使用pinia來實現(xiàn)狀態(tài)管理,他也說pinia就是Vuex的新版本,這篇文章主要給大家介紹了關(guān)于Pinia入門學(xué)習(xí)之實現(xiàn)簡單的用戶狀態(tài)管理的相關(guān)資料,需要的朋友可以參考下2022-11-11vue-quill-editor富文本編輯器超詳細(xì)入門使用步驟
vue中很多項目都需要用到富文本編輯器,在使用了ueditor和tinymce后,發(fā)現(xiàn)并不理想,所以果斷使用vue-quill-editor來實現(xiàn),下面這篇文章主要給大家介紹了關(guān)于vue-quill-editor富文本編輯器入門使用步驟的相關(guān)資料,需要的朋友可以參考下2022-08-08