Skip to content

Commit 1055e02

Browse files
SantosGuillamotartemiomoralesgziolo
authored
Block Bindings: Allow bindings bootstrap after registration (#63797)
* 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]>
1 parent 3dfdfef commit 1055e02

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

packages/blocks/src/api/test/registration.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,43 @@ describe( 'blocks', () => {
17031703
'Block bindings source "core/test-source" is already registered.'
17041704
);
17051705
} );
1706+
1707+
it( 'should correctly merge properties when bootstrap happens after registration', () => {
1708+
// Register source in the client.
1709+
const clientOnlyProperties = {
1710+
getValues: () => 'values',
1711+
setValues: () => 'new values',
1712+
getPlaceholder: () => 'placeholder',
1713+
canUserEditValue: () => true,
1714+
};
1715+
registerBlockBindingsSource( {
1716+
name: 'core/custom-source',
1717+
label: 'Client Label',
1718+
usesContext: [ 'postId', 'postType' ],
1719+
...clientOnlyProperties,
1720+
} );
1721+
1722+
// Bootstrap source from the server.
1723+
unlock(
1724+
dispatch( blocksStore )
1725+
).addBootstrappedBlockBindingsSource( {
1726+
name: 'core/custom-source',
1727+
label: 'Server Label',
1728+
usesContext: [ 'postId', 'serverContext' ],
1729+
} );
1730+
1731+
// Check that the bootstrap values prevail and the client properties are still there.
1732+
expect( getBlockBindingsSource( 'core/custom-source' ) ).toEqual( {
1733+
// Should use the server label.
1734+
label: 'Server Label',
1735+
// Should merge usesContext from server and client.
1736+
usesContext: [ 'postId', 'postType', 'serverContext' ],
1737+
// Should keep client properties.
1738+
...clientOnlyProperties,
1739+
} );
1740+
1741+
unregisterBlockBindingsSource( 'core/custom-source' );
1742+
} );
17061743
} );
17071744

17081745
describe( 'unregisterBlockBindingsSource', () => {

packages/blocks/src/store/reducer.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -372,19 +372,17 @@ export function collections( state = {}, action ) {
372372
}
373373

374374
export function blockBindingsSources( state = {}, action ) {
375+
// Merge usesContext with existing values, potentially defined in the server registration.
376+
const existingUsesContext = state[ action.name ]?.usesContext || [];
377+
const newUsesContext = action.usesContext || [];
378+
const mergedArrays = Array.from(
379+
new Set( existingUsesContext.concat( newUsesContext ) )
380+
);
381+
const mergedUsesContext =
382+
mergedArrays.length > 0 ? mergedArrays : undefined;
383+
375384
switch ( action.type ) {
376385
case 'ADD_BLOCK_BINDINGS_SOURCE':
377-
// Merge usesContext with existing values, potentially defined in the server registration.
378-
let mergedUsesContext = [
379-
...( state[ action.name ]?.usesContext || [] ),
380-
...( action.usesContext || [] ),
381-
];
382-
// Remove duplicates.
383-
mergedUsesContext =
384-
mergedUsesContext.length > 0
385-
? [ ...new Set( mergedUsesContext ) ]
386-
: undefined;
387-
388386
return {
389387
...state,
390388
[ action.name ]: {
@@ -401,8 +399,13 @@ export function blockBindingsSources( state = {}, action ) {
401399
return {
402400
...state,
403401
[ action.name ]: {
402+
/*
403+
* Keep the exisitng properties in case the source has been registered
404+
* in the client before bootstrapping.
405+
*/
406+
...state[ action.name ],
404407
label: action.label,
405-
usesContext: action.usesContext,
408+
usesContext: mergedUsesContext,
406409
},
407410
};
408411
case 'REMOVE_BLOCK_BINDINGS_SOURCE':

0 commit comments

Comments
 (0)