<template>
  <div>
    <script id="editor" type="text/plain"></script>
  </div>
</template>

<script>
    import '../../static/UE/ueditor.config.js'
    import '../../static/UE/ueditor.all.min.js'
    import '../../static/UE/lang/zh-cn/zh-cn.js'
    import '../../static/UE/ueditor.parse.min.js'
    export default {
        name: 'UE',
        data() {
            return {
                editor: null,
                buildTime: null,
            }
        },
        props: {
            defaultMsg: {
                type: String
            },
            config: {
                type: Object
            }
        },
        created() {

        },
        mounted() {
            this.buildTime = setTimeout(() => {
                const _this = this;
                if(this.config.UEDITOR_HOME_URL){
                    if(process.env.NODE_ENV == 'production'){
                        this.config.UEDITOR_HOME_URL = this.$router.options.base+this.config.UEDITOR_HOME_URL
                    }
                }else{
                    this.config.UEDITOR_HOME_URL ='/static/UE/'
                    if(process.env.NODE_ENV == 'production'){
                        this.config.UEDITOR_HOME_URL = this.$router.options.base+this.config.UEDITOR_HOME_URL
                    }
                }
                this.editor = UE.getEditor('editor', this.config); // 初始化UE
                this.editor.addListener("ready", function() {
                    _this.editor.setContent(_this.defaultMsg); // 确保UE加载完成后，放入内容。
                });
            }, 500)
        },
        methods: {
            getUEContent() { // 获取内容方法
                return this.editor.getContent()
            },
            setUEContent(str) { // 设置内容方法
                return this.editor.setContent(str)
            }
        },
        destroyed() {
            clearTimeout(this.buildTime);
            this.editor.destroy();
        }
    }
</script>