前端学习案例-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'// 配置服务器端地址
          // 自定义 header
          this. editor.config.uploadVideoHeaders = {
            'Authorization': `Bearer ` + token
          }
          this.editor.config.uploadVideoName = 'file'//后端接收上传文件的参数名(这是配置视频)
     
     
          this.editor.config.uploadImgServer = '.com/api/v1/upload/picture'// 配置服务器端地址
          // 自定义 header
          this.editor.config.uploadImgHeaders = {
            'Authorization': `Bearer ` + token
          }
          this.editor.config.uploadFileName = 'file' // 后端接受上传文件的参数名(这是配置图片)
          this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024 // 将图片大小限制为 2M
          this.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.data
              console.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 : '.....'} } 这种格式,可使用 customInsert
            customInsert: 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使用有效 放弃很容易 但是坚持一定很酷
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值