<template>
  <div>
    <textarea type="text/plain" :id="id"></textarea>
    <!-- 选择图片 -->

    <el-dialog title="选择文件" :visible.sync="changeState" width="1240px">
      <ChooseImg @SelectId="SelectId" :IsMultiple="IsMultiple"></ChooseImg>
    </el-dialog>
  </div>
</template>
<script>
  import ChooseImg from "@/components/global/ChooseImg.vue";
  export default {
    components: {
      ChooseImg
    },
    name: 'UE',
    data() {
      return {
        id: 'editor' + (Math.floor((Math.random() * 10000) + 1)),
        editor: null,
        changeState: false,
        isInputChange: false,
        tempContent: this.defaultMsg
      }
    },
    props: {
      defaultMsg: {
        type: String
      },
      config: {
        type: Object
      },
      //是否多选
      IsMultiple: {
        type: Boolean
      }
    },
    mounted() {
      this.loadUe();
    },
    watch: {
      defaultMsg(newVal, oldVal) {
        if (!this.isInputChange && newVal) {
          if (this.editor && this.editor.isReady === 1) {
            this.editor.setContent(newVal);
          } else {
            this.tempContent = newVal;
          }
        }
        if (this.isInputChange) {
          this.isInputChange = false;
        }
      },
    },
    methods: {
      InitData() {

      },
      SelectId(resultMsg) {
        if (resultMsg) {
          let html = '';
          this.ue = UE.getEditor(this.id);
          if (this.IsMultiple) {
            if (resultMsg.length > 0) {
              resultMsg.forEach(item => {
                html += '<img src="' + this.getIconLink(item.url) + '" style="max-width: 100%;" />';
              })
            }
          } else {
            html = '<img src="' + this.getIconLink(resultMsg.url) + '" style="max-width: 100%;" />';
          }
          this.ue.execCommand('inserthtml', html);
        }
        this.changeState = false;
      },
      getUEContent() { // 获取内容方法
        return this.editor.getContent()
      },
      loadUe() {
        const _this = this;
        UE.registerUI('appinsertimage', (editor, uiName) => {
          return new UE.ui.Button({
            name: uiName,
            title: '插入图片',
            //添加额外样式，指定icon图标，这里默认使用一个重复的icon
            cssRules: 'background-position: -381px 0px;',
            onclick() {
              self.editor = editor;
              _this.changeState = true;
            },
          });
        });
        this.editor = UE.getEditor(this.id, this.config); // 初始化UE
        let self = this;
        this.editor.addListener("ready", function () {
          _this.editor.setContent(_this.defaultMsg); // 确保UE加载完成后，放入内容。
        });
        this.editor.addListener('ready', editor => {
          if (_this.tempContent) {
            _this.editor.setContent(_this.tempContent);
          }
        });
        this.editor.addListener('keyup', editor => {
          this.isInputChange = false;
          _this.$emit('input', _this.editor.getContent());
        });
        this.editor.addListener('contentChange', editor => {
          this.isInputChange = false;
          _this.$emit('input', _this.editor.getContent());
        });
      }
    },
    destroyed() {
      this.editor.destroy();
    },

  }

</script>
