Skip to content

Commit

Permalink
Improve block bindings utils
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Dec 4, 2024
1 parent e7e9cda commit ec2d136
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
14 changes: 7 additions & 7 deletions packages/block-editor/src/components/block-edit/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import BlockContext from '../block-context';
import isURLLike from '../link-control/is-url-like';
import {
canBindAttribute,
hasPatternOverridesDefaultBindings,
replacePatternOverrideDefaultBindings,
hasPatternOverridesDefaultBinding,
replacePatternOverridesDefaultBinding,
} from '../../utils/block-bindings';
import { unlock } from '../../lock-unlock';

Expand Down Expand Up @@ -67,7 +67,7 @@ const EditWithGeneratedProps = ( props ) => {

const blockBindings = useMemo(
() =>
replacePatternOverrideDefaultBindings(
replacePatternOverridesDefaultBinding(
name,
attributes?.metadata?.bindings
),
Expand Down Expand Up @@ -179,7 +179,7 @@ const EditWithGeneratedProps = ( props ) => {
]
);

const hasPatternOverrideDefault = hasPatternOverridesDefaultBindings(
const hasPatternOverrides = hasPatternOverridesDefaultBinding(
attributes?.metadata?.bindings
);
const setBoundAttributes = useCallback(
Expand Down Expand Up @@ -239,11 +239,11 @@ const EditWithGeneratedProps = ( props ) => {
if (
// Don't update non-connected attributes if the block is using pattern overrides
// and the editing is happening while overriding the pattern (not editing the original).
! ( hasPatternOverrideDefault && hasParentPattern ) &&
! ( hasPatternOverrides && hasParentPattern ) &&
Object.keys( keptAttributes ).length
) {
// Don't update caption and href until they are supported.
if ( hasPatternOverrideDefault ) {
if ( hasPatternOverrides ) {
delete keptAttributes.caption;
delete keptAttributes.href;
}
Expand All @@ -255,7 +255,7 @@ const EditWithGeneratedProps = ( props ) => {
blockBindings,
clientId,
context,
hasPatternOverrideDefault,
hasPatternOverrides,
setAttributes,
registeredSources,
name,
Expand Down
49 changes: 35 additions & 14 deletions packages/block-editor/src/utils/block-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,34 @@ const BLOCK_BINDINGS_ALLOWED_BLOCKS = {
'core/button': [ 'url', 'text', 'linkTarget', 'rel' ],
};

/**
* Checks if the given object is empty.
*
* @param {?Object} object The object to check.
*
* @return {boolean} Whether the object is empty.
*/
function isObjectEmpty( object ) {
return ! object || Object.keys( object ).length === 0;
}

/**
* Based on the given block name,
* check if it is possible to bind the block.
* Based on the given block name, checks if it is possible to bind the block.
*
* @param {string} blockName The name of the block.
*
* @param {string} blockName - The block name.
* @return {boolean} Whether it is possible to bind the block to sources.
*/
export function canBindBlock( blockName ) {
return blockName in BLOCK_BINDINGS_ALLOWED_BLOCKS;
}

/**
* Based on the given block name and attribute name,
* check if it is possible to bind the block attribute.
* Based on the given block name and attribute name, checks if it is possible to bind the block attribute.
*
* @param {string} blockName The name of the block.
* @param {string} attributeName The name of attribute.
*
* @param {string} blockName - The block name.
* @param {string} attributeName - The attribute name.
* @return {boolean} Whether it is possible to bind the block attribute.
*/
export function canBindAttribute( blockName, attributeName ) {
Expand All @@ -48,29 +55,43 @@ export function canBindAttribute( blockName, attributeName ) {
);
}

/**
* Gets the bindable attributes for a given block.
*
* @param {string} blockName The name of the block.
*
* @return {string[]} The bindable attributes for the block.
*/
export function getBindableAttributes( blockName ) {
return BLOCK_BINDINGS_ALLOWED_BLOCKS[ blockName ];
}

export function hasPatternOverridesDefaultBindings( bindings ) {
/**
* Checks if the block has the `__default` binding for pattern overrides.
*
* @param {?Record<string, object>} bindings A block's bindings from the metadata attribute.
*
* @return {boolean} Whether the block has the `__default` binding for pattern overrides.
*/
export function hasPatternOverridesDefaultBinding( bindings ) {
return bindings?.[ DEFAULT_ATTRIBUTE ]?.source === PATTERN_OVERRIDES_SOURCE;
}

/**
* Returns the bindings with the `__default` binding for pattern overrides
* replaced with the full-set of supported attributes. e.g.:
*
* bindings passed in: `{ __default: { source: 'core/pattern-overrides' } }`
* bindings returned: `{ content: { source: 'core/pattern-overrides' } }`
* - bindings passed in: `{ __default: { source: 'core/pattern-overrides' } }`
* - bindings returned: `{ content: { source: 'core/pattern-overrides' } }`
*
* @param {string} blockName The block name (e.g. 'core/paragraph').
* @param {Object} bindings A block's bindings from the metadata attribute.
* @param {string} blockName The block name (e.g. 'core/paragraph').
* @param {?Record<string, object>} bindings A block's bindings from the metadata attribute.
*
* @return {Object} The bindings with default replaced for pattern overrides.
*/
export function replacePatternOverrideDefaultBindings( blockName, bindings ) {
export function replacePatternOverridesDefaultBinding( blockName, bindings ) {
// The `__default` binding currently only works for pattern overrides.
if ( hasPatternOverridesDefaultBindings( bindings ) ) {
if ( hasPatternOverridesDefaultBinding( bindings ) ) {
const supportedAttributes = BLOCK_BINDINGS_ALLOWED_BLOCKS[ blockName ];
const bindingsWithDefaults = {};
for ( const attributeName of supportedAttributes ) {
Expand Down

0 comments on commit ec2d136

Please sign in to comment.