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/comment-editor/editor/index.tsx
var editor_exports = {};
__export(editor_exports, {
  default: () => editor_default
});
module.exports = __toCommonJS(editor_exports);
var import_react = __toESM(require("react"));
var import_index = require("./index.less");
var Editor = (props) => {
  const { value, placeholder, autoFocus, onlyPasteText, onKeyDown, onInput, setEditorFn } = props;
  const editorRef = (0, import_react.useRef)(null);
  const _onInput = (e) => {
    let { innerHTML, innerText } = e.target;
    onInput == null ? void 0 : onInput({ html: innerHTML, text: innerText });
  };
  const onPaste = (e) => {
    if (onlyPasteText) {
      e.preventDefault();
      let pastedText = e.clipboardData.getData("text/plain");
      document.execCommand("insertText", false, pastedText);
    }
  };
  const lastEditRange = (0, import_react.useRef)();
  const recordLastEditRange = () => {
    var selection = getSelection();
    if (!selection)
      return;
    lastEditRange.current = selection.getRangeAt(0);
  };
  const insertHtml = (0, import_react.useCallback)(
    (value2, options) => {
      var _a, _b, _c;
      let selection = getSelection();
      if (!selection)
        return;
      const { className, contentEditable } = options || {};
      let insertNode = document.createElement("span");
      insertNode.innerHTML = value2;
      insertNode.className = className ? `insertHtml ${className}` : "insertHtml";
      insertNode.setAttribute("contentEditable", contentEditable ? "true" : "false");
      if (lastEditRange.current) {
        selection.removeAllRanges();
        selection.addRange(lastEditRange.current);
      } else {
        (_a = editorRef.current) == null ? void 0 : _a.focus();
      }
      let range = selection.getRangeAt(0);
      range.insertNode(insertNode);
      range.setStartAfter(insertNode);
      range.collapse(true);
      selection.removeAllRanges();
      selection.addRange(range);
      lastEditRange.current = selection.getRangeAt(0);
      onInput == null ? void 0 : onInput({ html: ((_b = editorRef.current) == null ? void 0 : _b.innerHTML) || "", text: ((_c = editorRef.current) == null ? void 0 : _c.innerText) || "" });
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    []
  );
  const cleanContent = (0, import_react.useCallback)(() => {
    if (editorRef.current) {
      editorRef.current.innerHTML = "";
      onInput == null ? void 0 : onInput({ html: "", text: "" });
    }
  }, []);
  (0, import_react.useEffect)(() => {
    if (autoFocus) {
      setTimeout(() => {
        var _a;
        (_a = editorRef.current) == null ? void 0 : _a.focus();
      });
    }
    setEditorFn({ insertHtml, cleanContent });
  }, []);
  (0, import_react.useEffect)(() => {
    value && insertHtml(value);
  }, [value, insertHtml]);
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: "comment-editor",
      ref: editorRef,
      contentEditable: true,
      placeholder,
      onBlur: recordLastEditRange,
      onInput: _onInput,
      onPaste,
      onKeyDown
    }
  );
};
var editor_default = Editor;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});
