Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Bindings: Allow label override when it is defined in client registration #66160

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,14 +854,6 @@ export const registerBlockBindingsSource = ( source ) => {
}

// Check the `label` property is correct.
if ( label && existingSource?.label ) {
warning(
'Block bindings "' +
name +
'" source label is already defined in the server.'
);
return;
}

if ( ! label && ! existingSource?.label ) {
warning( 'Block bindings source must contain a label.' );
Expand All @@ -873,6 +865,10 @@ export const registerBlockBindingsSource = ( source ) => {
return;
}

if ( label && existingSource?.label && label !== existingSource?.label ) {
warning( 'Block bindings "' + name + '" source label was overriden.' );
}

// Check the `usesContext` property is correct.
if ( usesContext && ! Array.isArray( usesContext ) ) {
warning( 'Block bindings source usesContext must be an array.' );
Expand Down
26 changes: 22 additions & 4 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1512,20 +1512,38 @@ describe( 'blocks', () => {
expect( getBlockBindingsSource( 'core/testing' ) ).toBeUndefined();
} );

it( 'should not override label from the server', () => {
it( 'should override label from the server', () => {
// Simulate bootstrap source from the server.
registerBlockBindingsSource( {
name: 'core/server',
name: 'core/testing',
label: 'Server label',
} );
// Override the source with a different label in the client.
registerBlockBindingsSource( {
name: 'core/server',
name: 'core/testing',
label: 'Client label',
} );
expect( console ).toHaveWarnedWith(
'Block bindings "core/server" source label is already defined in the server.'
'Block bindings "core/testing" source label was overriden.'
);
const source = getBlockBindingsSource( 'core/testing' );
unregisterBlockBindingsSource( 'core/testing' );
expect( source.label ).toEqual( 'Client label' );
} );

it( 'should keep label from the server when not defined in the client', () => {
// Simulate bootstrap source from the server.
registerBlockBindingsSource( {
name: 'core/testing',
label: 'Server label',
} );
// Override the source with a different label in the client.
registerBlockBindingsSource( {
name: 'core/testing',
} );
const source = getBlockBindingsSource( 'core/testing' );
unregisterBlockBindingsSource( 'core/testing' );
expect( source.label ).toEqual( 'Server label' );
} );

// Check the `usesContext` array is correct.
Expand Down
3 changes: 1 addition & 2 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,7 @@ export function blockBindingsSources( state = {}, action ) {
return {
...state,
[ action.name ]: {
// Don't override the label if it's already set.
label: state[ action.name ]?.label || action.label,
label: action.label || state[ action.name ]?.label,
usesContext: getMergedUsesContext(
state[ action.name ]?.usesContext,
action.usesContext
Expand Down
Loading