<!--
 * @Description: 富文本编辑器封装
 * @Author: 空白格
 * @Date: 2022-08-29 11:20:00
 * @LastEditors: 空白格
 * @LastEditTime: 2022-08-29 11:58:45
 * @FilePath: \veterans_client_web\src\components\Quill\index.vue
 * @Copyright: Copyright (c) 2016~2022 by 空白格, All Rights Reserved.
-->

<template>
  <quill-editor
    class="editor"
    v-model="myContent"
    ref="myQuillEditor"
    :options="editorOption"
    @blur="onEditorBlur($event)"
    @focus="onEditorFocus($event)"
    @change="onEditorChange($event)"
  >
  </quill-editor>
</template>

<script>
import { quillEditor, Quill } from "vue-quill-editor";
import { ImageExtend, QuillWatch } from "quill-image-extend-module";

Quill.register("modules/ImageExtend", ImageExtend);

// 工具栏配置
const toolbarOptions = [
  ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线 -----['bold', 'italic', 'underline', 'strike']
  ["blockquote", "code-block"], // 引用  代码块-----['blockquote', 'code-block']
  [{ header: 1 }, { header: 2 }], // 1、2 级标题-----[{ header: 1 }, { header: 2 }]
  [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表-----[{ list: 'ordered' }, { list: 'bullet' }]
  [{ script: "sub" }, { script: "super" }], // 上标/下标-----[{ script: 'sub' }, { script: 'super' }]
  [{ indent: "-1" }, { indent: "+1" }], // 缩进-----[{ indent: '-1' }, { indent: '+1' }]
  [{ direction: "rtl" }], // 文本方向-----[{'direction': 'rtl'}]
  [{ size: ["small", false, "large", "huge"] }], // 字体大小-----[{ size: ['small', false, 'large', 'huge'] }]
  [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题-----[{ header: [1, 2, 3, 4, 5, 6, false] }]
  [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色-----[{ color: [] }, { background: [] }]
  [{ font: [] }], // 字体种类-----[{ font: [] }]
  [{ align: [] }], // 对齐方式-----[{ align: [] }]
  ["clean"], // 清除文本格式-----['clean']
  ["link", "image"], // 链接、图片、视频-----['link', 'image', 'video']
];

export default {
  name: "Quill",
  components: { quillEditor },
  props: {
    content: {
      type: String,
      default: "",
    },
    placeholder: {
      type: String,
      default: "请输入内容",
    },
  },
  data() {
    return {
      // 富文本
      myContent: this.content,
      editorOption: {
        placeholder: this.placeholder,
        theme: "snow", // or 'bubble'
        modules: {
          ImageExtend: {
            loading: true,
            name: "file",
            size: 2, // 可选参数 图片大小，单位为M，1M = 1024kb
            action: process.env.VUE_APP_BASE_API + "/file/upload/single/minio",
            header: {
              Authorization: `Bearer ${this.$store.state.user.token}`,
            },
            response: (res) => {
              // // 图片下载路径
              let url = ``;
              if (res.code === 200) {
                url = res.data.url ?? "";
              }
              return url;
            },
          },
          toolbar: {
            container: toolbarOptions,
            handlers: {
              image: function () {
                QuillWatch.emit(this.quill.id);
              },
            },
          },
        },
      },
    };
  },
  watch: {
    // 监听content值变化, 确保输入框中的数据刷新
    content() {
      this.myContent = this.content;
    },
  },
  methods: {
    // 失去焦点事件
    onEditorBlur() {
      this.$emit("onEditorBlur", this.myContent);
    },
    // 获得焦点事件
    onEditorFocus() {
      this.$emit("onEditorFocus", this.myContent);
    },
    // 内容改变事件
    onEditorChange() {
      this.$emit("onEditorChange", this.myContent);
    },
  },
};
</script>
