UNPKG

@wordpress/block-editor

Version:
282 lines (281 loc) 7.92 kB
// packages/block-editor/src/components/use-block-commands/index.js import { __, sprintf } from "@wordpress/i18n"; import { hasBlockSupport, store as blocksStore, switchToBlockType, isTemplatePart } from "@wordpress/blocks"; import { useSelect, useDispatch } from "@wordpress/data"; import { useCommandLoader } from "@wordpress/commands"; import { copy, trash as remove, plus as add, group, ungroup, seen, unseen } from "@wordpress/icons"; import BlockIcon from "../block-icon"; import { store as blockEditorStore } from "../../store"; import { cleanEmptyObject } from "../../hooks/utils"; import { jsx } from "react/jsx-runtime"; var getTransformCommands = () => function useTransformCommands() { const { replaceBlocks, multiSelect } = useDispatch(blockEditorStore); const { blocks, clientIds, canRemove, possibleBlockTransformations, invalidSelection } = useSelect((select) => { const { getBlockRootClientId, getBlockTransformItems, getSelectedBlockClientIds, getBlocksByClientId, canRemoveBlocks } = select(blockEditorStore); const selectedBlockClientIds = getSelectedBlockClientIds(); const selectedBlocks = getBlocksByClientId( selectedBlockClientIds ); if (selectedBlocks.filter((block) => !block).length > 0) { return { invalidSelection: true }; } const rootClientId = getBlockRootClientId( selectedBlockClientIds[0] ); return { blocks: selectedBlocks, clientIds: selectedBlockClientIds, possibleBlockTransformations: getBlockTransformItems( selectedBlocks, rootClientId ), canRemove: canRemoveBlocks(selectedBlockClientIds), invalidSelection: false }; }, []); if (invalidSelection) { return { isLoading: false, commands: [] }; } const isTemplate = blocks.length === 1 && isTemplatePart(blocks[0]); function selectForMultipleBlocks(insertedBlocks) { if (insertedBlocks.length > 1) { multiSelect( insertedBlocks[0].clientId, insertedBlocks[insertedBlocks.length - 1].clientId ); } } function onBlockTransform(name) { const newBlocks = switchToBlockType(blocks, name); replaceBlocks(clientIds, newBlocks); selectForMultipleBlocks(newBlocks); } const hasPossibleBlockTransformations = !!possibleBlockTransformations.length && canRemove && !isTemplate; if (!clientIds || clientIds.length < 1 || !hasPossibleBlockTransformations) { return { isLoading: false, commands: [] }; } const commands = possibleBlockTransformations.map( (transformation) => { const { name, title, icon } = transformation; return { name: "core/block-editor/transform-to-" + name.replace("/", "-"), /* translators: %s: Block or block variation name. */ label: sprintf(__("Transform to %s"), title), icon: /* @__PURE__ */ jsx(BlockIcon, { icon }), callback: ({ close }) => { onBlockTransform(name); close(); } }; } ); return { isLoading: false, commands }; }; var getQuickActionsCommands = () => function useQuickActionsCommands() { const { clientIds, isUngroupable, isGroupable } = useSelect( (select) => { const { getSelectedBlockClientIds, isUngroupable: _isUngroupable, isGroupable: _isGroupable } = select(blockEditorStore); const selectedBlockClientIds = getSelectedBlockClientIds(); return { clientIds: selectedBlockClientIds, isUngroupable: _isUngroupable(), isGroupable: _isGroupable() }; }, [] ); const { canInsertBlockType, getBlockRootClientId, getBlocksByClientId, canRemoveBlocks, getBlockName } = useSelect(blockEditorStore); const { getDefaultBlockName, getGroupingBlockName } = useSelect(blocksStore); const blocks = getBlocksByClientId(clientIds); const { removeBlocks, replaceBlocks, duplicateBlocks, insertAfterBlock, insertBeforeBlock, updateBlockAttributes } = useDispatch(blockEditorStore); const onGroup = () => { if (!blocks.length) { return; } const groupingBlockName = getGroupingBlockName(); const newBlocks = switchToBlockType(blocks, groupingBlockName); if (!newBlocks) { return; } replaceBlocks(clientIds, newBlocks); }; const onUngroup = () => { if (!blocks.length) { return; } const innerBlocks = blocks[0].innerBlocks; if (!innerBlocks.length) { return; } replaceBlocks(clientIds, innerBlocks); }; if (!clientIds || clientIds.length < 1) { return { isLoading: false, commands: [] }; } const rootClientId = getBlockRootClientId(clientIds[0]); const canInsertDefaultBlock = canInsertBlockType( getDefaultBlockName(), rootClientId ); const canDuplicate = blocks.every((block) => { return !!block && hasBlockSupport(block.name, "multiple", true) && canInsertBlockType(block.name, rootClientId); }); const canRemove = canRemoveBlocks(clientIds); const canToggleBlockVisibility = blocks.every( ({ clientId }) => hasBlockSupport(getBlockName(clientId), "visibility", true) ); const commands = []; if (canDuplicate) { commands.push({ name: "duplicate", label: __("Duplicate"), callback: () => duplicateBlocks(clientIds, true), icon: copy }); } if (canInsertDefaultBlock) { commands.push( { name: "add-before", label: __("Add before"), callback: () => { const clientId = Array.isArray(clientIds) ? clientIds[0] : clientId; insertBeforeBlock(clientId); }, icon: add }, { name: "add-after", label: __("Add after"), callback: () => { const clientId = Array.isArray(clientIds) ? clientIds[clientIds.length - 1] : clientId; insertAfterBlock(clientId); }, icon: add } ); } if (isGroupable) { commands.push({ name: "Group", label: __("Group"), callback: onGroup, icon: group }); } if (isUngroupable) { commands.push({ name: "ungroup", label: __("Ungroup"), callback: onUngroup, icon: ungroup }); } if (canRemove) { commands.push({ name: "remove", label: __("Delete"), callback: () => removeBlocks(clientIds, true), icon: remove }); } if (canToggleBlockVisibility) { const hasHiddenBlock = blocks.some( (block) => block.attributes.metadata?.blockVisibility === false ); commands.push({ name: "core/toggle-block-visibility", label: hasHiddenBlock ? __("Show") : __("Hide"), callback: () => { const attributesByClientId = Object.fromEntries( blocks?.map(({ clientId, attributes }) => [ clientId, { metadata: cleanEmptyObject({ ...attributes?.metadata, blockVisibility: hasHiddenBlock ? void 0 : false }) } ]) ); updateBlockAttributes(clientIds, attributesByClientId, { uniqueByBlock: true }); }, icon: hasHiddenBlock ? seen : unseen }); } return { isLoading: false, commands: commands.map((command) => ({ ...command, name: "core/block-editor/action-" + command.name, callback: ({ close }) => { command.callback(); close(); } })) }; }; var useBlockCommands = () => { useCommandLoader({ name: "core/block-editor/blockTransforms", hook: getTransformCommands() }); useCommandLoader({ name: "core/block-editor/blockQuickActions", hook: getQuickActionsCommands(), context: "block-selection-edit" }); }; export { useBlockCommands }; //# sourceMappingURL=index.js.map