<template>
  <div class="tinymce-container editor-container" :class="{fullscreen:fullscreen}">
    <textarea :id="tinymceId" class="tinymce-textarea" />
  </div>
</template>

<script>
import plugins from "./plugins"; // 见下文
import toolbar from "./toolbar"; // 见下文

// 上传html片段接口根据自己项目更换
// import { uploadHtml } from "@/api/upload";
export default {
  name: "Tinymce",
  props: {
    // 默认填充到富文本的html文件
    htmlstr: {
      type: String,
      default: "",
    },
    toolbar: {
      type: Array,
      default() {
        return [];
      },
    },
    menubar: {
      type: Boolean,
      default: true,
    },
    height: {
      type: Number,
      default: 400,
    },
  },
  data() {
    return {
      hasChange: false,
      hasInit: false,
      tinymceId: "vue-tinymce-" + +new Date(),
      fullscreen: false,
      value: "",
      editorContent: "",
    };
  },
  watch: {
    value(val) {
      this.$nextTick(() =>
        window.tinymce.get(this.tinymceId).setContent(val || "")
      );
    },
    htmlstr(val) {
      this.value = val
      if (this.isUrl) {
        this.loadUrl(val);
      }
    },
  },
  created() {
      this.value = this.htmlstr + "";
      this.editorContent = this.htmlstr + "";
  },
  mounted() {
    this.initTinymce();
  },
  activated() {
    this.initTinymce();
  },
  deactivated() {
    this.destroyTinymce();
  },
  destroyed() {
    this.destroyTinymce();
  },
  methods: {
    initTinymce() {
      window.tinymce.init({
        branding:false,
        fontsize_formats: "12px 14px 16px 18px 20px 24px 36px",
        language: "zh_CN",
        language_url: "./static/tinymce/langs/zh_CN.js",
        selector: `#${this.tinymceId}`,
        height: this.height,
        body_class: "panel-body ",
        object_resizing: true,
        toolbar: this.toolbar.length > 0 ? this.toolbar : toolbar,
        menubar: this.menubar,
        plugins: plugins,
        end_container_on_empty_block: true,
        powerpaste_word_import: "clean",
        paste_data_images:true,
        code_dialog_height: 450,
        code_dialog_width: 1000,
        advlist_bullet_styles: "square",
        advlist_number_styles: "default",
        default_link_target: "_blank",
        link_title: false,
        file_picker_types:"file",
        file_picker_callback:(cb,value,meta)=>{
          console.log(cb,value,meta)          
        },
        file_upload_handler:async (blobInfo,success,failure)=>{
          let form = new FormData()
          console.log(blobInfo)
          form.append('file',blobInfo.file)
           await this.$http({
            method: "post",
            data: form,
            headers: {
              'Content-Type':'multipart/form-data'
            },
            url: `cms/news/upload/file`,
          }).then(result=>{
            let url = result.data.filePath;
            success(url)
          }).catch(err=>{
            failure(err)
          })
        },
        images_upload_handler:async (blobInfo,success)=>{
          let form = new FormData()
          console.log(blobInfo.blob(),blobInfo.filename())
          form.append('file',blobInfo.blob(),blobInfo.filename())
          
          await this.$http({
            method: "post",
            data: form,
            headers: {
              'Content-Type':'multipart/form-data'
            },
            url: `cms/news/upload/img`,
          }).then(result=>{
            let url = result.data.filePath;
            success(url)
          }).catch(()=>{
            this.$alert("上传失败,请稍后再试", "提示", {
              confirmButtonText: "确定",
              callback: () => {
                this.loading.close()
              },
            });
          })
        },
        init_instance_callback: (editor) => {
          if (this.value) {
            editor.setContent(this.value);
          }
          this.hasInit = true;
          editor.on("Blur", () => {
            this.hasChange = true;
            this.$emit("blur", editor.getContent());
            this.editorContent = editor.getContent();
          });
        },
        setup(editor) {
          editor.on("FullscreenStateChanged", (e) => {
            this.fullscreen = e.state;
          });
        },
      });
    },
    destroyTinymce() {
      if (window.tinymce.get(this.tinymceId)) {
        window.tinymce.get(this.tinymceId).destroy();
      }
    },
    loadUrl(url) {
      this.editorContent = url
      // if (url && url.length > 0) {
      //   this.$http
      //     .get(url)
      //     .then((response) => {
      //       // 处理HTML显示
      //       this.value = response.data;
      //       this.editorContent = response.data;
      //       this.$emit("subLoadUrlToHtml", response.data);
      //       this.$emit("input", response.data);
      //     })
      //     .catch(() => {
      //       this.value = "服务器数据加载失败，请重试!";
      //     });
      // }
    },
    // 设置编辑器内容
    setContent(value) {
      window.tinymce.get(this.tinymceId).setContent(value);
    },
    // 获取编辑器内容
    getContent() {
      window.tinymce.get(this.tinymceId).getContent();
    },
    // 图片上传成功后填充到富文本编辑器
    async imageSuccess(urlList) {
      try {
        let imageTemplateList = "";
        urlList.forEach((item) => {
          const image = `<img style="max-width:100%;" src="${item}">`;
          imageTemplateList = imageTemplateList + image;
        });
        window.tinymce.get(this.tinymceId).insertContent(imageTemplateList);
        this.$message({
          message: "上传成功！",
          type: "success",
        });
      } catch (error) {
        console.log(error);
        this.$message({
          message: error,
          type: "error",
        });
      }
    },
    // 编辑器内容上传到cos，调用返回url
    // async content2Url() {
    //   try {
    //     const res = await uploadHtml(this.editorContent);
    //     return res;
    //   } catch (error) {
    //     this.$message({
    //       message: error.data.message,
    //       type: "error",
    //     });
    //   }
    // },
  },
};
</script>
<style lang="less" scoped>
#tinymce {
  background-color: blue;
  p {
    margin: 0;
  }
}
.tinymce-container {
  position: relative;
}
.tinymce-container{
    /deep/ .mce-fullscreen {
        z-index: 10000;
    }
}
.tinymce-textarea {
  visibility: hidden;
  z-index: -1;
}
.editor-custom-btn-container {
  position: absolute;
  right: 4px;
  top: 4px;
  /*z-index: 2005;*/
}
.fullscreen .editor-custom-btn-container {
  z-index: 10000;
  position: fixed;
}
.editor-upload-btn {
  display: inline-block;
}
// 隐藏底部logo栏
.mce-edit-area + .mce-statusbar {
  opacity: 0;
  height: 0;
}
</style>
