{"version":3,"file":"navigation-link-ui.mjs","names":["blockEditorStore"],"sources":["../../../src/navigation/edit/navigation-link-ui.jsx"],"sourcesContent":["import { store as blockEditorStore } from '@wordpress/block-editor';\nimport { useDispatch, useSelect } from '@wordpress/data';\nimport {\n\tLinkUI,\n\tupdateAttributes,\n\tuseEntityBinding,\n} from '../../navigation-link/shared';\n\nconst BLOCKS_WITH_LINK_UI_SUPPORT = [\n\t'core/navigation-link',\n\t'core/navigation-submenu',\n];\n\nexport function NavigationLinkUI( {\n\tinsertedBlockClientId,\n\tsetInsertedBlockClientId,\n\tallowedBlockNames,\n} ) {\n\tconst { updateBlockAttributes, removeBlock } =\n\t\tuseDispatch( blockEditorStore );\n\tconst { getBlock } = useSelect( blockEditorStore );\n\n\tconst { insertedBlockName, insertedBlockAttributes } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getBlockName, getBlockAttributes } =\n\t\t\t\tselect( blockEditorStore );\n\n\t\t\treturn {\n\t\t\t\tinsertedBlockName: getBlockName( insertedBlockClientId ),\n\t\t\t\tinsertedBlockAttributes: getBlockAttributes(\n\t\t\t\t\tinsertedBlockClientId\n\t\t\t\t),\n\t\t\t};\n\t\t},\n\t\t[ insertedBlockClientId ]\n\t);\n\n\tconst showLinkControls =\n\t\tBLOCKS_WITH_LINK_UI_SUPPORT?.includes( insertedBlockName );\n\n\t// Get binding utilities for the inserted block\n\tconst { createBinding, clearBinding } = useEntityBinding( {\n\t\tclientId: insertedBlockClientId,\n\t\tattributes: insertedBlockAttributes || {},\n\t} );\n\n\tif ( ! showLinkControls ) {\n\t\treturn null;\n\t}\n\n\t/**\n\t * Cleanup function for auto-inserted Navigation Link blocks.\n\t *\n\t * Removes the block if it has no URL and clears the inserted block state.\n\t * This ensures consistent cleanup behavior across different contexts.\n\t */\n\tconst cleanupInsertedBlock = () => {\n\t\t// Prevent automatic block selection when removing blocks in list view context\n\t\t// This avoids focus stealing that would close the list view and switch to canvas\n\t\tconst shouldAutoSelectBlock = false;\n\n\t\t// Follows the exact same pattern as Navigation Link block's onClose handler\n\t\t// If there is no URL then remove the auto-inserted block to avoid empty blocks\n\t\tif ( ! insertedBlockAttributes?.url && insertedBlockClientId ) {\n\t\t\t// Remove the block entirely to avoid poor UX\n\t\t\t// This matches the Navigation Link block's behavior\n\t\t\tremoveBlock( insertedBlockClientId, shouldAutoSelectBlock );\n\t\t}\n\t\tsetInsertedBlockClientId( null );\n\t};\n\n\tconst setInsertedBlockAttributes =\n\t\t( _insertedBlockClientId ) => ( _updatedAttributes ) => {\n\t\t\tif ( ! _insertedBlockClientId ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tupdateBlockAttributes( _insertedBlockClientId, _updatedAttributes );\n\t\t};\n\n\t// Wrapper function to clean up original block when a new block is selected\n\tconst handleSetInsertedBlock = ( newBlock ) => {\n\t\t// Prevent automatic block selection when removing blocks in list view context\n\t\t// This avoids focus stealing that would close the list view and switch to canvas\n\t\tconst shouldAutoSelectBlock = false;\n\n\t\t// If we have an existing inserted block and a new block is being set,\n\t\t// remove the original block to avoid duplicates.\n\t\t// \"Browse all\" calls this from the main inserter after the link UI has\n\t\t// closed, and closing already removed the original block.\n\t\tif ( newBlock && getBlock( insertedBlockClientId ) ) {\n\t\t\tremoveBlock( insertedBlockClientId, shouldAutoSelectBlock );\n\t\t}\n\t\tsetInsertedBlockClientId( newBlock?.clientId ?? null );\n\t};\n\n\treturn (\n\t\t<LinkUI\n\t\t\tclientId={ insertedBlockClientId }\n\t\t\tlink={ insertedBlockAttributes }\n\t\t\tallowedBlockNames={ allowedBlockNames }\n\t\t\tonBlockInsert={ handleSetInsertedBlock }\n\t\t\tonClose={ () => {\n\t\t\t\t// Use cleanup function\n\t\t\t\tcleanupInsertedBlock();\n\t\t\t} }\n\t\t\tonChange={ ( updatedValue ) => {\n\t\t\t\t// updateAttributes determines the final state and returns metadata\n\t\t\t\tconst { isEntityLink, attributes: updatedAttributes } =\n\t\t\t\t\tupdateAttributes(\n\t\t\t\t\t\tupdatedValue,\n\t\t\t\t\t\tsetInsertedBlockAttributes( insertedBlockClientId ),\n\t\t\t\t\t\tinsertedBlockAttributes\n\t\t\t\t\t);\n\n\t\t\t\t// Handle URL binding based on the final computed state\n\t\t\t\t// Only create bindings for entity links (posts, pages, taxonomies)\n\t\t\t\t// Never create bindings for custom links (manual URLs)\n\t\t\t\tif ( isEntityLink ) {\n\t\t\t\t\tcreateBinding( updatedAttributes );\n\t\t\t\t} else {\n\t\t\t\t\tclearBinding();\n\t\t\t\t}\n\n\t\t\t\tsetInsertedBlockClientId( null );\n\t\t\t} }\n\t\t/>\n\t);\n}\n"],"mappings":";;;;;AAQA,MAAM,8BAA8B,CACnC,wBACA,yBACD;AAEA,SAAgB,iBAAkB,EACjC,uBACA,0BACA,qBACG;CACH,MAAM,EAAE,uBAAuB,gBAC9B,YAAaA,KAAiB;CAC/B,MAAM,EAAE,aAAa,UAAWA,KAAiB;CAEjD,MAAM,EAAE,mBAAmB,4BAA4B,WACpD,WAAY;EACb,MAAM,EAAE,cAAc,uBACrB,OAAQA,KAAiB;EAE1B,OAAO;GACN,mBAAmB,aAAc,qBAAsB;GACvD,yBAAyB,mBACxB,qBACD;EACD;CACD,GACA,CAAE,qBAAsB,CACzB;CAEA,MAAM,mBACL,6BAA6B,SAAU,iBAAkB;CAG1D,MAAM,EAAE,eAAe,iBAAiB,iBAAkB;EACzD,UAAU;EACV,YAAY,2BAA2B,CAAC;CACzC,CAAE;CAEF,IAAK,CAAE,kBACN,OAAO;;;;;;;CASR,MAAM,6BAA6B;EAGlC,MAAM,wBAAwB;EAI9B,IAAK,CAAE,yBAAyB,OAAO,uBAGtC,YAAa,uBAAuB,qBAAsB;EAE3D,yBAA0B,IAAK;CAChC;CAEA,MAAM,8BACH,4BAA8B,uBAAwB;EACvD,IAAK,CAAE,wBACN;EAED,sBAAuB,wBAAwB,kBAAmB;CACnE;CAGD,MAAM,0BAA2B,aAAc;EAG9C,MAAM,wBAAwB;EAM9B,IAAK,YAAY,SAAU,qBAAsB,GAChD,YAAa,uBAAuB,qBAAsB;EAE3D,yBAA0B,UAAU,YAAY,IAAK;CACtD;CAEA,OACC,oBAAC,QAAD;EACC,UAAW;EACX,MAAO;EACa;EACpB,eAAgB;EAChB,eAAgB;GAEf,qBAAqB;EACtB;EACA,WAAa,iBAAkB;GAE9B,MAAM,EAAE,cAAc,YAAY,sBACjC,iBACC,cACA,2BAA4B,qBAAsB,GAClD,uBACD;GAKD,IAAK,cACJ,cAAe,iBAAkB;QAEjC,aAAa;GAGd,yBAA0B,IAAK;EAChC;CACA,CAAA;AAEH"}