UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 6.21 kB
{ "version": 3, "sources": ["../../../src/components/block-bindings/use-block-bindings-utils.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useDispatch, useRegistry } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../../store';\nimport { useBlockEditContext } from '../block-edit';\n\n/**\n * Checks if the given object is empty.\n *\n * @param {?Object} object The object to check.\n *\n * @return {boolean} Whether the object is empty.\n */\nfunction isObjectEmpty( object ) {\n\treturn ! object || Object.keys( object ).length === 0;\n}\n\n/**\n * Contains utils to update the block `bindings` metadata.\n *\n * @typedef {Object} WPBlockBindingsUtils\n *\n * @property {Function} updateBlockBindings Updates the value of the bindings connected to block attributes.\n * @property {Function} removeAllBlockBindings Removes the bindings property of the `metadata` attribute.\n */\n\n/**\n * Retrieves the existing utils needed to update the block `bindings` metadata.\n * They can be used to create, modify, or remove connections from the existing block attributes.\n *\n * It contains the following utils:\n * - `updateBlockBindings`: Updates the value of the bindings connected to block attributes. It can be used to remove a specific binding by setting the value to `undefined`.\n * - `removeAllBlockBindings`: Removes the bindings property of the `metadata` attribute.\n *\n * @since 6.7.0 Introduced in WordPress core.\n *\n * @param {?string} clientId Optional block client ID. If not set, it will use the current block client ID from the context.\n *\n * @return {?WPBlockBindingsUtils} Object containing the block bindings utils.\n *\n * @example\n * ```js\n * import { useBlockBindingsUtils } from '@wordpress/block-editor'\n * const { updateBlockBindings, removeAllBlockBindings } = useBlockBindingsUtils();\n *\n * // Update url and alt attributes.\n * updateBlockBindings( {\n * url: {\n * source: 'core/post-meta',\n * args: {\n * key: 'url_custom_field',\n * },\n * },\n * alt: {\n * source: 'core/post-meta',\n * args: {\n * key: 'text_custom_field',\n * },\n * },\n * } );\n *\n * // Remove binding from url attribute.\n * updateBlockBindings( { url: undefined } );\n *\n * // Remove bindings from all attributes.\n * removeAllBlockBindings();\n * ```\n */\nexport default function useBlockBindingsUtils( clientId ) {\n\tconst { clientId: contextClientId } = useBlockEditContext();\n\tconst blockClientId = clientId || contextClientId;\n\tconst { updateBlockAttributes } = useDispatch( blockEditorStore );\n\tconst { getBlockAttributes } = useRegistry().select( blockEditorStore );\n\n\t/**\n\t * Updates the value of the bindings connected to block attributes.\n\t * It removes the binding when the new value is `undefined`.\n\t *\n\t * @param {Object} bindings Bindings including the attributes to update and the new object.\n\t * @param {string} bindings.source The source name to connect to.\n\t * @param {Object} [bindings.args] Object containing the arguments needed by the source.\n\t *\n\t * @example\n\t * ```js\n\t * import { useBlockBindingsUtils } from '@wordpress/block-editor'\n\t *\n\t * const { updateBlockBindings } = useBlockBindingsUtils();\n\t * updateBlockBindings( {\n\t * url: {\n\t * source: 'core/post-meta',\n\t * args: {\n\t * key: 'url_custom_field',\n\t * },\n\t * \t },\n\t * alt: {\n\t * source: 'core/post-meta',\n\t * args: {\n\t * key: 'text_custom_field',\n\t * },\n\t * \t }\n\t * } );\n\t * ```\n\t */\n\tconst updateBlockBindings = ( bindings ) => {\n\t\tconst { metadata: { bindings: currentBindings, ...metadata } = {} } =\n\t\t\tgetBlockAttributes( blockClientId );\n\t\tconst newBindings = { ...currentBindings };\n\n\t\tObject.entries( bindings ).forEach( ( [ attribute, binding ] ) => {\n\t\t\tif ( ! binding && newBindings[ attribute ] ) {\n\t\t\t\tdelete newBindings[ attribute ];\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tnewBindings[ attribute ] = binding;\n\t\t} );\n\n\t\tconst newMetadata = {\n\t\t\t...metadata,\n\t\t\tbindings: newBindings,\n\t\t};\n\n\t\tif ( isObjectEmpty( newMetadata.bindings ) ) {\n\t\t\tdelete newMetadata.bindings;\n\t\t}\n\n\t\tupdateBlockAttributes( blockClientId, {\n\t\t\tmetadata: isObjectEmpty( newMetadata ) ? undefined : newMetadata,\n\t\t} );\n\t};\n\n\t/**\n\t * Removes the bindings property of the `metadata` attribute.\n\t *\n\t * @example\n\t * ```js\n\t * import { useBlockBindingsUtils } from '@wordpress/block-editor'\n\t *\n\t * const { removeAllBlockBindings } = useBlockBindingsUtils();\n\t * removeAllBlockBindings();\n\t * ```\n\t */\n\tconst removeAllBlockBindings = () => {\n\t\tconst { metadata: { bindings, ...metadata } = {} } =\n\t\t\tgetBlockAttributes( blockClientId );\n\t\tupdateBlockAttributes( blockClientId, {\n\t\t\tmetadata: isObjectEmpty( metadata ) ? undefined : metadata,\n\t\t} );\n\t};\n\n\treturn { updateBlockBindings, removeAllBlockBindings };\n}\n"], "mappings": ";AAGA,SAAS,aAAa,mBAAmB;AAKzC,SAAS,SAAS,wBAAwB;AAC1C,SAAS,2BAA2B;AASpC,SAAS,cAAe,QAAS;AAChC,SAAO,CAAE,UAAU,OAAO,KAAM,MAAO,EAAE,WAAW;AACrD;AAqDe,SAAR,sBAAwC,UAAW;AACzD,QAAM,EAAE,UAAU,gBAAgB,IAAI,oBAAoB;AAC1D,QAAM,gBAAgB,YAAY;AAClC,QAAM,EAAE,sBAAsB,IAAI,YAAa,gBAAiB;AAChE,QAAM,EAAE,mBAAmB,IAAI,YAAY,EAAE,OAAQ,gBAAiB;AA+BtE,QAAM,sBAAsB,CAAE,aAAc;AAC3C,UAAM,EAAE,UAAU,EAAE,UAAU,iBAAiB,GAAG,SAAS,IAAI,CAAC,EAAE,IACjE,mBAAoB,aAAc;AACnC,UAAM,cAAc,EAAE,GAAG,gBAAgB;AAEzC,WAAO,QAAS,QAAS,EAAE,QAAS,CAAE,CAAE,WAAW,OAAQ,MAAO;AACjE,UAAK,CAAE,WAAW,YAAa,SAAU,GAAI;AAC5C,eAAO,YAAa,SAAU;AAC9B;AAAA,MACD;AACA,kBAAa,SAAU,IAAI;AAAA,IAC5B,CAAE;AAEF,UAAM,cAAc;AAAA,MACnB,GAAG;AAAA,MACH,UAAU;AAAA,IACX;AAEA,QAAK,cAAe,YAAY,QAAS,GAAI;AAC5C,aAAO,YAAY;AAAA,IACpB;AAEA,0BAAuB,eAAe;AAAA,MACrC,UAAU,cAAe,WAAY,IAAI,SAAY;AAAA,IACtD,CAAE;AAAA,EACH;AAaA,QAAM,yBAAyB,MAAM;AACpC,UAAM,EAAE,UAAU,EAAE,UAAU,GAAG,SAAS,IAAI,CAAC,EAAE,IAChD,mBAAoB,aAAc;AACnC,0BAAuB,eAAe;AAAA,MACrC,UAAU,cAAe,QAAS,IAAI,SAAY;AAAA,IACnD,CAAE;AAAA,EACH;AAEA,SAAO,EAAE,qBAAqB,uBAAuB;AACtD;", "names": [] }