@wordpress/block-library
Version:
Block library for the WordPress editor.
130 lines (129 loc) • 5.05 kB
JavaScript
import { createBlock, hasBlockSupport, store } from "@wordpress/blocks";
import { addSubmenu, chevronDown, chevronUp, moreVertical } from "@wordpress/icons";
import { DropdownMenu, MenuGroup, MenuItem } from "@wordpress/components";
import { useDispatch, useSelect } from "@wordpress/data";
import { __, sprintf } from "@wordpress/i18n";
import { BlockTitle, store as store$1 } from "@wordpress/block-editor";
import { DEFAULT_BLOCK } from "../constants.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/navigation/edit/leaf-more-menu.jsx
const POPOVER_PROPS = {
className: "block-editor-block-settings-menu__popover",
placement: "bottom-start"
};
const BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU = ["core/navigation-link", "core/navigation-submenu"];
function AddSubmenuItem({ clientId, onClose, isDisabled, updateExpansion, setInsertedBlockClientId }) {
const { insertBlock, replaceBlock, replaceInnerBlocks } = useDispatch(store$1);
const { getBlock } = useSelect(store$1);
return /* @__PURE__ */ jsx(MenuItem, {
icon: addSubmenu,
disabled: isDisabled,
onClick: () => {
const updateSelectionOnInsert = false;
const newLink = createBlock(DEFAULT_BLOCK.name, DEFAULT_BLOCK.attributes);
const block = getBlock(clientId);
if (block.name === "core/navigation-submenu") insertBlock(newLink, block.innerBlocks.length, clientId, updateSelectionOnInsert);
else {
const newSubmenu = createBlock("core/navigation-submenu", block.attributes, block.innerBlocks);
replaceBlock(clientId, newSubmenu);
replaceInnerBlocks(newSubmenu.clientId, [newLink], updateSelectionOnInsert);
}
setInsertedBlockClientId(newLink.clientId);
updateExpansion({
type: "expand",
clientIds: [clientId]
});
onClose();
},
children: __("Add submenu link")
});
}
function LeafMoreMenu({ clientId, updateExpansion, setInsertedBlockClientId, ...props }) {
const { moveBlocksDown, moveBlocksUp, removeBlocks, duplicateBlocks, insertBeforeBlock, insertAfterBlock } = useDispatch(store$1);
const removeLabel = sprintf(__("Remove %s"), BlockTitle({
clientId,
maximumLength: 25
}));
const { blockName, rootClientId, canDuplicate, canInsertBlock, isFirst, isLast } = useSelect((select) => {
const { getBlockRootClientId, getBlockName, canInsertBlockType, getDirectInsertBlock, getBlockIndex, getBlockCount } = select(store$1);
const { getDefaultBlockName } = select(store);
const _rootClientId = getBlockRootClientId(clientId);
const _blockName = getBlockName(clientId);
const canInsertDefaultBlock = canInsertBlockType(getDefaultBlockName(), _rootClientId);
const directInsertBlock = _rootClientId ? getDirectInsertBlock(_rootClientId) : null;
return {
blockName: _blockName,
rootClientId: _rootClientId,
canDuplicate: !!_blockName && hasBlockSupport(_blockName, "multiple", true) && canInsertBlockType(_blockName, _rootClientId),
canInsertBlock: (canInsertDefaultBlock || !!directInsertBlock) && !!_blockName && canInsertBlockType(_blockName, _rootClientId),
isFirst: getBlockIndex(clientId) === 0,
isLast: getBlockIndex(clientId) === getBlockCount(_rootClientId) - 1
};
}, [clientId]);
const isSubmenuDisabled = !BLOCKS_THAT_CAN_BE_CONVERTED_TO_SUBMENU.includes(blockName);
return /* @__PURE__ */ jsx(DropdownMenu, {
icon: moreVertical,
label: __("Options"),
className: "block-editor-block-settings-menu",
popoverProps: POPOVER_PROPS,
noIcons: true,
...props,
children: ({ onClose }) => /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs(MenuGroup, { children: [
/* @__PURE__ */ jsx(MenuItem, {
icon: chevronUp,
disabled: isFirst,
accessibleWhenDisabled: true,
onClick: () => {
moveBlocksUp([clientId], rootClientId);
onClose();
},
children: __("Move up")
}),
/* @__PURE__ */ jsx(MenuItem, {
icon: chevronDown,
disabled: isLast,
accessibleWhenDisabled: true,
onClick: () => {
moveBlocksDown([clientId], rootClientId);
onClose();
},
children: __("Move down")
}),
/* @__PURE__ */ jsx(AddSubmenuItem, {
clientId,
onClose,
isDisabled: isSubmenuDisabled,
updateExpansion,
setInsertedBlockClientId
}),
canDuplicate && /* @__PURE__ */ jsx(MenuItem, {
onClick: () => {
duplicateBlocks([clientId]);
onClose();
},
children: __("Duplicate")
}),
canInsertBlock && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(MenuItem, {
onClick: () => {
insertBeforeBlock(clientId);
onClose();
},
children: __("Add before")
}), /* @__PURE__ */ jsx(MenuItem, {
onClick: () => {
insertAfterBlock(clientId);
onClose();
},
children: __("Add after")
})] })
] }), /* @__PURE__ */ jsx(MenuGroup, { children: /* @__PURE__ */ jsx(MenuItem, {
onClick: () => {
removeBlocks([clientId], false);
onClose();
},
children: removeLabel
}) })] })
});
}
export { LeafMoreMenu as default };
//# sourceMappingURL=leaf-more-menu.mjs.map