<template>
  <div class="cn-wang-editor" style="border: 1px solid #ccc;">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editor"
      :defaultConfig="toolbarConfig"
      :mode="mode"
    />
    <Editor
      style="flex: 1; overflow-y: hidden;"
      v-bind="$attrs" v-on="$listeners"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="onCreated"
    />
  </div>
</template>

<script>
import Vue from 'vue'
import {Editor, Toolbar} from '@wangeditor/editor-for-vue'

export default {
  name: 'WangEditor',
  components: {Editor, Toolbar},
  data() {
    let sever = '/cs-harmonic-boot/csconfiguration/uploadImage'
    // 本地加api
    if (process.env.NODE_ENV === 'development') {
      sever = '/api' + sever
    }
    return {
      editor: null,
      toolbarConfig: {
        excludeKeys: ['fullScreen', 'emotion'],
      },
      editorConfig: {
        placeholder: '请输入内容...',
        MENU_CONF: {
          uploadImage: {
            server:sever,
            fieldName: 'file',
            compress: true,
            uploadFileName: 'file',
            withCredentials: true,
            headers: {},
            timeout: 0,
            customInsert: (insertImg, result, editor) => {
              const url = result.data.url
              insertImg(url)
            }
          }
        }
      },
      mode: 'default', // or 'simple'
    }
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    },
  },
  mounted() {
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  }
}
</script>
<style lang="scss">
.cn-wang-editor {
  display: flex;
  flex-direction: column;
  height: 500px;
}

</style>
