Skip to content

Commit

Permalink
Switch to declaring the binding on the attribute itself
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan committed Nov 25, 2024
1 parent f2efc94 commit 72e3c3f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ Displays the name of this site. Update the block, and the changes apply everywhe

- **Name:** core/site-title
- **Category:** theme
- **Supports:** align (full, wide), blockBindings (content), color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Supports:** align (full, wide), color (background, gradients, link, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** content, isLink, level, levelOptions, linkTarget, textAlign

## Social Icon
Expand Down
33 changes: 21 additions & 12 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/**
* WordPress dependencies
*/
import {
store as blocksStore,
hasBlockSupport,
getBlockType,
} from '@wordpress/blocks';
import { store as blocksStore, getBlockType } from '@wordpress/blocks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useRegistry, useSelect } from '@wordpress/data';
import { useCallback, useMemo, useContext } from '@wordpress/element';
Expand Down Expand Up @@ -71,15 +67,28 @@ function replacePatternOverrideDefaultBindings( blockName, bindings ) {
return bindings;
}

function addBlockSupportsBindings( blockName, bindings ) {
/**
* Adds block type bindings defined in block.json to the existing bindings object.
* Block type bindings are defined in the block's attributes using the `binding` property.
*
* @param {string} blockName The name of the block (e.g. 'core/paragraph')
* @param {Object} bindings The existing bindings object to add to.
*
* @return {Object} The merged bindings object containing both existing and block type bindings
*/
function addBlockTypeBindings( blockName, bindings ) {
const settings = getBlockType( blockName );
if ( ! hasBlockSupport( settings, 'blockBindings' ) ) {
return bindings;
}
const supportsBindings = settings.supports.blockBindings;
const attributes = settings.attributes ?? {};
const blockTypeBindings = {};
Object.keys( attributes ).forEach( ( attributeName ) => {
if ( attributes[ attributeName ].binding ) {
blockTypeBindings[ attributeName ] =
attributes[ attributeName ].binding;
}
} );
return {
...bindings,
...supportsBindings,
...blockTypeBindings,
};
}

Expand Down Expand Up @@ -127,7 +136,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
props.attributes.metadata?.bindings
);

return addBlockSupportsBindings( name, bindings );
return addBlockTypeBindings( name, bindings );
}, [ props.attributes.metadata?.bindings, name ] );

// While this hook doesn't directly call any selectors, `useSelect` is
Expand Down
16 changes: 7 additions & 9 deletions packages/block-library/src/site-title/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
"type": "rich-text",
"source": "rich-text",
"selector": "p,h1,h2,h3,h4,h5,h6",
"role": "content"
"role": "content",
"binding": {
"source": "core/site",
"args": {
"key": "title"
}
}
},
"level": {
"type": "number",
Expand All @@ -38,14 +44,6 @@
},
"supports": {
"align": [ "wide", "full" ],
"blockBindings": {
"content": {
"source": "core/site",
"args": {
"key": "title"
}
}
},
"html": false,
"color": {
"gradients": true,
Expand Down

0 comments on commit 72e3c3f

Please sign in to comment.