<template>
  <div class="tinymceBase">
    <Editor
      api-key="0tdy8tkwpzlqdxy4zv39t99jo6er7pxdy24bkq0batry668n"
      :init="{
        ...DefaultConfig,
        setup,
      }"
    />
  </div>
</template>
<script lang="ts">
import Editor from "@tinymce/tinymce-vue";
import { defineComponent } from "vue";

export default defineComponent({
  data() {
    const Id = Date.now();
    return {
      Id: Id.toString(),
      Editor: null as any,
      DefaultConfig: {
        height: 400,
        menubar: false,
        toolbar1: `styleselect | fontselect | formatselect | fontsizeselect | forecolor backcolor | bold italic underline strikethrough | table | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | removeformat  hr | code  link | undo redo | fullscreen `,
        plugins: `
            paste
            importcss
            table
            advlist
            fullscreen
            link
            media
            lists
            hr
            preview
            wordcount
          `,

        forced_root_block: "p",
        importcss_append: true,
        content_style: `
            *                         { padding:0; margin:0; }
            html, body                { height:100%; }
            a                         { text-decoration: none; }
            iframe                    { width: 100%; }
            p                         { line-height:1.6; margin: 0px; }
            table                     { word-wrap:break-word; word-break:break-all; max-width:100%; border:none; border-color:#999; }
            .mce-object-iframe        { width:100%; box-sizing:border-box; margin:0; padding:0; }
            ul,ol                     { list-style-position:inside; }
          `,
        insert_button_items: "image link | inserttable",
        paste_webkit_styles: "all",
        paste_merge_formats: true,
        nonbreaking_force_tab: false,
        paste_auto_cleanup_on_paste: false,
        fontsize_formats: "10px 11px 12px 14px 16px 18px 20px 24px",
        style_formats: [
          {
            title: "首行缩进",
            block: "p",
            styles: { "text-indent": "2em" },
          },
          {
            title: "行高",
            items: [
              { title: "1", styles: { "line-height": "1" }, inline: "span" },
              {
                title: "1.5",
                styles: { "line-height": "1.5" },
                inline: "span",
              },
              { title: "2", styles: { "line-height": "2" }, inline: "span" },
              {
                title: "2.5",
                styles: { "line-height": "2.5" },
                inline: "span",
              },
              { title: "3", styles: { "line-height": "3" }, inline: "span" },
            ],
          },
        ],

        // FontSelect
        font_formats: `
            微软雅黑=微软雅黑;
            宋体=宋体;
            黑体=黑体;
            仿宋=仿宋;
            楷体=楷体;
            隶书=隶书;
            幼圆=幼圆;
            Andale Mono=andale mono,times;
            Arial=arial, helvetica,
            sans-serif;
            Arial Black=arial black, avant garde;
            Book Antiqua=book antiqua,palatino;
            Comic Sans MS=comic sans ms,sans-serif;
`,
        tabfocus_elements: ":prev,:next",
        object_resizing: true,
        language: "zh_CN",
      },
    };
  },
  components: {
    Editor,
  },
  props: {
    value: {
      default: "",
      type: String,
    },
    config: {
      type: Object,
      default: () => {
        return {
          theme: "modern",
          height: 300,
        };
      },
    },
    url: {
      default: "",
      type: String,
    },
    maxSize: {
      default: 2097152,
      type: Number,
    },
    withCredentials: {
      default: false,
      type: Boolean,
    },
  },
  beforeUnmount() {
    this.Editor?.remove();
  },
  emits: ["input"],
  methods: {
    setup(editor: any) {
      this.Editor = editor;
      editor.on("init", () => {
        editor.setContent(this.value);
      });
      editor.on("input change undo redo", () => {
        this.$emit("input", editor.getContent());
      });
    },
  },
});
</script>
<style lang="scss" scoped>
.tinymceBase {
  width: 100%;
  margin: 0 auto;

  &::-webkit-scrollbar {
    width: 10px;
    height: 10px;
  }
  &::-webkit-scrollbar-track {
    background: red;
    border-radius: 2px;
  }
  &::-webkit-scrollbar-thumb {
    background: red;
    border-radius: 10px;
  }
  &::-webkit-scrollbar-thumb:hover {
    background: red;
  }
  &::-webkit-scrollbar-corner {
    background: red;
  }
  :deep(.mce-flow-layout-item.mce-last) {
    display: none;
  }
  :deep(.mce-txt) {
    width: 58px;
  }
}
</style>
