index.vue 5.09 KB
<template>
    <div>
      <div id="div-demo"></div>
    </div>
  </template>
  <script>
  import E from 'wangeditor'
  export default {
    name: 'textBox',
    data() {
      return {
        editor: '',
        content: '',
        editorOption: {}
      }
    },
    props: {
      excludeMenus: {
        type: Array,
        default: () => ['code']
      },
      boxHeight:{
        type: Number,
        default: 240
      }
    },
    mounted() {
      this.getEdit()
    },
    methods: {
      handleEditorClick(e) {
        const target = e.target;
        console.log(target.nodeName)
        if (target.nodeName === 'VIDEO') {
          // 视频被点击,移动光标到视频后面
          // const range = document.createRange();
          // const selection = window.getSelection();
          // range.setStartAfter(target); // 设置范围的起点为视频元素之后
          // range.collapse(true); // 折叠范围
          // selection.removeAllRanges(); // 清除所有范围
          // selection.addRange(range); // 添加新范围
        }
      },
      getEdit() {
        const _this = this
        this.editor = new E('#div-demo')
        this.editor.config.uploadImgShowBase64 = true
        this.editor.config.showMenuTooltips = true
        this.editor.config.menuTooltipPosition = 'down'
        this.editor.config.height = this.boxHeight
        this.editor.config.width = 500
        this.editor.config.excludeMenus = this.excludeMenus
        this.editor.config.pasteIgnoreImg = true
        // this.editor.config.uploadImgHooks = {
        //   before: (xhr, editer, files) => {
        //     if(files[0].name === 'image.png') {
        //       return {
        //         prevent: true,
        //         msg: '请您将截图以上传图片的方式显示在文本框中!'
        //       }
        //     }
        //   }
        // }
        this.editor.config.onchange = html => {
          // console.log(html);
          this.content = html // 绑定当前逐渐地值
          this.$emit('change', this.content) // 将内容同步到父组件中
        }
        this.editor.config.onfocus = () => {
          this.$emit('focus')
        }
        // 上传图片
        this.editor.config.customUploadImg = function(resultFiles, insertImgFn) {
          var file = resultFiles[0]
          var reader = new FileReader()
          // 默认1MB
          const limitSize = 10
          const isLt = file.size / 1024 / 1024 < limitSize
          if (!isLt) {
            _this.$message.error('上传图片大小不能超过' + limitSize + 'MB!')
            return true
          }
          reader.readAsDataURL(file)
          reader.onload = e => {
            const imgFile = e.target.result
            const base64 = file.name.split('.')[file.name.split('.').length - 1] + '@' + imgFile.split(',')[1]
            _this.$apis.common.pictureUpload({ file: base64 }).then(res => {
              if (res) {
                // console.log(res)
                insertImgFn(res)
              }
            })
            // resultFiles 是 input 中选中的文件列表
            // insertImgFn 是获取图片 url 后,插入到编辑器的方法
            // 上传图片,返回结果,将图片插入到编辑器中
          }
        }
        // 上传视频
        this.editor.config.customUploadVideo = function(resultFiles, insertVideoFn) {
          var file = resultFiles[0]
          var reader = new FileReader()
          // 默认1MB
          const limitSize = 10
          const isLt = file.size / 1024 / 1024 < limitSize
          if (!isLt) {
            _this.$message.error('上传视频大小不能超过' + limitSize + 'MB!')
            return true
          }
          reader.readAsDataURL(file)
          reader.onload = e => {
            const videoFile = e.target.result
            const base64 = file.name.split('.')[file.name.split('.').length - 1] + '@' + videoFile.split(',')[1]
            _this.$apis.common.pictureUpload({ file: base64 }).then(res => {
              if (res) {
                // console.log(res)
                insertVideoFn(res)
              }
            })
            // resultFiles 是 input 中选中的文件列表
            // insertImgFn 是获取图片 url 后,插入到编辑器的方法
            // 上传图片,返回结果,将图片插入到编辑器中
          }
        }
        this.editor.config.onchangeTimeout = 5000
        this.editor.create()

        // 绑定点击事件处理器
        this.editor.$textElem.on('click', this.handleEditorClick)
      },
      setEdit(html) {
        this.$nextTick(() => {
          this.editor.txt.html(html)
        })
      },
      forbidEdit() {
        // this.editor.$textElem.attr('contenteditable', false)
        this.editor.disable()
      }
    }
  }
  </script>
  <style lang="scss" scoped>
  #div-demo {
    // margin: 16px 300px 0 0;
  }
  .el-form .editor {
    margin-top: 20px;
  }
  .myQuillEditor {
    background-color: #fff;
    height: 200px;
  }
  </style>
  <style>
  .w-e-text-container {
    z-index: 1 !important;
  }
  .w-e-toolbar {
    z-index: 2 !important;
  }
  </style>