這篇文章主要介紹vue-quill-editor+plupload富文本編輯器的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,先為富順等服務(wù)建站,富順等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為富順企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
1,先給vue項(xiàng)目中下載vue-quill-editor
依賴npm install vue-quill-editor --save
2,下載plupload依賴npm install plupload --save
3,在組件中分別引入對應(yīng)的js
import { quillEditor }from 'vue-quill-editor' import '@/assets/js/crypto1/crypto/crypto.js' import '@/assets/js/crypto1/hmac/hmac.js' import '@/assets/js/crypto1/sha1/sha1.js' import Base64from '@/assets/js/base64.js' import pluploadfrom 'plupload'
4,編寫plupload上傳圖片需要的事件以及參數(shù)
let accessid = '阿里oss申請的accessid' let accesskey = '阿里oss申請的accesskey' let host = '阿里oss的存儲(chǔ)文件地址' let bucket = 'image' let g_dirname = '' let g_object_name = '' let g_object_name_type = '' var timestamp let now = timestamp = Date.parse(new Date())/ 1000 let pos = '' let suffix = '' var policyText = { 'expiration': '2020-01-01T12:00:00.000Z',// 設(shè)置該P(yáng)olicy的失效時(shí)間,超過這個(gè)失效時(shí)間之后,就沒有辦法通過這個(gè)policy上傳文件了 'conditions': [ ['content-length-range',0,1048576000]// 設(shè)置上傳文件的大小限制 ] } var policyBase64 = Base64.encode(JSON.stringify(policyText)) let message = policyBase64 var bytes = Crypto.HMAC(Crypto.SHA1,message,accesskey, {asBytes: true}) var signature = Crypto.util.bytesToBase64(bytes) // 選擇上傳名字是本地文件名字還是隨機(jī)文件名字 function check_object_radio () { g_object_name_type = 'random_name' } // 默認(rèn)是上傳到根目錄 function get_dirname () { g_dirname = 'image/' } // 獲得隨機(jī)的字符串 function random_string (len) { len = len || 32 var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678' var maxPos = chars.length var pwd = '' for (var i = 0;i < len;i++) { pwd += chars.charAt(Math.floor(Math.random()* maxPos)) } return pwd } // 獲取文件后綴 function get_suffix (filename) { pos = filename.lastIndexOf('.') suffix = '' if (pos !== -1) { suffix = filename.substring(pos) } return suffix } // 獲取文件名字 function calculate_object_name (filename) { if (g_object_name_type === 'local_name') { g_object_name += filename }else if (g_object_name_type === 'random_name') { suffix = get_suffix(filename) g_object_name = random_string(10)+ new Date().getTime()+ suffix // g_object_name = filename } return '' } function get_uploaded_object_name (filename) { if (g_object_name_type === 'local_name') { var tmp_name = g_object_name tmp_name = tmp_name.replace(filename,filename) return tmp_name }else if (g_object_name_type === 'random_name') { return g_object_name } } // 設(shè)置上傳參數(shù) function set_upload_param (up,filename,ret) { // g_object_name = g_dirname if (filename !== '') { suffix = get_suffix(filename) calculate_object_name(filename) } let new_multipart_params = { 'Filename': g_dirname + g_object_name, 'key': g_dirname + g_object_name, 'policy': policyBase64, 'OSSAccessKeyId': accessid, 'success_action_status': '200',// 讓服務(wù)端返回200,不然,默認(rèn)會(huì)返回204 'signature': signature } console.log(g_object_name) up.setOption({ 'url': host, 'multipart_params': new_multipart_params }) up.start() } 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'], ['clean']// remove formatting button ] export default { name: 'AddNotice', components: { quillEditor }, data () { return { loading: false, textarea_name: '', textarea_nei: '', content: null, editorOption:{ placeholder: '', theme: 'snow',// or 'bubble' modules: { toolbar: { container: toolbarOptions,// 工具欄 handlers: { 'image': function (value) { if (value) { // alert(1) document.querySelector('#selectfiles').click() }else { this.quill.format('image',false); } } } } } } } }, props: { Notice_id: Number, AddNotice_id: Number }, watch: {}, methods: { data_qing () { this.textarea_name = '' this.content = '' }, AddNoticeOff () { this.$emit('AddNoticeOff') }, gonggao_push () { this.loading = true let data = { token: this.token(), id: this.AddNotice_id, match_id: this.Notice_id, title: this.textarea_name, content: this.content } this.$axios.post('/match/Notice/NoticeSave',this.$qs.stringify(data)) .then((res)=> { this.loading = false console.log(res) if (res.data.code === '200') { this.$emit('AddNoticeOff') this.$emit('notice_ajax',0) } }).catch((err)=> { this.loading = false console.log(err) }) }, escape2Html (str) { var arrEntities = {'lt': '<','gt': '>','nbsp': ' ','amp': '&','quot': '"'} return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function (all,t) { return arrEntities[t] }) }, data_zhan () { let data = { token: this.token(), id: this.AddNotice_id } this.$axios.post('/match/Notice/NoticeInfo',this.$qs.stringify(data)) .then((res)=> { let new_data = res.data.data if (res.data.code === '200') { this.textarea_name = new_data.title this.content = this.escape2Html(new_data.content) console.log(this.escape2Html(new_data.content)) } }).catch((err)=> { console.log(err) }) } }, computed: { }, created () { }, mounted () { var that = this var uploader = new plupload.Uploader({ runtimes: 'html5,flash,silverlight,html4', browse_button: 'selectfiles', multi_selection: true, // container: document.getElementById('container'), flash_swf_url: './../assets/plupload-2.1.2/js/Moxie.swf', silverlight_xap_url: './../assets/plupload-2.1.2/js/Moxie.xap', url: host, init: { PostInit: function () { console.log(7777) }, QueueChanged: function (up) {// 數(shù)組變化是發(fā)生 console.log(up) }, FileFiltered: function (up,files) { var fileSize = (Math.round(files.size * 100 / (1024 * 1024))/ 100).toString()// MB if (fileSize > 10) { uploader.removeFile(files) } }, // 圖片成功添加到上傳隊(duì)列中后的事件 FilesAdded: function (up,files) { console.log(111) plupload.each(files,function (v,i) { }) console.log(files) set_upload_param(uploader,'',false) }, BeforeUpload: function (up,file) { console.log(3333) check_object_radio() get_dirname() set_upload_param(up,file.name,true) }, UploadProgress: function (up,file) { console.log(file.percent)// 進(jìn)度條設(shè)置 }, FileUploaded: function (up,file,info) { console.log(555) // 獲取富文本組件實(shí)例 console.log(that.$refs) let quill = that.$refs.myQuillEditor.quill if (info.status === 200) { console.log(get_uploaded_object_name(file.name)) let url = host+'/'+g_dirname+get_uploaded_object_name(file.name) // 獲取光標(biāo)所在位置 let length = quill.getSelection().index; // 插入圖片 res.info為服務(wù)器返回的圖片地址 quill.insertEmbed(length,'image',url) // 調(diào)整光標(biāo)到最后 quill.setSelection(length + 1) }else { this.$message.error('圖片插入失敗') } }, Error: function (up,err) { // document.getElementById('console').appendChild(document.createTextNode("\nError xml:" + err.response)) } } }) uploader.init() }, destroyed () { } }
以上是“vue-quill-editor+plupload富文本編輯器的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享標(biāo)題:vue-quill-editor+plupload富文本編輯器的示例分析
當(dāng)前路徑:http://www.rwnh.cn/article6/ghcpog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站排名、網(wǎng)站營銷、自適應(yīng)網(wǎng)站、標(biāo)簽優(yōu)化、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)