@wordpress/block-library
Version:
Block library for the WordPress editor.
134 lines (132 loc) • 5.14 kB
JavaScript
;
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-library/src/block-keyboard-shortcuts/index.js
var block_keyboard_shortcuts_exports = {};
__export(block_keyboard_shortcuts_exports, {
default: () => block_keyboard_shortcuts_default
});
module.exports = __toCommonJS(block_keyboard_shortcuts_exports);
var import_element = require("@wordpress/element");
var import_data = require("@wordpress/data");
var import_keyboard_shortcuts = require("@wordpress/keyboard-shortcuts");
var import_i18n = require("@wordpress/i18n");
var import_blocks = require("@wordpress/blocks");
var import_block_editor = require("@wordpress/block-editor");
function BlockKeyboardShortcuts() {
const { registerShortcut } = (0, import_data.useDispatch)(import_keyboard_shortcuts.store);
const { replaceBlocks } = (0, import_data.useDispatch)(import_block_editor.store);
const { getBlockName, getSelectedBlockClientId, getBlockAttributes } = (0, import_data.useSelect)(import_block_editor.store);
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,
(0, import_blocks.createBlock)(destinationBlockName, newAttributes)
);
};
(0, import_element.useEffect)(() => {
registerShortcut({
name: "core/block-editor/transform-heading-to-paragraph",
category: "block-library",
description: (0, import_i18n.__)("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: (0, import_i18n.__)("Transform paragraph to heading."),
keyCombination: {
modifier: "access",
character: `${level}`
}
});
});
}, [registerShortcut]);
(0, import_keyboard_shortcuts.useShortcut)(
"core/block-editor/transform-heading-to-paragraph",
(event) => handleTransformHeadingAndParagraph(event, 0)
);
(0, import_keyboard_shortcuts.useShortcut)(
"core/block-editor/transform-paragraph-to-heading-1",
(event) => handleTransformHeadingAndParagraph(event, 1)
);
(0, import_keyboard_shortcuts.useShortcut)(
"core/block-editor/transform-paragraph-to-heading-2",
(event) => handleTransformHeadingAndParagraph(event, 2)
);
(0, import_keyboard_shortcuts.useShortcut)(
"core/block-editor/transform-paragraph-to-heading-3",
(event) => handleTransformHeadingAndParagraph(event, 3)
);
(0, import_keyboard_shortcuts.useShortcut)(
"core/block-editor/transform-paragraph-to-heading-4",
(event) => handleTransformHeadingAndParagraph(event, 4)
);
(0, import_keyboard_shortcuts.useShortcut)(
"core/block-editor/transform-paragraph-to-heading-5",
(event) => handleTransformHeadingAndParagraph(event, 5)
);
(0, import_keyboard_shortcuts.useShortcut)(
"core/block-editor/transform-paragraph-to-heading-6",
(event) => handleTransformHeadingAndParagraph(event, 6)
);
return null;
}
var block_keyboard_shortcuts_default = BlockKeyboardShortcuts;
//# sourceMappingURL=index.js.map