'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var editorForVue = require('@wangeditor/editor-for-vue');
require('../../../utils/index.js');
var richTextEditor = require('./rich-text-editor.js');
require('@wangeditor/editor/dist/css/style.css');
var createNamespace = require('../../../utils/create-namespace.js');

"use strict";
var _sfc_main = /* @__PURE__ */ vue.defineComponent({
  ...{ name: "LlRichTextEditor" },
  __name: "rich-text-editor",
  props: richTextEditor.richTextEditorProps,
  emits: richTextEditor.richTextEditorEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const bem = createNamespace.createNamespace("rich-text-editor");
    const props = __props;
    const emits = __emit;
    const editorRef = vue.shallowRef();
    const innerValue = vue.ref(props.value);
    const loading = vue.ref(props.loading);
    const allImages = vue.ref([]);
    const editorConfig = vue.computed(() => {
      return {
        MENU_CONF: {
          uploadImage: {
            async onBeforeUpload(file) {
              if (props.onBeforeUploadImage) {
                const res = await props.onBeforeUploadImage(file);
                return res ? file : false;
              }
              return file;
            },
            async customUpload(file, insertFn) {
              loading.value = true;
              try {
                const res = await props.customUploadImage(file);
                insertFn(res);
                loading.value = false;
              } catch (error) {
                editorRef.value.alert("\u9519\u8BEF\u4FE1\u606F", props.uploadImageError);
                loading.value = false;
              }
            },
            base64LimitSize: 5 * 1024
          },
          insertImage: {
            async onInsertedImage(imageNode) {
              allImages.value.push(imageNode);
            }
          },
          uploadVideo: {
            async customUpload(file, insertFn) {
              if (props.customUploadVideo) {
                loading.value = true;
                try {
                  const res = await props.customUploadVideo(file);
                  insertFn(res, richTextEditor.FALLBACK_IMAGE);
                  loading.value = false;
                } catch (error) {
                  editorRef.value.alert("\u9519\u8BEF\u4FE1\u606F", props.uploadImageError);
                  loading.value = false;
                }
              }
            }
          }
        },
        ...props.editorConfig
      };
    });
    const handleCreated = (editor) => {
      editorRef.value = editor;
      allImages.value = editor.getElemsByType("image");
      emits("created", editor);
    };
    const onEditorUpdate = (value) => {
      innerValue.value = value;
      emits("update:value", value);
    };
    const handleChange = (editor) => {
      emits("change", editor);
    };
    const handleDestroyed = (editor) => {
      emits("destroyed", editor);
    };
    const handleFocus = (editor) => {
      emits("focus", editor);
    };
    const handleBlur = (editor) => {
      emits("blur", editor);
    };
    const customAlert = (info, type) => {
      emits("alert", [info, type]);
    };
    const getEditorImage = () => {
      const editor = editorRef.value;
      if (editor == null) return;
      return {
        allImages: allImages.value,
        currentImages: editor.getElemsByType("image")
      };
    };
    vue.onBeforeUnmount(() => {
      const editor = editorRef.value;
      if (editor == null) return;
      editor.destroy();
    });
    vue.watch(
      () => props.loading,
      (val) => {
        loading.value = val;
      }
    );
    vue.watch(
      () => props.value,
      (newValue) => {
        if (newValue !== innerValue.value) {
          innerValue.value = newValue;
        }
      },
      { deep: true }
    );
    vue.watch(
      () => [props.disabled, loading.value],
      async (val) => {
        const editor = editorRef.value;
        if (editor == null) return;
        await vue.nextTick();
        if (val[0] || val[1]) {
          editor.disable();
        } else {
          editor.enable();
        }
      },
      {
        deep: true
      }
    );
    __expose({
      editorRef: editorRef.value,
      getEditorImage
    });
    return (_ctx, _cache) => {
      const _component_a_spin = vue.resolveComponent("a-spin");
      return vue.openBlock(), vue.createElementBlock(
        "div",
        {
          class: vue.normalizeClass(vue.unref(bem).b())
        },
        [
          vue.createVNode(_component_a_spin, { spinning: loading.value }, {
            default: vue.withCtx(() => [
              vue.createElementVNode(
                "div",
                {
                  class: vue.normalizeClass(vue.unref(bem).e("container"))
                },
                [
                  vue.createVNode(vue.unref(editorForVue.Toolbar), {
                    class: vue.normalizeClass(vue.unref(bem).e("toolbar-container")),
                    editor: editorRef.value,
                    "default-config": props.toolbarConfig,
                    mode: props.mode
                  }, null, 8, ["class", "editor", "default-config", "mode"]),
                  vue.createCommentVNode("  "),
                  vue.createVNode(vue.unref(editorForVue.Editor), {
                    "model-value": innerValue.value,
                    class: vue.normalizeClass(vue.unref(bem).e("editor-container")),
                    "default-config": editorConfig.value,
                    mode: props.mode,
                    "onUpdate:modelValue": onEditorUpdate,
                    onOnCreated: handleCreated,
                    onOnChange: handleChange,
                    onOnDestroyed: handleDestroyed,
                    onOnFocus: handleFocus,
                    onOnBlur: handleBlur,
                    onCustomAlert: customAlert
                  }, null, 8, ["model-value", "class", "default-config", "mode"])
                ],
                2
                /* CLASS */
              )
            ]),
            _: 1
            /* STABLE */
          }, 8, ["spinning"])
        ],
        2
        /* CLASS */
      );
    };
  }
});

exports.default = _sfc_main;
//# sourceMappingURL=rich-text-editor.vue2.js.map
