<script>
import Editor, { EditorMode } from '@hufe921/canvas-editor'

export default {
  name: 'RichTextModal',
  components: { },
  data () {
    return {
      instance: null,
      richText: undefined
    }
  },
  methods: {
    init (params) {
      const {
        richText = {
          data: {
            header: [],
            main: [],
            footer: []
          },
          options: {}
        }
      } = params
      this.richText = richText
      if (this.instance) {
        this.instance.destroy()
      }
      this.instance = new Editor(
        this.$refs.editorContainer,
        this.richText.data,
        this.richText.options,
      )
      // 设置编辑模式
      this.instance.command.executeMode(EditorMode.EDIT)
    },
    getValue () {
      return this.instance.command.getValue()
    }
  },
  beforeDestroy () {
    if (this.instance) {
      this.instance.destroy()
      this.instance = null
    }
  }
}
</script>

<template>
  <div ref="editorContainer"></div>
</template>

<style scoped lang="less">

</style>
