@wordpress/block-library
Version:
Block library for the WordPress editor.
242 lines (240 loc) • 8.35 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/navigation/edit/leaf-more-menu.js
var leaf_more_menu_exports = {};
__export(leaf_more_menu_exports, {
default: () => LeafMoreMenu
});
module.exports = __toCommonJS(leaf_more_menu_exports);
var import_blocks = require("@wordpress/blocks");
var import_icons = require("@wordpress/icons");
var import_components = require("@wordpress/components");
var import_data = require("@wordpress/data");
var import_i18n = require("@wordpress/i18n");
var import_block_editor = require("@wordpress/block-editor");
var import_constants = require("../constants.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
var POPOVER_PROPS = {
className: "block-editor-block-settings-menu__popover",
placement: "bottom-start"
};
var 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, import_data.useDispatch)(import_block_editor.store);
const { getBlock } = (0, import_data.useSelect)(import_block_editor.store);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.MenuItem,
{
icon: import_icons.addSubmenu,
disabled: isDisabled,
onClick: () => {
const updateSelectionOnInsert = false;
const newLink = (0, import_blocks.createBlock)(
import_constants.DEFAULT_BLOCK.name,
import_constants.DEFAULT_BLOCK.attributes
);
const block = getBlock(clientId);
if (block.name === "core/navigation-submenu") {
insertBlock(
newLink,
block.innerBlocks.length,
clientId,
updateSelectionOnInsert
);
} else {
const newSubmenu = (0, import_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, import_i18n.__)("Add submenu link")
}
);
}
function LeafMoreMenu({
clientId,
updateExpansion,
setInsertedBlockClientId,
...props
}) {
const {
moveBlocksDown,
moveBlocksUp,
removeBlocks,
duplicateBlocks,
insertBeforeBlock,
insertAfterBlock
} = (0, import_data.useDispatch)(import_block_editor.store);
const removeLabel = (0, import_i18n.sprintf)(
/* translators: %s: block name */
(0, import_i18n.__)("Remove %s"),
(0, import_block_editor.BlockTitle)({ clientId, maximumLength: 25 })
);
const {
blockName,
rootClientId,
canDuplicate,
canInsertBlock,
isFirst,
isLast
} = (0, import_data.useSelect)(
(select) => {
const {
getBlockRootClientId,
getBlockName,
canInsertBlockType,
getDirectInsertBlock,
getBlockIndex,
getBlockCount
} = select(import_block_editor.store);
const { getDefaultBlockName } = select(import_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, import_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, import_jsx_runtime.jsx)(
import_components.DropdownMenu,
{
icon: import_icons.moreVertical,
label: (0, import_i18n.__)("Options"),
className: "block-editor-block-settings-menu",
popoverProps: POPOVER_PROPS,
noIcons: true,
...props,
children: ({ onClose }) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.MenuGroup, { children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.MenuItem,
{
icon: import_icons.chevronUp,
disabled: isFirst,
accessibleWhenDisabled: true,
onClick: () => {
moveBlocksUp([clientId], rootClientId);
onClose();
},
children: (0, import_i18n.__)("Move up")
}
),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.MenuItem,
{
icon: import_icons.chevronDown,
disabled: isLast,
accessibleWhenDisabled: true,
onClick: () => {
moveBlocksDown([clientId], rootClientId);
onClose();
},
children: (0, import_i18n.__)("Move down")
}
),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
AddSubmenuItem,
{
clientId,
onClose,
isDisabled: isSubmenuDisabled,
updateExpansion,
setInsertedBlockClientId
}
),
canDuplicate && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.MenuItem,
{
onClick: () => {
duplicateBlocks([clientId]);
onClose();
},
children: (0, import_i18n.__)("Duplicate")
}
),
canInsertBlock && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.MenuItem,
{
onClick: () => {
insertBeforeBlock(clientId);
onClose();
},
children: (0, import_i18n.__)("Add before")
}
),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.MenuItem,
{
onClick: () => {
insertAfterBlock(clientId);
onClose();
},
children: (0, import_i18n.__)("Add after")
}
)
] })
] }),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.MenuGroup, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_components.MenuItem,
{
onClick: () => {
removeBlocks([clientId], false);
onClose();
},
children: removeLabel
}
) })
] })
}
);
}
//# sourceMappingURL=leaf-more-menu.cjs.map