UNPKG

@wordpress/block-library

Version:
8 lines (7 loc) 4.07 kB
{ "version": 3, "sources": ["../../../src/navigation-link/shared/use-handle-link-change.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useCallback } from '@wordpress/element';\nimport { useDispatch } from '@wordpress/data';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\n\n/**\n * Internal dependencies\n */\nimport { updateAttributes } from './update-attributes';\nimport { useEntityBinding } from './use-entity-binding';\n\n/**\n * Custom hook that returns a callback for handling link selection/change.\n * Manages the transition between entity links and custom links,\n * including proper binding creation and cleanup.\n *\n * @param {Object} options - Configuration options\n * @param {string} options.clientId - Block client ID\n * @param {Object} options.attributes - Current block attributes\n * @param {Function} options.setAttributes - Standard setAttribute function\n * @return {Function} Callback function to handle link changes\n */\nexport function useHandleLinkChange( { clientId, attributes, setAttributes } ) {\n\tconst { updateBlockAttributes } = useDispatch( blockEditorStore );\n\tconst { hasUrlBinding, createBinding, clearBinding } = useEntityBinding( {\n\t\tclientId,\n\t\tattributes,\n\t} );\n\n\treturn useCallback(\n\t\t( updatedLink ) => {\n\t\t\tif ( ! updatedLink ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst attrs = {\n\t\t\t\turl: updatedLink.url,\n\t\t\t\tkind: updatedLink.kind,\n\t\t\t\ttype: updatedLink.type,\n\t\t\t\tid: updatedLink.id,\n\t\t\t};\n\n\t\t\t// Only include title when there's no existing label\n\t\t\t// This preserves user-customized labels when updating links\n\t\t\tif ( ! attributes.label || attributes.label === '' ) {\n\t\t\t\tattrs.title = updatedLink.title;\n\t\t\t}\n\n\t\t\t// Check if transitioning from entity to custom link\n\t\t\tconst willBeCustomLink = ! updatedLink.id && hasUrlBinding;\n\n\t\t\tif ( willBeCustomLink ) {\n\t\t\t\t// Clear the binding first\n\t\t\t\tclearBinding();\n\n\t\t\t\t// Use direct store dispatch to bypass setBoundAttributes wrapper\n\t\t\t\t// which prevents updates to bound attributes.\n\t\t\t\tupdateBlockAttributes( clientId, {\n\t\t\t\t\turl: updatedLink.url,\n\t\t\t\t\tkind: 'custom',\n\t\t\t\t\ttype: 'custom',\n\t\t\t\t\tid: undefined,\n\t\t\t\t} );\n\t\t\t} else {\n\t\t\t\t// Normal flow for entity links or unbound custom links\n\t\t\t\tconst { isEntityLink, attributes: updatedAttributes } =\n\t\t\t\t\tupdateAttributes( attrs, setAttributes, attributes );\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\t\t\t}\n\t\t},\n\t\t[\n\t\t\tattributes,\n\t\t\tclientId,\n\t\t\thasUrlBinding,\n\t\t\tcreateBinding,\n\t\t\tclearBinding,\n\t\t\tsetAttributes,\n\t\t\tupdateBlockAttributes,\n\t\t]\n\t);\n}\n"], "mappings": ";AAGA,SAAS,mBAAmB;AAC5B,SAAS,mBAAmB;AAC5B,SAAS,SAAS,wBAAwB;AAK1C,SAAS,wBAAwB;AACjC,SAAS,wBAAwB;AAa1B,SAAS,oBAAqB,EAAE,UAAU,YAAY,cAAc,GAAI;AAC9E,QAAM,EAAE,sBAAsB,IAAI,YAAa,gBAAiB;AAChE,QAAM,EAAE,eAAe,eAAe,aAAa,IAAI,iBAAkB;AAAA,IACxE;AAAA,IACA;AAAA,EACD,CAAE;AAEF,SAAO;AAAA,IACN,CAAE,gBAAiB;AAClB,UAAK,CAAE,aAAc;AACpB;AAAA,MACD;AAEA,YAAM,QAAQ;AAAA,QACb,KAAK,YAAY;AAAA,QACjB,MAAM,YAAY;AAAA,QAClB,MAAM,YAAY;AAAA,QAClB,IAAI,YAAY;AAAA,MACjB;AAIA,UAAK,CAAE,WAAW,SAAS,WAAW,UAAU,IAAK;AACpD,cAAM,QAAQ,YAAY;AAAA,M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