<template>
  <div style="border: 1px solid #ccc;">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editor"
      :defaultConfig="toolbarConfig"
      :mode="mode"
    />
    <Editor
      style="overflow-y: hidden;"
      :style="{
        height: editorHeight+'px'
      }"
      v-model="html"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onChange="onChange"
      @onCreated="onCreated"
    />
  </div>
</template>

<script>
import '@wangeditor/editor/dist/css/style.css'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import {getToken} from "@/utils/auth";  //组件

// yarn add @wangeditor/editor
// # 或者 npm install @wangeditor/editor --save

// yarn add @wangeditor/editor-for-vue
// # 或者 npm install @wangeditor/editor-for-vue --save

export default {
  name: "WEditor",
  components: { Editor, Toolbar },
  props:{
    value:{
      type:String,
      default:''
    },
    editorHeight: {
      type: Number,
      default: 300
    }
  },
  data() {
    return {
      editor: null,
      html: '',
      toolbarConfig: {
        excludeKeys: [
          'uploadVideo',
          'emotion',
          'fullScreen',
        ],
      },
      editorConfig: {
        placeholder: '',
        MENU_CONF: {
          uploadImage: {
            maxFileSize: 5 * 1024 * 1024,
            server: process.env.VUE_APP_BASE_API + "/common/upload",
            fieldName: 'file',
            // 自定义增加 http  header
            headers: {
              Authorization: "Bearer " + getToken(),
            },
            // 上传进度的回调函数
            onProgress(progress) {
              // progress 是 0-100 的数字
              console.log('progress', progress)
            },
            // 单个文件上传成功之后
            onSuccess:(file, res) => {
              this.$modal.msgSuccess("上传成功");
            },
            // 上传错误，或者触发 timeout 超时
            onError:(file, err, res) => {
              this.$modal.msgError("上传失败");
            },
            customInsert(result, insertFn) {
              // res 即服务端的返回结果
              insertFn(result.url,'','')
            }
          }
        }
      },
      mode: 'default', // or 'simple'
    }
  },
  methods: {
    onCreated(editor) {
      this.html = this.value
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
    onChange(editor){
      if (this.html !== this.value) {
        this.$emit('input', this.html) // 将内容同步到父组件中
      }
    }
  },
  watch: {
    value: function(value) {
      if (value !== this.html) {
        this.html = value
      }
    }
    //value为编辑框输入的内容，这里我监听了一下值，当父组件调用得时候，如果给value赋值了，子组件将会显示父组件赋给的值
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  }
}
</script>

<style scoped >
.w-e-toolbar {
  z-index: 2!important;
}
.w-e-menu {
  z-index: 2 !important;
}
.w-e-text-container {
  z-index: 1 !important;
}
.w-e-bar-item-menus-container .title{
  margin: 0 !important;
}
</style>
