WangEdit富文本编辑器增加上传视频功能
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                WangEdit富文本编辑器增加上传视频功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                乘著今天中午的時間 對以前項目的一個需求進行一定的處理
前天去了甲方公司
接到了了一個新的需求
就是可以把項目的一個富文本的編輯器可以設置為能夠上傳視頻
于是乎
就要對vue里面的這個組件進行操作了
首先我們可以看一眼官網的文檔
需要用到的就直接到官網文檔進行查詢即可了
?需求(接口文檔)
?
由于官網的的文檔是對原生js進行說明的
所以vue的寫法就可以這樣先寫了、
代碼部分(封裝子組件)
<template lang="html"><div class="editor"><!--定義的為表頭的屬性--><div ref="toolbar" class="toolbar"></div><!--定義的為表格的屬性--><div ref="editor" class="text" ></div></div> </template> <script> /*引入王edit插件*/ import E from 'wangeditor' import {ACCESS_TOKEN} from "@/store/KeyConstants"; /*開始引入Vue的模塊*/ import Vue from "vue"; export default {name: 'EditorBar',data() {return {editor: null,info_: null,UploadVidio:'',}},model: {prop: 'value',event: 'change'},props: {value: {type: String,default: ''},isClear: {type: Boolean,default: false},setMode:{type:Boolean,request:true}},watch: {isClear(val) {// 觸發清除文本域內容if (val) {this.editor.txt.clear()this.info_ = null}},value: function(value) {if (value !== this.editor.txt.html()) {this.editor.txt.html(this.value)}},//value為編輯框輸入的內容,這里我監聽了一下值,當父組件調用得時候,如果給value賦值了,子組件將會顯示父組件賦給的值},created() {/*控制上傳圖片的邏輯*/this.$nextTick(()=>{this.seteditor()this.editor.txt.html(this.value)})},methods: {seteditor() {const token = Vue.ls.get(ACCESS_TOKEN);/*選中對應的元素*/this.editor = new E(this.$refs.toolbar, this.$refs.editor)/*設置存儲照片的格式*/// this.editor.config.uploadImgShowBase64 = true // base 64 存儲圖片this.editor.config.uploadVideoServer = 't.com/api/v1/upload/picture'// 配置服務器端地址// 自定義 headerthis. editor.config.uploadVideoHeaders = {'Authorization': `Bearer ` + token}this.editor.config.uploadVideoName = 'file'//后端接收上傳文件的參數名(這是配置視頻)this.editor.config.uploadImgServer = '.com/api/v1/upload/picture'// 配置服務器端地址// 自定義 headerthis.editor.config.uploadImgHeaders = {'Authorization': `Bearer ` + token}this.editor.config.uploadFileName = 'file' // 后端接受上傳文件的參數名(這是配置圖片)this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024 // 將圖片大小限制為 2Mthis.editor.config.uploadImgMaxLength = 6 // 限制一次最多上傳 3 張圖片this.editor.config.uploadImgTimeout = 3 * 60 * 1000 // 設置超時時間// 配置菜單this.editor.config.menus = ['head', // 標題'bold', // 粗體'fontSize', // 字號'fontName', // 字體'italic', // 斜體'underline', // 下劃線'strikeThrough', // 刪除線'foreColor', // 文字顏色'backColor', // 背景顏色'link', // 插入鏈接'list', // 列表'justify', // 對齊方式'quote', // 引用'emoticon', // 表情'image', // 插入圖片'table', // 表格'video', // 插入視頻'code', // 插入代碼'undo', // 撤銷'redo', // 重復'fullscreen' // 全屏]//上傳圖片的回調this.editor.config.uploadImgHooks = {fail: (xhr, editor, result) => {// 插入圖片失敗回調},success: (xhr, editor, result) => {// 圖片上傳成功回調},timeout: (xhr, editor) => {// 網絡超時的回調},error: (xhr, editor) => {// 圖片上傳錯誤的回調},customInsert: (insertImg, result, editor) => {// 圖片上傳成功,插入圖片的回調//result為上傳圖片成功的時候返回的數據,這里我打印了一下發現后臺返回的是data:[{url:"路徑的形式"},...]//insertImg()為插入圖片的函數//循環插入圖片// for (let i = 0; i < 1; i++) {// postAction('/content/picture',{file})let url = result.dataconsole.log(url)insertImg(url)// }}}this.editor.config.onchange = (html) => {this.info_ = html // 綁定當前逐漸地值this.$emit('ChangePicture', this.info_) // 將內容同步到父組件中}// 視頻上傳 /* this.editor.config.uploadVideoServer ="http://otp.cdinfotech.top/file/upload_images"; // 上傳接口this.editor.config.uploadVideoParams = {"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundarycZm1pHksXeHS6t5r"}*/ /* this.editor.config.uploadVideoHooks = {// 上傳完成處理方法customInsert: function (insertVideo, result) {if (result.code == 0) {(result.data || "").split(",").forEach(function (link) {link && insertVideo(link);});} else {/!* flavrShowByTime("上傳失敗", null, "danger");*!/}},};*///上傳視頻的回調this.editor.config.uploadVideoHooks = {// 上傳視頻之前 /* before: function(xhr) {console.log(xhr)// 可阻止視頻上傳return {prevent: true,msg: '需要提示給用戶的錯誤信息'}},*/// 視頻上傳并返回了結果,視頻插入已成功success: function(xhr) {console.log('success', xhr)},// 視頻上傳并返回了結果,但視頻插入時出錯了fail: function(xhr, editor, resData) {console.log('fail', resData)},// 上傳視頻出錯,一般為 http 請求的錯誤error: function(xhr, editor, resData) {console.log('error', xhr, resData)},// 上傳視頻超時timeout: function(xhr) {console.log('timeout')},// 視頻上傳并返回了結果,想要自己把視頻插入到編輯器中// 例如服務器端返回的不是 { errno: 0, data: { url : '.....'} } 這種格式,可使用 customInsertcustomInsert: function(insertVideoFn, result) {// result 即服務端返回的接口console.log('customInsert', result)// insertVideoFn 可把視頻插入到編輯器,傳入視頻 src ,執行函數即可//result.data 就是url的值insertVideoFn(result.data)}}this.editor.create()}} } </script><style lang="css"> .editor {width: 100%;margin: 0 auto;position: relative;z-index: 0; } .toolbar {border: 1px solid #ccc; } .text {border: 1px solid #ccc;min-height: 500px; } </style>這邊直接上代碼 以后用到富文本可以直接使用
就很nice
?我是歌謠 這部分支隊wangEdit使用有效 放棄很容易 但是堅持一定很酷
總結
以上是生活随笔為你收集整理的WangEdit富文本编辑器增加上传视频功能的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 学习 jQuery 源码整体架构,打造属
- 下一篇: oracle 数据库er生成,oracl
