Skip to content

Commit

Permalink
Block Bindings: Allow bindings bootstrap after registration (#63797)
Browse files Browse the repository at this point in the history
* Add unit test

* Keep existing properties in bootstrap

* Update test description

Co-authored-by: Greg Ziółkowski <[email protected]>

* Reuse mergedContext

* Improve `mergedUsesContext` definition

---------

Co-authored-by: SantosGuillamot <[email protected]>
Co-authored-by: artemiomorales <[email protected]>
Co-authored-by: gziolo <[email protected]>
  • Loading branch information
4 people authored Jul 25, 2024
1 parent 3dfdfef commit 1055e02
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
37 changes: 37 additions & 0 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,43 @@ describe( 'blocks', () => {
'Block bindings source "core/test-source" is already registered.'
);
} );

it( 'should correctly merge properties when bootstrap happens after registration', () => {
// Register source in the client.
const clientOnlyProperties = {
getValues: () => 'values',
setValues: () => 'new values',
getPlaceholder: () => 'placeholder',
canUserEditValue: () => true,
};
registerBlockBindingsSource( {
name: 'core/custom-source',
label: 'Client Label',
usesContext: [ 'postId', 'postType' ],
...clientOnlyProperties,
} );

// Bootstrap source from the server.
unlock(
dispatch( blocksStore )
).addBootstrappedBlockBindingsSource( {
name: 'core/custom-source',
label: 'Server Label',
usesContext: [ 'postId', 'serverContext' ],
} );

// Check that the bootstrap values prevail and the client properties are still there.
expect( getBlockBindingsSource( 'core/custom-source' ) ).toEqual( {
// Should use the server label.
label: 'Server Label',
// Should merge usesContext from server and client.
usesContext: [ 'postId', 'postType', 'serverContext' ],
// Should keep client properties.
...clientOnlyProperties,
} );

unregisterBlockBindingsSource( 'core/custom-source' );
} );
} );

describe( 'unregisterBlockBindingsSource', () => {
Expand Down
27 changes: 15 additions & 12 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,19 +372,17 @@ export function collections( state = {}, action ) {
}

export function blockBindingsSources( state = {}, action ) {
// Merge usesContext with existing values, potentially defined in the server registration.
const existingUsesContext = state[ action.name ]?.usesContext || [];
const newUsesContext = action.usesContext || [];
const mergedArrays = Array.from(
new Set( existingUsesContext.concat( newUsesContext ) )
);
const mergedUsesContext =
mergedArrays.length > 0 ? mergedArrays : undefined;

switch ( action.type ) {
case 'ADD_BLOCK_BINDINGS_SOURCE':
// Merge usesContext with existing values, potentially defined in the server registration.
let mergedUsesContext = [
...( state[ action.name ]?.usesContext || [] ),
...( action.usesContext || [] ),
];
// Remove duplicates.
mergedUsesContext =
mergedUsesContext.length > 0
? [ ...new Set( mergedUsesContext ) ]
: undefined;

return {
...state,
[ action.name ]: {
Expand All @@ -401,8 +399,13 @@ export function blockBindingsSources( state = {}, action ) {
return {
...state,
[ action.name ]: {
/*
* Keep the exisitng properties in case the source has been registered
* in the client before bootstrapping.
*/
...state[ action.name ],
label: action.label,
usesContext: action.usesContext,
usesContext: mergedUsesContext,
},
};
case 'REMOVE_BLOCK_BINDINGS_SOURCE':
Expand Down

0 comments on commit 1055e02

Please sign in to comment.