// packages/block-library/src/block-keyboard-shortcuts/index.js
import { useEffect } from "@wordpress/element";
import { useSelect, useDispatch } from "@wordpress/data";
import {
  useShortcut,
  store as keyboardShortcutsStore
} from "@wordpress/keyboard-shortcuts";
import { __ } from "@wordpress/i18n";
import { createBlock } from "@wordpress/blocks";
import { store as blockEditorStore } from "@wordpress/block-editor";
function BlockKeyboardShortcuts() {
  const { registerShortcut } = useDispatch(keyboardShortcutsStore);
  const { replaceBlocks } = useDispatch(blockEditorStore);
  const { getBlockName, getSelectedBlockClientId, getBlockAttributes } = useSelect(blockEditorStore);
  const handleTransformHeadingAndParagraph = (event, level) => {
    event.preventDefault();
    const currentClientId = getSelectedBlockClientId();
    if (currentClientId === null) {
      return;
    }
    const blockName = getBlockName(currentClientId);
    const isParagraph = blockName === "core/paragraph";
    const isHeading = blockName === "core/heading";
    if (!isParagraph && !isHeading) {
      return;
    }
    const destinationBlockName = level === 0 ? "core/paragraph" : "core/heading";
    const attributes = getBlockAttributes(currentClientId);
    if (isParagraph && level === 0 || isHeading && attributes.level === level) {
      return;
    }
    const newAttributes = {
      content: attributes.content
    };
    const sourceTextAlign = attributes.textAlign || attributes.style?.typography?.textAlign;
    if (destinationBlockName === "core/heading") {
      newAttributes.level = level;
      if (sourceTextAlign) {
        newAttributes.textAlign = sourceTextAlign;
      }
    } else if (sourceTextAlign) {
      newAttributes.style = {
        typography: {
          textAlign: sourceTextAlign
        }
      };
    }
    replaceBlocks(
      currentClientId,
      createBlock(destinationBlockName, newAttributes)
    );
  };
  useEffect(() => {
    registerShortcut({
      name: "core/block-editor/transform-heading-to-paragraph",
      category: "block-library",
      description: __("Transform heading to paragraph."),
      keyCombination: {
        modifier: "access",
        character: "0"
      },
      aliases: [
        {
          modifier: "access",
          character: "7"
        }
      ]
    });
    [1, 2, 3, 4, 5, 6].forEach((level) => {
      registerShortcut({
        name: `core/block-editor/transform-paragraph-to-heading-${level}`,
        category: "block-library",
        description: __("Transform paragraph to heading."),
        keyCombination: {
          modifier: "access",
          character: `${level}`
        }
      });
    });
  }, [registerShortcut]);
  useShortcut(
    "core/block-editor/transform-heading-to-paragraph",
    (event) => handleTransformHeadingAndParagraph(event, 0)
  );
  useShortcut(
    "core/block-editor/transform-paragraph-to-heading-1",
    (event) => handleTransformHeadingAndParagraph(event, 1)
  );
  useShortcut(
    "core/block-editor/transform-paragraph-to-heading-2",
    (event) => handleTransformHeadingAndParagraph(event, 2)
  );
  useShortcut(
    "core/block-editor/transform-paragraph-to-heading-3",
    (event) => handleTransformHeadingAndParagraph(event, 3)
  );
  useShortcut(
    "core/block-editor/transform-paragraph-to-heading-4",
    (event) => handleTransformHeadingAndParagraph(event, 4)
  );
  useShortcut(
    "core/block-editor/transform-paragraph-to-heading-5",
    (event) => handleTransformHeadingAndParagraph(event, 5)
  );
  useShortcut(
    "core/block-editor/transform-paragraph-to-heading-6",
    (event) => handleTransformHeadingAndParagraph(event, 6)
  );
  return null;
}
var block_keyboard_shortcuts_default = BlockKeyboardShortcuts;
export {
  block_keyboard_shortcuts_default as default
};
//# sourceMappingURL=index.js.map
