From 9ecb8affb2f111eda07748b722acc9847e88df01 Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Wed, 16 Oct 2024 10:04:30 +0200 Subject: [PATCH 1/5] Change registration to override label --- packages/blocks/src/api/registration.js | 12 ++++-------- packages/blocks/src/store/reducer.js | 3 +-- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/packages/blocks/src/api/registration.js b/packages/blocks/src/api/registration.js index ae12b0cfbcf49d..941653db48a9c8 100644 --- a/packages/blocks/src/api/registration.js +++ b/packages/blocks/src/api/registration.js @@ -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.' ); @@ -873,6 +865,10 @@ export const registerBlockBindingsSource = ( source ) => { return; } + if ( 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.' ); diff --git a/packages/blocks/src/store/reducer.js b/packages/blocks/src/store/reducer.js index 16594a79271e6c..7f53496d39cbc4 100644 --- a/packages/blocks/src/store/reducer.js +++ b/packages/blocks/src/store/reducer.js @@ -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, usesContext: getMergedUsesContext( state[ action.name ]?.usesContext, action.usesContext From 01489389aa03d077cabdbb56dc67aa7750d46e85 Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Wed, 16 Oct 2024 10:04:40 +0200 Subject: [PATCH 2/5] Change unit test --- packages/blocks/src/api/test/registration.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/blocks/src/api/test/registration.js b/packages/blocks/src/api/test/registration.js index 99ca6390d2d3b1..917a4b7a150ece 100644 --- a/packages/blocks/src/api/test/registration.js +++ b/packages/blocks/src/api/test/registration.js @@ -1512,20 +1512,23 @@ 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' ); } ); // Check the `usesContext` array is correct. From b19618b9f01d431f3669ab3e98770b1c94a9cefe Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Wed, 16 Oct 2024 10:27:18 +0200 Subject: [PATCH 3/5] Compare label values --- packages/blocks/src/api/registration.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/blocks/src/api/registration.js b/packages/blocks/src/api/registration.js index 941653db48a9c8..2f4bab2b5f2589 100644 --- a/packages/blocks/src/api/registration.js +++ b/packages/blocks/src/api/registration.js @@ -865,7 +865,7 @@ export const registerBlockBindingsSource = ( source ) => { return; } - if ( label && existingSource?.label ) { + if ( label && existingSource?.label && label !== existingSource?.label ) { warning( 'Block bindings "' + name + '" source label was overriden.' ); } From 5f91194bb675059d6c25e9963f948e7261bd3306 Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Wed, 16 Oct 2024 13:17:34 +0200 Subject: [PATCH 4/5] Keep server value if client is not defined --- packages/blocks/src/store/reducer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/blocks/src/store/reducer.js b/packages/blocks/src/store/reducer.js index 7f53496d39cbc4..ac652b91890319 100644 --- a/packages/blocks/src/store/reducer.js +++ b/packages/blocks/src/store/reducer.js @@ -403,7 +403,7 @@ export function blockBindingsSources( state = {}, action ) { return { ...state, [ action.name ]: { - label: action.label, + label: action.label || state[ action.name ]?.label, usesContext: getMergedUsesContext( state[ action.name ]?.usesContext, action.usesContext From 0010f6355a37b647f2246ddbd88bba886a84a9cb Mon Sep 17 00:00:00 2001 From: Mario Santos Date: Wed, 16 Oct 2024 13:21:47 +0200 Subject: [PATCH 5/5] Add unit test to ensure server label persists --- packages/blocks/src/api/test/registration.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/blocks/src/api/test/registration.js b/packages/blocks/src/api/test/registration.js index 917a4b7a150ece..73f310bbf04dc7 100644 --- a/packages/blocks/src/api/test/registration.js +++ b/packages/blocks/src/api/test/registration.js @@ -1531,6 +1531,21 @@ describe( 'blocks', () => { 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. it( 'should reject invalid usesContext property', () => { registerBlockBindingsSource( {