UNPKG

@wordpress/block-library

Version:
61 lines (60 loc) • 2.73 kB
import { store } from "@wordpress/block-editor"; import { useDispatch, useSelect } from "@wordpress/data"; import { LinkUI, updateAttributes, useEntityBinding } from "../../navigation-link/shared/index.mjs"; import { jsx } from "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 } = useDispatch(store); const { getBlock } = useSelect(store); const { insertedBlockName, insertedBlockAttributes } = useSelect((select) => { const { getBlockName, getBlockAttributes } = select(store); return { insertedBlockName: getBlockName(insertedBlockClientId), insertedBlockAttributes: getBlockAttributes(insertedBlockClientId) }; }, [insertedBlockClientId]); const showLinkControls = BLOCKS_WITH_LINK_UI_SUPPORT?.includes(insertedBlockName); const { createBinding, clearBinding } = 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__ */ jsx(LinkUI, { clientId: insertedBlockClientId, link: insertedBlockAttributes, allowedBlockNames, onBlockInsert: handleSetInsertedBlock, onClose: () => { cleanupInsertedBlock(); }, onChange: (updatedValue) => { const { isEntityLink, attributes: updatedAttributes } = updateAttributes(updatedValue, setInsertedBlockAttributes(insertedBlockClientId), insertedBlockAttributes); if (isEntityLink) createBinding(updatedAttributes); else clearBinding(); setInsertedBlockClientId(null); } }); } export { NavigationLinkUI }; //# sourceMappingURL=navigation-link-ui.mjs.map