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_antd = require("antd");
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_hooks = require("../_util/hooks");
var import_ImageUploader = __toESM(require("./ImageUploader"));
var import_MediaUploader = __toESM(require("./MediaUploader"));
var import_upload = __toESM(require("../upload"));
var import_Preview = __toESM(require("./Preview"));
var import_pluginExtends = require("./pluginExtends");
var import_utils2 = require("./utils");
var import_index = require("./index.less");
import_antd.message.config({
  duration: 2,
  maxCount: 1
  // 最多显示个数
});
var CONTROLS = ["font-size", "text-align", "text-color", "bold", "italic", "underline", "link", "code", "media"];
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.fieldName }, item.fieldName)));
};
var Editor = (props) => {
  const uploadInstance = (0, import_react.useRef)({});
  const { uiLocale } = (0, import_react.useContext)(import_context.ConfigContext);
  const {
    tagList,
    maxLength = 5e3,
    simple,
    className = "",
    value,
    defaultValue,
    onChange = () => null,
    hooks,
    componentBelowControlBar,
    extendControls,
    ...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((0, import_utils2.formatValue)(value));
    if ("defaultValue" in props)
      return import_braft_editor.default.createEditorState((0, import_utils2.formatValue)(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((0, import_utils2.formatValue)(value)));
    }
  }, [value]);
  const uploadFn = (param) => {
    const { nosUpload } = uploadInstance.current;
    nosUpload == null ? void 0 : nosUpload({
      file: param.file,
      onSuccess: (res) => {
        param.success({ url: res.url });
      }
    });
  };
  const { run: calcMaxLength } = (0, import_ahooks.useDebounceFn)(
    () => {
      let html2 = editorState ? editorState.toHTML() : "", length = maxLength;
      tagList == null ? void 0 : tagList.forEach((item) => {
        let len = html2.split(item.htmlValue).length;
        len > 1 && (length += item.fieldName.length * (len - 1));
      });
      setMaxLength(length);
    },
    { wait: 0 }
  );
  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);
  };
  const onReachMaxLength = () => {
    import_antd.message.warning((0, import_utils.replaceMessage)(uiLocale.BraftEditor.maxLengthWarning, { maxLength }));
  };
  const imageUploader = {
    key: "antd-uploader-image",
    type: "component",
    component: /* @__PURE__ */ import_react.default.createElement(import_ImageUploader.default, { setEditorState, editorState })
  }, mediaUploader = {
    key: "antd-uploader-media",
    type: "component",
    component: /* @__PURE__ */ import_react.default.createElement(import_MediaUploader.default, { setEditorState, editorState })
  }, 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 getExtendControls = () => {
    if (extendControls && !extendControls.length) {
      return [];
    }
    return [imageUploader, mediaUploader, preview, ...extendControls || []];
  };
  const renderFooter = () => {
    if (componentBelowControlBar && typeof componentBelowControlBar === "function") {
      return componentBelowControlBar({ onChoseTags });
    }
    if (componentBelowControlBar) {
      return componentBelowControlBar;
    }
    if (tagList) {
      return /* @__PURE__ */ import_react.default.createElement(Keywords, { locales: uiLocale, onClick: onChoseTags, tagList });
    }
    return null;
  };
  return (0, import_hooks.useConfig)(
    /* @__PURE__ */ import_react.default.createElement("div", { className: `m-braftEditor ${className}` }, /* @__PURE__ */ import_react.default.createElement(
      import_braft_editor.default,
      {
        hooks: restHooks,
        componentBelowControlBar: renderFooter(),
        onReachMaxLength,
        controls: simple ? CONTROLS : void 0,
        extendControls: getExtendControls(),
        ...rest,
        language: uiLocale.locale,
        maxLength: _maxLength,
        value: editorState,
        onChange: handleChange,
        media: { uploadFn }
      }
    ), /* @__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 })), /* @__PURE__ */ import_react.default.createElement("div", { style: { display: "none" } }, /* @__PURE__ */ import_react.default.createElement(
      import_upload.default,
      {
        getInstance: (params) => {
          uploadInstance.current = params;
        }
      }
    )))
  );
};
Editor.Preview = import_Preview.default;
Editor.createEditorState = import_braft_editor.default.createEditorState;
Editor.ContentUtils = import_braft_utils.ContentUtils;
var braft_editor_default = Editor;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});
