wangEditor 修改 “視訊”選單 的實現方式,達到上傳視訊的功能---精簡版
阿新 • • 發佈:2019-01-07
/* menu - video */ // 建構函式 function Video(editor) { this.editor = editor; this.$elem = $('<div class="w-e-menu"><i class="w-e-icon-play"><i/></div>'); this.type = 'panel'; // 當前是否 active 狀態 this._active = false; } // 原型 Video.prototype = { constructor: Video, onClick: function onClick() { this._createInsertPanel(); }, _createInsertPanel: function _createInsertPanel() { var editor = this.editor; var uploadVideo = editor.uploadVideo; var config = editor.config; // id var upTriggerId = getRandom('up-trigger'); var upFileId = getRandom('up-file'); // tabs 的配置 var tabsConfig = [{ title: '上傳 video', tpl: '<div class="w-e-up-img-container">\n ' + '<div id="' + upTriggerId + '" class="w-e-up-btn">\n ' + '<i class="w-e-icon-upload2"></i>\n </div>\n ' + '<div style="display:none;">\n <input id="' + upFileId + '" type="file" multiple="multiple" accept="audio/mp4, video/mp4"/>\n ' + '</div>\n </div>', events: [{ // 觸發選擇視訊 selector: '#' + upTriggerId, type: 'click', fn: function fn() { var $file = $('#' + upFileId); var fileElem = $file[0]; if (fileElem) { fileElem.click(); } else { // 返回 true 可關閉 panel return true; } } }, { // 選擇視訊完畢 selector: '#' + upFileId, type: 'change', fn: function fn() { var $file = $('#' + upFileId); var fileElem = $file[0]; if (!fileElem) { // 返回 true 可關閉 panel return true; } // 獲取選中的 file 物件列表 var fileList = fileElem.files; if (fileList.length) { uploadVideo.uploadVideo(fileList); } // 返回 true 可關閉 panel return true; } }] } ]; // tabs end // 判斷 tabs 的顯示 var tabsConfigResult = []; tabsConfigResult.push(tabsConfig[0]); // 建立 panel 並顯示 var panel = new Panel(this, { width: 300, tabs: tabsConfigResult }); panel.show(); // 記錄屬性 this.panel = panel; }, // 試圖改變 active 狀態 tryChangeActive: function tryChangeActive(e) { var editor = this.editor; var $elem = this.$elem; if (editor._selectedImg) { this._active = true; $elem.addClass('w-e-active'); } else { this._active = false; $elem.removeClass('w-e-active'); } } }; /* 所有選單的彙總 */ // 儲存選單的建構函式 var MenuConstructors = {}; MenuConstructors.video = Video; /* 上傳視訊 */ // 建構函式 function UploadVideo(editor) { this.editor = editor; } // 原型 UploadVideo.prototype = { constructor: UploadVideo, // 根據 debug 彈出不同的資訊 _alert: function _alert(alertInfo, debugInfo) { var editor = this.editor; var debug = editor.config.debug; // var debug = true; var customAlert = editor.config.customAlert; if (debug) { throw new Error('wangEditor: ' + (debugInfo || alertInfo)); } else { if (customAlert && typeof customAlert === 'function') { customAlert(alertInfo); } else { alert(alertInfo); } } }, // 上傳視訊 uploadVideo: function uploadVideo(files) { var _this3 = this; if (!files || !files.length) { return; } // ------------------------------ 獲取配置資訊 ------------------------------ var editor = this.editor; var config = editor.config; var uploadImgServer = "/file/upload"; // var uploadImgShowBase64 = config.uploadImgShowBase64; var maxSize = 100 * 1024 * 1024; //100M var maxSizeM = maxSize / 1000 / 1000; var maxLength = 1; var uploadFileName = "file"; var uploadImgParams = config.uploadImgParams || {}; // var uploadImgParamsWithUrl = config.uploadImgParamsWithUrl; var uploadImgHeaders = {}; var hooks = config.uploadImgHooks || {}; var timeout = 5 * 60 * 1000; //5 min var withCredentials = config.withCredentials; if (withCredentials == null) { withCredentials = false; } // ------------------------------ 驗證檔案資訊 ------------------------------ var resultFiles = []; var errInfo = []; arrForEach(files, function (file) { var name = file.name; var size = file.size; // chrome 低版本 name === undefined if (!name || !size) { return; } if (/\.(mp4)$/i.test(name) === false) { // 字尾名不合法,不是圖片 errInfo.push('\u3010' + name + '\u3011\u4E0D\u662F\u56FE\u7247'); return; } if (maxSize < size) { // 上傳圖片過大 errInfo.push('\u3010' + name + '\u3011\u5927\u4E8E ' + maxSizeM + 'M'); return; } // 驗證通過的加入結果列表 resultFiles.push(file); }); // 丟擲驗證資訊 if (errInfo.length) { this._alert('視訊驗證未通過: \n' + errInfo.join('\n')); return; } if (resultFiles.length > maxLength) { this._alert('一次最多上傳' + maxLength + '個視訊'); return; } // ------------------------------ 自定義上傳 ------------------------------ // 新增視訊資料 var formdata = new FormData(); arrForEach(resultFiles, function (file) { var name = uploadFileName || file.name; formdata.append(name, file); }); // ------------------------------ 上傳視訊 ------------------------------ if (uploadImgServer && typeof uploadImgServer === 'string') { // 新增引數 var uploadImgServerArr = uploadImgServer.split('#'); uploadImgServer = uploadImgServerArr[0]; var uploadImgServerHash = uploadImgServerArr[1] || ''; objForEach(uploadImgParams, function (key, val) { val = encodeURIComponent(val); // 第一,將引數拼接到 url 中 if (uploadImgParamsWithUrl) { if (uploadImgServer.indexOf('?') > 0) { uploadImgServer += '&'; } else { uploadImgServer += '?'; } uploadImgServer = uploadImgServer + key + '=' + val; } // 第二,將引數新增到 formdata 中 formdata.append(key, val); }); if (uploadImgServerHash) { uploadImgServer += '#' + uploadImgServerHash; } // 定義 xhr var xhr = new XMLHttpRequest(); xhr.open('POST', uploadImgServer); // 設定超時 xhr.timeout = timeout; xhr.ontimeout = function () { // hook - timeout if (hooks.timeout && typeof hooks.timeout === 'function') { hooks.timeout(xhr, editor); } _this3._alert('上傳圖片超時'); }; // 監控 progress if (xhr.upload) { xhr.upload.onprogress = function (e) { var percent = void 0; // 進度條 var progressBar = new Progress(editor); if (e.lengthComputable) { percent = e.loaded / e.total; progressBar.show(percent); } }; } // 返回資料 xhr.onreadystatechange = function () { var result = void 0; if (xhr.readyState === 4) { if (xhr.status < 200 || xhr.status >= 300) { // hook - error if (hooks.error && typeof hooks.error === 'function') { hooks.error(xhr, editor); } // xhr 返回狀態錯誤 _this3._alert('上傳視訊發生錯誤', '\u4E0A\u4F20\u56FE\u7247\u53D1\u751F\u9519\u8BEF\uFF0C\u670D\u52A1\u5668\u8FD4\u56DE\u72B6\u6001\u662F ' + xhr.status); return; } result = xhr.responseText; if ((typeof result === 'undefined' ? 'undefined' : _typeof(result)) !== 'object') { try { result = JSON.parse(result); } catch (ex) { // hook - fail if (hooks.fail && typeof hooks.fail === 'function') { hooks.fail(xhr, editor, result); } _this3._alert('上傳視訊失敗', '上傳視訊返回結果錯誤,返回結果是: ' + result); return; } } if (!hooks.customInsert && result.errno != '0') { // hook - fail if (hooks.fail && typeof hooks.fail === 'function') { hooks.fail(xhr, editor, result); } // 資料錯誤 _this3._alert('上傳視訊失敗', '上傳視訊返回結果錯誤,返回結果 errno=' + result.errno); } else { if (hooks.customInsert && typeof hooks.customInsert === 'function') { // 使用者自定義插入方法 // var _video_src = ' <video class="video-js" controls preload="auto" data-setup="{}"><source src="' + result.obj + '" type="video/mp4"></video>' editor.cmd.do('insertHTML', '<video src="' + result.obj + '" style="max-width: 50%;max-height:50%;" controls autobuffer />'); _this3._alert("upload successfully") // hooks.customInsert(_this3.insertLinkImg.bind(_this3), result, editor); } else { // 將圖片插入編輯器 var data = result.data || []; data.forEach(function (link) { _this3.insertLinkImg(link); }); } // hook - success if (hooks.success && typeof hooks.success === 'function') { hooks.success(xhr, editor, result); } } } }; // hook - before if (hooks.before && typeof hooks.before === 'function') { var beforeResult = hooks.before(xhr, editor, resultFiles); if (beforeResult && (typeof beforeResult === 'undefined' ? 'undefined' : _typeof(beforeResult)) === 'object') { if (beforeResult.prevent) { // 如果返回的結果是 {prevent: true, msg: 'xxxx'} 則表示使用者放棄上傳 this._alert(beforeResult.msg); return; } } } // 自定義 headers objForEach(uploadImgHeaders, function (key, val) { xhr.setRequestHeader(key, val); }); // 跨域傳 cookie xhr.withCredentials = withCredentials; // 傳送請求 xhr.send(formdata); // 注意,要 return 。不去操作接下來的 base64 顯示方式 return; } } }; // 修改原型 Editor.prototype = { constructor: Editor, // 新增視訊上傳 _initUploadVideo: function _initUploadVideo() { this.uploadVideo = new UploadVideo(this); }, // 建立編輯器 create: function create() { // 新增 視訊上傳 this._initUploadVideo(); }, };
這是精簡的,只有 video ,完整的版本連結:http://blog.csdn.net/yong472727322/article/details/79564328