@wordpress/block-library
Version:
Block library for the WordPress editor.
135 lines (134 loc) • 6.23 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_blocks = require("@wordpress/blocks");
let _wordpress_icons = require("@wordpress/icons");
let _wordpress_components = require("@wordpress/components");
let _wordpress_data = require("@wordpress/data");
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_block_editor = require("@wordpress/block-editor");
let ___constants_js = require("../constants.cjs");
let react_jsx_runtime = require("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 } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store);
const { getBlock } = (0, _wordpress_data.useSelect)(_wordpress_block_editor.store);
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.MenuItem, {
icon: _wordpress_icons.addSubmenu,
disabled: isDisabled,
onClick: () => {
const updateSelectionOnInsert = false;
const newLink = (0, _wordpress_blocks.createBlock)(___constants_js.DEFAULT_BLOCK.name, ___constants_js.DEFAULT_BLOCK.attributes);
const block = getBlock(clientId);
if (block.name === "core/navigation-submenu") insertBlock(newLink, block.innerBlocks.length, clientId, updateSelectionOnInsert);
else {
const newSubmenu = (0, _wordpress_blocks.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: (0, _wordpress_i18n.__)("Add submenu link")
});
}
function LeafMoreMenu({ clientId, updateExpansion, setInsertedBlockClientId, ...props }) {
const { moveBlocksDown, moveBlocksUp, removeBlocks, duplicateBlocks, insertBeforeBlock, insertAfterBlock } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store);
const removeLabel = (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Remove %s"), (0, _wordpress_block_editor.BlockTitle)({
clientId,
maximumLength: 25
}));
const { blockName, rootClientId, canDuplicate, canInsertBlock, isFirst, isLast } = (0, _wordpress_data.useSelect)((select) => {
const { getBlockRootClientId, getBlockName, canInsertBlockType, getDirectInsertBlock, getBlockIndex, getBlockCount } = select(_wordpress_block_editor.store);
const { getDefaultBlockName } = select(_wordpress_blocks.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 && (0, _wordpress_blocks.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__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.DropdownMenu, {
icon: _wordpress_icons.moreVertical,
label: (0, _wordpress_i18n.__)("Options"),
className: "block-editor-block-settings-menu",
popoverProps: POPOVER_PROPS,
noIcons: true,
...props,
children: ({ onClose }) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.MenuGroup, { children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.MenuItem, {
icon: _wordpress_icons.chevronUp,
disabled: isFirst,
accessibleWhenDisabled: true,
onClick: () => {
moveBlocksUp([clientId], rootClientId);
onClose();
},
children: (0, _wordpress_i18n.__)("Move up")
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.MenuItem, {
icon: _wordpress_icons.chevronDown,
disabled: isLast,
accessibleWhenDisabled: true,
onClick: () => {
moveBlocksDown([clientId], rootClientId);
onClose();
},
children: (0, _wordpress_i18n.__)("Move down")
}),
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(AddSubmenuItem, {
clientId,
onClose,
isDisabled: isSubmenuDisabled,
updateExpansion,
setInsertedBlockClientId
}),
canDuplicate && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.MenuItem, {
onClick: () => {
duplicateBlocks([clientId]);
onClose();
},
children: (0, _wordpress_i18n.__)("Duplicate")
}),
canInsertBlock && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.MenuItem, {
onClick: () => {
insertBeforeBlock(clientId);
onClose();
},
children: (0, _wordpress_i18n.__)("Add before")
}), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.MenuItem, {
onClick: () => {
insertAfterBlock(clientId);
onClose();
},
children: (0, _wordpress_i18n.__)("Add after")
})] })
] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.MenuGroup, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.MenuItem, {
onClick: () => {
removeBlocks([clientId], false);
onClose();
},
children: removeLabel
}) })] })
});
}
exports.default = LeafMoreMenu;
//# sourceMappingURL=leaf-more-menu.cjs.map