<template>
  <div class="tinymce-box">
    <editor
      v-if="visible"
      v-model="myValue"
      :init="init"
      :disabled="disabled"
      @onClick="onClick">
    </editor>
  </div>
</template>

<script>
import tinymce from 'tinymce/tinymce' // tinymce默认hidden，不引入不显示
import Editor from '@tinymce/tinymce-vue'
import 'tinymce/themes/silver'
import 'tinymce/icons/default'

// 编辑器插件plugins
// 更多插件参考：https://www.tiny.cloud/docs/plugins/
import 'tinymce/plugins/fullscreen'// 插入上传图片插件
import 'tinymce/plugins/image'// 插入上传图片插件
import 'tinymce/plugins/media'// 插入视频插件
import 'tinymce/plugins/table'// 插入表格插件
import 'tinymce/plugins/lists'// 列表插件
import 'tinymce/plugins/wordcount'// 字数统计插件
import 'tinymce/plugins/code'
import http from '@/utils/request'

export default {
  components: {
    Editor
  },
  name: 'Tinymce',
  props: {
    id: {
      type: String,
      default: ''
    },
    value: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    plugins: {
      type: [String, Array],
      default: 'lists fullscreen code image media table wordcount'
    },
    toolbar: {
      type: [String, Array],
      default: 'undo redo |  formatselect |  bold italic forecolor backcolor | code | fontsizeselect lineheight alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists image media table | removeformat | fullscreen'
    }
  },
  data () {
    return {
      visible: true,
      init: {
        language_url: './tinymce/langs/zh_CN.js',
        language: 'zh_CN',
        skin_url: './tinymce/skins/ui/oxide',
        // skin_url: 'tinymce/skins/ui/oxide-dark',//暗色系
        height: 300,
        plugins: this.plugins,
        toolbar: this.toolbar,
        branding: false,
        menubar: false,
        convert_urls: false,
        fontsize_formats: '12px 14px 16px 18px 24px 36px',
        lineheight_formats: '1 1.1 1.2 1.3 1.4 1.5 2',
        // 如需ajax上传可参考https://www.tiny.cloud/docs/configure/file-image-upload/#images_upload_handler
        images_upload_handler: async (blobInfo, success, failure) => {
          const formdata = new FormData()

          formdata.append('file', blobInfo.blob())
          http.post(this.uploadUrl, formdata, { headers: {
            'Content-Type': 'multipart/form-data' // 之前说的以表单传数据的格式来传递fromdata
          } }).then((res) => {
            console.log(res.link)
            success(res.link)
          })
        }
      },
      myValue: this.value
    }
  },
  mounted () {
    this.visible = false
    this.$nextTick(() => {
      this.visible = true
    })
  },
  methods: {
    // show(){
    //   this.visible=true
    //   this.$nextTick(() => {
    //     this.visible = true
    //   })
    // },
    // 添加相关的事件，可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
    // 需要什么事件可以自己增加
    onClick (e) {
      this.$emit('onClick', e, tinymce)
    },
    // 可以添加一些自己的自定义事件，如清空内容
    clear () {
      this.myValue = ''
    }
  },
  watch: {
    value (newValue) {
      this.myValue = newValue
    },
    myValue (newValue) {
      this.$emit('input', newValue)
    }
  }
}

</script>
<style scoped>

</style>
