TextBox.vue 4.08 KB
<template>
  <div>
    <div :id="id"></div>
  </div>
</template>

<script>
import { uploadImg, syncUploadVideo, uploadVideo } from "@/api/sys.base"
import E from "wangeditor"
export default {
  name: "TextBox",
  data() {
    return {
      editor: "",
      content: "",
      editorOption: {}
    }
  },
  props: {
    id: {
      type: String,
      default: 'div-demo'
    },
    excludeMenus: {
      type: Array,
      default: () => ["code"]
    },
    boxHeight: {
      type: Number,
      default: 240
    }
  },
  mounted() {
    this.getEdit()
  },
  methods: {
    getEdit() {
      const _this = this
      this.editor = new E(`#${this.id}`)
      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 = "500px"
      this.editor.config.excludeMenus = this.excludeMenus
      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 => {
          uploadImg({ file: base64, compress: 0 }).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()
    },
    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 {
  width: 575px;
  // 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>