forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Bindings: Create utils to update or remove bindings (WordPress#…
…64102) * Add `useBlockBindingsUtils` * Use utils in custom fields UI * Use utils in pattern overrides * Properly populate custom fields * Add comments * Update wrong comment Co-authored-by: SantosGuillamot <santosguillamot@git.wordpress.org> Co-authored-by: artemiomorales <artemiosans@git.wordpress.org> Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org>
- Loading branch information
Showing
4 changed files
with
144 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../store'; | ||
import { useBlockEditContext } from '../components/block-edit'; | ||
|
||
export function useBlockBindingsUtils() { | ||
const { clientId } = useBlockEditContext(); | ||
const { updateBlockAttributes } = useDispatch( blockEditorStore ); | ||
const { getBlockAttributes } = useSelect( blockEditorStore ); | ||
|
||
/** | ||
* Updates the value of the bindings connected to block attributes. | ||
* It removes the binding when the new value is `undefined`. | ||
* | ||
* @param {Object} bindings Bindings including the attributes to update and the new object. | ||
* @param {string} bindings.source The source name to connect to. | ||
* @param {Object} [bindings.args] Object containing the arguments needed by the source. | ||
* | ||
* @example | ||
* ```js | ||
* import { useBlockBindingsUtils } from '@wordpress/block-editor' | ||
* | ||
* const { updateBlockBindings } = useBlockBindingsUtils(); | ||
* updateBlockBindings( { | ||
* url: { | ||
* source: 'core/post-meta', | ||
* args: { | ||
* key: 'url_custom_field', | ||
* }, | ||
* }, | ||
* alt: { | ||
* source: 'core/post-meta', | ||
* args: { | ||
* key: 'text_custom_field', | ||
* }, | ||
* } | ||
* } ); | ||
* ``` | ||
*/ | ||
const updateBlockBindings = ( bindings ) => { | ||
const { metadata } = getBlockAttributes( clientId ); | ||
const newBindings = { ...metadata?.bindings }; | ||
Object.entries( bindings ).forEach( ( [ attribute, binding ] ) => { | ||
if ( ! binding && newBindings[ attribute ] ) { | ||
delete newBindings[ attribute ]; | ||
return; | ||
} | ||
newBindings[ attribute ] = binding; | ||
} ); | ||
|
||
const newMetadata = { | ||
...metadata, | ||
bindings: newBindings, | ||
}; | ||
|
||
if ( Object.keys( newMetadata.bindings ).length === 0 ) { | ||
delete newMetadata.bindings; | ||
} | ||
|
||
updateBlockAttributes( clientId, { | ||
metadata: | ||
Object.keys( newMetadata ).length === 0 | ||
? undefined | ||
: newMetadata, | ||
} ); | ||
}; | ||
|
||
/** | ||
* Removes the bindings property of the `metadata` attribute. | ||
* | ||
* @example | ||
* ```js | ||
* import { useBlockBindingsUtils } from '@wordpress/block-editor' | ||
* | ||
* const { removeAllBlockBindings } = useBlockBindingsUtils(); | ||
* removeAllBlockBindings(); | ||
* ``` | ||
*/ | ||
const removeAllBlockBindings = () => { | ||
const { metadata } = getBlockAttributes( clientId ); | ||
const newMetadata = { ...metadata }; | ||
delete newMetadata.bindings; | ||
updateBlockAttributes( clientId, { | ||
metadata: | ||
Object.keys( newMetadata ).length === 0 | ||
? undefined | ||
: newMetadata, | ||
} ); | ||
}; | ||
|
||
return { updateBlockBindings, removeAllBlockBindings }; | ||
} |
Oops, something went wrong.