@wordpress/block-library
Version:
Block library for the WordPress editor.
66 lines (65 loc) • 3.17 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_data = require("@wordpress/data");
let ______navigation_link_shared_index_js = require("../../navigation-link/shared/index.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/navigation/edit/navigation-link-ui.jsx
const BLOCKS_WITH_LINK_UI_SUPPORT = ["core/navigation-link", "core/navigation-submenu"];
function NavigationLinkUI({ insertedBlockClientId, setInsertedBlockClientId, allowedBlockNames }) {
const { updateBlockAttributes, removeBlock } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store);
const { getBlock } = (0, _wordpress_data.useSelect)(_wordpress_block_editor.store);
const { insertedBlockName, insertedBlockAttributes } = (0, _wordpress_data.useSelect)((select) => {
const { getBlockName, getBlockAttributes } = select(_wordpress_block_editor.store);
return {
insertedBlockName: getBlockName(insertedBlockClientId),
insertedBlockAttributes: getBlockAttributes(insertedBlockClientId)
};
}, [insertedBlockClientId]);
const showLinkControls = BLOCKS_WITH_LINK_UI_SUPPORT?.includes(insertedBlockName);
const { createBinding, clearBinding } = (0, ______navigation_link_shared_index_js.useEntityBinding)({
clientId: insertedBlockClientId,
attributes: insertedBlockAttributes || {}
});
if (!showLinkControls) return null;
/**
* Cleanup function for auto-inserted Navigation Link blocks.
*
* Removes the block if it has no URL and clears the inserted block state.
* This ensures consistent cleanup behavior across different contexts.
*/
const cleanupInsertedBlock = () => {
const shouldAutoSelectBlock = false;
if (!insertedBlockAttributes?.url && insertedBlockClientId) removeBlock(insertedBlockClientId, shouldAutoSelectBlock);
setInsertedBlockClientId(null);
};
const setInsertedBlockAttributes = (_insertedBlockClientId) => (_updatedAttributes) => {
if (!_insertedBlockClientId) return;
updateBlockAttributes(_insertedBlockClientId, _updatedAttributes);
};
const handleSetInsertedBlock = (newBlock) => {
const shouldAutoSelectBlock = false;
if (newBlock && getBlock(insertedBlockClientId)) removeBlock(insertedBlockClientId, shouldAutoSelectBlock);
setInsertedBlockClientId(newBlock?.clientId ?? null);
};
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(______navigation_link_shared_index_js.LinkUI, {
clientId: insertedBlockClientId,
link: insertedBlockAttributes,
allowedBlockNames,
onBlockInsert: handleSetInsertedBlock,
onClose: () => {
cleanupInsertedBlock();
},
onChange: (updatedValue) => {
const { isEntityLink, attributes: updatedAttributes } = (0, ______navigation_link_shared_index_js.updateAttributes)(updatedValue, setInsertedBlockAttributes(insertedBlockClientId), insertedBlockAttributes);
if (isEntityLink) createBinding(updatedAttributes);
else clearBinding();
setInsertedBlockClientId(null);
}
});
}
exports.NavigationLinkUI = NavigationLinkUI;
//# sourceMappingURL=navigation-link-ui.cjs.map