From a1112a22a16d6599ada7ec84a5e397903d70bca3 Mon Sep 17 00:00:00 2001 From: Carlos Bravo <37012961+cbravobernal@users.noreply.github.com> Date: Wed, 18 Dec 2024 22:01:04 +0100 Subject: [PATCH] Use args --- lib/compat/wordpress-6.7/block-bindings.php | 3 +-- .../block-editor/src/components/rich-text/index.js | 2 +- packages/block-editor/src/hooks/block-bindings.js | 14 +++++++------- packages/blocks/src/api/registration.js | 12 +++--------- packages/blocks/src/api/test/registration.js | 12 ++++++------ packages/blocks/src/store/private-actions.js | 2 +- packages/blocks/src/store/reducer.js | 8 ++++---- packages/editor/src/bindings/post-meta.js | 4 +--- 8 files changed, 24 insertions(+), 33 deletions(-) diff --git a/lib/compat/wordpress-6.7/block-bindings.php b/lib/compat/wordpress-6.7/block-bindings.php index e364d19a499ac8..4bb7040f188a76 100644 --- a/lib/compat/wordpress-6.7/block-bindings.php +++ b/lib/compat/wordpress-6.7/block-bindings.php @@ -17,8 +17,7 @@ function gutenberg_bootstrap_server_block_bindings_sources() { 'name' => $source->name, 'label' => $source->label, 'usesContext' => $source->uses_context, - // Not sure if we need to update this if we already update Core to use `fields`. - 'fields' => $source->fields, + 'args' => $source->args, ); } $script = sprintf( 'for ( const source of %s ) { ! wp.blocks.getBlockBindingsSource( source.name ) && wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) ); diff --git a/packages/block-editor/src/components/rich-text/index.js b/packages/block-editor/src/components/rich-text/index.js index 380cbf2651f345..373ddddc61362c 100644 --- a/packages/block-editor/src/components/rich-text/index.js +++ b/packages/block-editor/src/components/rich-text/index.js @@ -203,7 +203,7 @@ export function RichTextWrapper( const { getBlockAttributes } = select( blockEditorStore ); const blockAttributes = getBlockAttributes( clientId ); - const fieldsList = blockBindingsSource?.fields?.( { + const fieldsList = blockBindingsSource?.args?.( { select, context: blockBindingsContext, } ); diff --git a/packages/block-editor/src/hooks/block-bindings.js b/packages/block-editor/src/hooks/block-bindings.js index eaae7ab5e40d5a..0901f7b5bd1b3a 100644 --- a/packages/block-editor/src/hooks/block-bindings.js +++ b/packages/block-editor/src/hooks/block-bindings.js @@ -209,9 +209,9 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => { const bindableAttributes = getBindableAttributes( blockName ); const dropdownMenuProps = useToolsPanelDropdownMenuProps(); - // `useSelect` is used purposely here to ensure `fields` + // `useSelect` is used purposely here to ensure `args` // is updated whenever there are updates in block context. - // `source.fields` may also call a selector via `select`. + // `source.args` may also call a selector via `select`. const _fieldsList = {}; const { fieldsList, canUpdateBlockBindings } = useSelect( ( select ) => { @@ -220,8 +220,8 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => { } const registeredSources = getBlockBindingsSources(); Object.entries( registeredSources ).forEach( - ( [ sourceName, { fields, usesContext } ] ) => { - if ( fields ) { + ( [ sourceName, { args, usesContext } ] ) => { + if ( args ) { // Populate context. const context = {}; if ( usesContext?.length ) { @@ -230,13 +230,13 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => { } } let sourceList; - if ( typeof fields === 'function' ) { - sourceList = fields( { + if ( typeof args === 'function' ) { + sourceList = args( { select, context, } ); } else { - sourceList = { ...fields }; + sourceList = { ...args }; } // Only add source if the list is not empty. if ( Object.keys( sourceList || {} ).length ) { diff --git a/packages/blocks/src/api/registration.js b/packages/blocks/src/api/registration.js index 5d544c8f471574..7e4e01f31d41e6 100644 --- a/packages/blocks/src/api/registration.js +++ b/packages/blocks/src/api/registration.js @@ -800,7 +800,7 @@ export const registerBlockBindingsSource = ( source ) => { getValues, setValues, canUserEditValue, - fields, + args, } = source; const existingSource = unlock( @@ -894,14 +894,8 @@ export const registerBlockBindingsSource = ( source ) => { return; } // Check the `fields` property is correct. - if ( - fields && - typeof fields !== 'function' && - typeof fields !== 'object' - ) { - warning( - 'Block bindings source fields must be a function or an object.' - ); + if ( args && typeof args !== 'object' ) { + warning( 'Block bindings source args must be an object.' ); } return unlock( dispatch( blocksStore ) ).addBlockBindingsSource( source ); diff --git a/packages/blocks/src/api/test/registration.js b/packages/blocks/src/api/test/registration.js index 6f6d00cca7d7e3..a3ea1badc78196 100644 --- a/packages/blocks/src/api/test/registration.js +++ b/packages/blocks/src/api/test/registration.js @@ -1697,15 +1697,15 @@ describe( 'blocks', () => { expect( getBlockBindingsSource( 'core/testing' ) ).toBeUndefined(); } ); - // Check the `fields` callback is correct. - it( 'should reject invalid fields callback', () => { + // Check the `args` callback is correct. + it( 'should reject invalid args callback', () => { registerBlockBindingsSource( { name: 'core/testing', label: 'testing', - fields: 'should be a function', + args: 'should be a function', } ); expect( console ).toHaveWarnedWith( - 'Block bindings source fields must be a function.' + 'Block bindings source args must be a function.' ); expect( getBlockBindingsSource( 'core/testing' ) ).toBeUndefined(); } ); @@ -1718,7 +1718,7 @@ describe( 'blocks', () => { getValues: () => 'value', setValues: () => 'new values', canUserEditValue: () => true, - fields: () => { + args: () => { return { field: 'value' }; }, }; @@ -1742,7 +1742,7 @@ describe( 'blocks', () => { expect( source.getValues ).toBeUndefined(); expect( source.setValues ).toBeUndefined(); expect( source.canUserEditValue ).toBeUndefined(); - expect( source.fields ).toBeUndefined(); + expect( source.args ).toBeUndefined(); unregisterBlockBindingsSource( 'core/valid-source' ); } ); diff --git a/packages/blocks/src/store/private-actions.js b/packages/blocks/src/store/private-actions.js index 45c6dbee1038c6..0f19154676184a 100644 --- a/packages/blocks/src/store/private-actions.js +++ b/packages/blocks/src/store/private-actions.js @@ -55,7 +55,7 @@ export function addBlockBindingsSource( source ) { getValues: source.getValues, setValues: source.setValues, canUserEditValue: source.canUserEditValue, - fields: source.fields, + args: source.args, }; } diff --git a/packages/blocks/src/store/reducer.js b/packages/blocks/src/store/reducer.js index bbac230dc05dbd..6408f31ffaa316 100644 --- a/packages/blocks/src/store/reducer.js +++ b/packages/blocks/src/store/reducer.js @@ -394,11 +394,11 @@ export function blockBindingsSources( state = {}, action ) { switch ( action.type ) { case 'ADD_BLOCK_BINDINGS_SOURCE': // Only open this API in Gutenberg and for `core/post-meta` for the moment. - let fields; + let args; if ( globalThis.IS_GUTENBERG_PLUGIN ) { - fields = action.fields; + args = action.args; } else if ( action.name === 'core/post-meta' ) { - fields = action.fields; + args = action.args; } return { ...state, @@ -413,7 +413,7 @@ export function blockBindingsSources( state = {}, action ) { // Only set `canUserEditValue` if `setValues` is also defined. canUserEditValue: action.setValues && action.canUserEditValue, - fields, + args, }, }; case 'REMOVE_BLOCK_BINDINGS_SOURCE': diff --git a/packages/editor/src/bindings/post-meta.js b/packages/editor/src/bindings/post-meta.js index c76112698b168d..c078476cb53c5c 100644 --- a/packages/editor/src/bindings/post-meta.js +++ b/packages/editor/src/bindings/post-meta.js @@ -138,7 +138,5 @@ export default { return true; }, - fields( { select, context } ) { - return getPostMetaFields( select, context ); - }, + args: ( { select, context } ) => getPostMetaFields( select, context ), };