<template>
  <div>
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editor"
      :default-config="toolbarConfig"
      :mode="mode"
    />
    <Editor
      v-model="content"
      style="height: 500px; overflow-y: hidden;"
      :default-config="editorConfig"
      :mode="mode"
      @onCreated="onCreated"
    />
  </div>
</template>

<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { upload } from '@/utils/upload'
import { mapGetters } from 'vuex'
export default {
  name: 'DmInformation',
  components: { Editor, Toolbar },
  dicts: ['information_status', 'is_true'],

  data() {
    return {
      editor: null,
      toolbarConfig: {},
      editorConfig: { placeholder: '请输入内容...',
        MENU_CONF: { 'uploadImage': {
          customUpload: this.update
        }}},
      mode: 'simple' // default or 'simple'
    }
  },
  computed: {
    ...mapGetters(['imagesUploadApi', 'baseApi'])
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  },
  methods: {
    update(file, insertFn) {
      // 文件上传
      // files 是 input 中选中的文件列表
      // insert 是获取图片 url 后，插入到编辑器的方法
      upload(this.imagesUploadApi, file).then((res) => {
        const data = res.data
        const url = data.url
        insertFn(url)
      })
    },
    changeEditor(e) {
      this.content = this.html_content
    },
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
      this.editorConfig.MENU_CONF = editor.getAllMenuKeys()
    },
    created() {
      console.log(this.html_content)
    }
  }
}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style scoped>
</style>
