var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/braft-editor/index.tsx
var braft_editor_exports = {};
__export(braft_editor_exports, {
  default: () => braft_editor_default
});
module.exports = __toCommonJS(braft_editor_exports);
var import_react = __toESM(require("react"));
var import_braft_editor = __toESM(require("braft-editor"));
var import_braft_utils = require("braft-utils");
var import_max_length = __toESM(require("braft-extensions/dist/max-length"));
var import_antd = require("antd");
var import_icons = require("@ant-design/icons");
var import_dist = require("braft-editor/dist/index.css");
var import_ahooks = require("ahooks");
var import_context = require("../config-provider/context");
var import_utils = require("../locale/utils");
var import_upload = __toESM(require("../upload"));
var import_Preview = __toESM(require("./Preview"));
var import_index = require("./index.less");
import_antd.message.config({
  duration: 2,
  maxCount: 1
  // 最多显示个数
});
var entityTags = {
  // 指定扩展类型
  type: "entity",
  // 指定扩展的entity名称，推荐使用全部大写，内部也会将小写转换为大写
  name: "KEYWORD-ITEM",
  // 在编辑器工具栏中增加一个控制按钮，点击时会将所选文字转换为该entity
  // control: {
  // 	text: '标签'
  // },
  // 指定entity的mutability属性，可选值为MUTABLE和IMMUTABLE，表明该entity是否可编辑，默认为MUTABLE
  mutability: "IMMUTABLE",
  // 指定通过上面新增的按钮创建entity时的默认附加数据
  // data: {},
  // 指定entity在编辑器中的渲染组件
  component: (props) => {
    return /* @__PURE__ */ import_react.default.createElement("span", { className: "keyword-item" }, props.children);
  },
  // 指定html转换为editorState时，何种规则的内容将会转换成该entity
  importer: (nodeName, node) => {
    if (nodeName.toLowerCase() === "span" && node.classList && node.classList.contains("keyword-item")) {
      return {
        mutability: "IMMUTABLE",
        data: {}
      };
    }
    return;
  },
  // 指定输出该entity在输出的html中的呈现方式
  exporter: (entityObject, originalText) => {
    return /* @__PURE__ */ import_react.default.createElement("span", { className: "keyword-item" }, originalText);
  }
};
import_braft_editor.default.use([(0, import_max_length.default)({}), entityTags]);
var CONTROLS = [
  "undo",
  "redo",
  "separator",
  "font-size",
  "separator",
  "text-align",
  "separator",
  "text-color",
  "bold",
  "italic",
  "underline",
  "separator",
  "link",
  "separator",
  "fullscreen"
];
var Keywords = (props) => {
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "u-keyWords" }, /* @__PURE__ */ import_react.default.createElement("span", null, props.locales.BraftEditor.optional), (props.tagList || []).map((item) => /* @__PURE__ */ import_react.default.createElement(import_antd.Tag, { onClick: () => props.onClick(item.htmlValue), key: item.id }, item.fieldName)));
};
var Editor = (props) => {
  const { tagList, maxLength = Infinity, simple, className = "", value, defaultValue, onChange = () => null, hooks, ...rest } = props;
  const { "before-preview": beforePreview, ...restHooks } = hooks || {};
  const { run: _onChange } = (0, import_ahooks.useDebounceFn)(onChange, { wait: 200 });
  const [_maxLength, setMaxLength] = (0, import_react.useState)(maxLength);
  const [visible, setVisible] = (0, import_react.useState)(false);
  const initialValue = (0, import_react.useMemo)(() => {
    if ("value" in props)
      return import_braft_editor.default.createEditorState(value);
    if ("defaultValue" in props)
      return import_braft_editor.default.createEditorState(defaultValue);
    return import_braft_editor.default.createEditorState("");
  }, []);
  const [editorState, setEditorState] = (0, import_react.useState)(initialValue);
  const [html, setHtml] = (0, import_react.useState)();
  (0, import_ahooks.useUpdateEffect)(() => {
    if (value && value !== editorState.toHTML()) {
      setEditorState(import_braft_editor.default.createEditorState(value));
    }
  }, [value]);
  const onUpload = (_, file) => {
    var _a;
    if (!file)
      return;
    if (file.status === "done") {
      setEditorState(
        import_braft_utils.ContentUtils.insertMedias(editorState, [
          {
            type: "IMAGE",
            url: (_a = file.response) == null ? void 0 : _a.url
          }
        ])
      );
    }
  };
  const { run: calcMaxLength } = (0, import_ahooks.useDebounceFn)(() => {
    let html2 = editorState ? editorState.toHTML() : "", length = _maxLength || Infinity;
    tagList && tagList.forEach((item) => {
      let len = html2.split(item.htmlValue).length;
      len > 1 && (length += item.fieldName.length * (len - 1));
    });
    setMaxLength(length);
  });
  const handleChange = (editorState2) => {
    calcMaxLength();
    setEditorState(editorState2);
    _onChange(editorState2.isEmpty() ? "" : editorState2.toHTML(), editorState2);
  };
  const onChoseTags = (htmlString) => {
    let value2 = import_braft_utils.ContentUtils.insertHTML(editorState, htmlString);
    calcMaxLength();
    setEditorState(value2);
    _onChange(value2.isEmpty() ? "" : value2.toHTML(), editorState);
  };
  return /* @__PURE__ */ import_react.default.createElement(import_context.ConfigContext.Consumer, null, (value2) => {
    const { uiLocale } = value2;
    let uploader = {
      key: "antd-uploader",
      type: "component",
      component: /* @__PURE__ */ import_react.default.createElement(import_upload.default, { accept: "image/*", onChange: onUpload, showUploadList: false }, /* @__PURE__ */ import_react.default.createElement("button", { type: "button", "data-title": uiLocale.BraftEditor.insertImage, className: "control-item button" }, /* @__PURE__ */ import_react.default.createElement(import_icons.PictureFilled, null)))
    }, preview = {
      className: "edit-preview",
      key: "custom-button",
      type: "button",
      text: uiLocale.global.preview,
      onClick: () => {
        setHtml(beforePreview ? beforePreview(editorState) : editorState == null ? void 0 : editorState.toHTML());
        setVisible(true);
      }
    };
    const extendControls = simple ? [uploader] : [uploader, "separator", preview];
    const onReachMaxLength = () => {
      import_antd.message.warning((0, import_utils.replaceMessage)(uiLocale.BraftEditor.maxLengthWarning, { maxLength: `${_maxLength}` }));
    };
    return /* @__PURE__ */ import_react.default.createElement(import_antd.ConfigProvider, { ...value2 }, /* @__PURE__ */ import_react.default.createElement("div", { className: `m-braftEditor ${className}` }, /* @__PURE__ */ import_react.default.createElement(
      import_braft_editor.default,
      {
        hooks: restHooks,
        componentBelowControlBar: tagList ? /* @__PURE__ */ import_react.default.createElement(Keywords, { locales: uiLocale, onClick: onChoseTags, tagList }) : null,
        onReachMaxLength,
        controls: simple ? CONTROLS : void 0,
        excludeControls: ["media"],
        extendControls,
        ...rest,
        language: uiLocale.locale,
        maxLength: _maxLength,
        value: editorState,
        onChange: handleChange
      }
    ), /* @__PURE__ */ import_react.default.createElement(import_antd.Modal, { title: uiLocale.global.preview, width: 1e3, open: visible, onCancel: () => setVisible(false), footer: false }, /* @__PURE__ */ import_react.default.createElement(import_Preview.default, { html }))));
  });
};
Editor.Preview = import_Preview.default;
Editor.createEditorState = import_braft_editor.default.createEditorState;
var braft_editor_default = Editor;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});
