"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/rich-text/event-listeners/enter.js
var enter_exports = {};
__export(enter_exports, {
  default: () => enter_default
});
module.exports = __toCommonJS(enter_exports);
var import_keycodes = require("@wordpress/keycodes");
var import_rich_text = require("@wordpress/rich-text");
var enter_default = (props) => (element) => {
  function onKeyDownDeprecated(event) {
    if (event.keyCode !== import_keycodes.ENTER) {
      return;
    }
    const { onReplace, onSplit } = props.current;
    if (onReplace && onSplit) {
      event.__deprecatedOnSplit = true;
    }
  }
  function onKeyDown(event) {
    if (event.defaultPrevented) {
      return;
    }
    if (event.target !== element) {
      return;
    }
    if (event.keyCode !== import_keycodes.ENTER) {
      return;
    }
    const {
      value,
      onChange,
      disableLineBreaks,
      onSplitAtEnd,
      onSplitAtDoubleLineEnd,
      registry
    } = props.current;
    event.preventDefault();
    const { text, start, end } = value;
    if (event.shiftKey) {
      if (!disableLineBreaks) {
        onChange((0, import_rich_text.insert)(value, "\n"));
      }
    } else if (onSplitAtEnd && start === end && end === text.length) {
      onSplitAtEnd();
    } else if (
      // For some blocks it's desirable to split at the end of the
      // block when there are two line breaks at the end of the
      // block, so triple Enter exits the block.
      onSplitAtDoubleLineEnd && start === end && end === text.length && text.slice(-2) === "\n\n"
    ) {
      registry.batch(() => {
        const _value = { ...value };
        _value.start = _value.end - 2;
        onChange((0, import_rich_text.remove)(_value));
        onSplitAtDoubleLineEnd();
      });
    } else if (!disableLineBreaks) {
      onChange((0, import_rich_text.insert)(value, "\n"));
    }
  }
  const { defaultView } = element.ownerDocument;
  defaultView.addEventListener("keydown", onKeyDown);
  element.addEventListener("keydown", onKeyDownDeprecated);
  return () => {
    defaultView.removeEventListener("keydown", onKeyDown);
    element.removeEventListener("keydown", onKeyDownDeprecated);
  };
};
//# sourceMappingURL=enter.js.map
