Skip to content
Closed
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
1 change: 1 addition & 0 deletions lib/compat/wordpress-6.7/block-bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function gutenberg_bootstrap_server_block_bindings_sources() {
'name' => $source->name,
'label' => $source->label,
'usesContext' => $source->uses_context,
'args' => $source->args,
);
}
$script = sprintf( 'for ( const source of %s ) { ! wp.blocks.getBlockBindingsSource( source.name ) && wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) );
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export function RichTextWrapper(

const { getBlockAttributes } = select( blockEditorStore );
const blockAttributes = getBlockAttributes( clientId );
const fieldsList = blockBindingsSource?.getFieldsList?.( {
const fieldsList = blockBindingsSource?.args?.( {
select,
context: blockBindingsContext,
} );
Expand Down
21 changes: 13 additions & 8 deletions packages/block-editor/src/hooks/block-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => {
const bindableAttributes = getBindableAttributes( blockName );
const dropdownMenuProps = useToolsPanelDropdownMenuProps();

// `useSelect` is used purposely here to ensure `getFieldsList`
// `useSelect` is used purposely here to ensure `args`
// is updated whenever there are updates in block context.
// `source.getFieldsList` may also call a selector via `select`.
// `source.args` may also call a selector via `select`.
const _fieldsList = {};
const { fieldsList, canUpdateBlockBindings } = useSelect(
( select ) => {
Expand All @@ -220,19 +220,24 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => {
}
const registeredSources = getBlockBindingsSources();
Object.entries( registeredSources ).forEach(
( [ sourceName, { getFieldsList, usesContext } ] ) => {
if ( getFieldsList ) {
( [ sourceName, { args, usesContext } ] ) => {
if ( args ) {
// Populate context.
const context = {};
if ( usesContext?.length ) {
for ( const key of usesContext ) {
context[ key ] = blockContext[ key ];
}
}
const sourceList = getFieldsList( {
select,
context,
} );
let sourceList;
if ( typeof args === 'function' ) {
sourceList = args( {
select,
context,
} );
} else {
sourceList = { ...args };
}
// Only add source if the list is not empty.
if ( Object.keys( sourceList || {} ).length ) {
_fieldsList[ sourceName ] = { ...sourceList };
Expand Down
12 changes: 5 additions & 7 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ export const registerBlockBindingsSource = ( source ) => {
getValues,
setValues,
canUserEditValue,
getFieldsList,
args,
} = source;

const existingSource = unlock(
Expand All @@ -811,6 +811,7 @@ export const registerBlockBindingsSource = ( source ) => {
* Check if the source has been already registered on the client.
* If any property expected to be "client-only" is defined, return a warning.
*/
// TODO: Check if server prop fields has to be included in serverProps.
const serverProps = [ 'label', 'usesContext' ];
for ( const prop in existingSource ) {
if ( ! serverProps.includes( prop ) && existingSource[ prop ] ) {
Expand Down Expand Up @@ -892,12 +893,9 @@ export const registerBlockBindingsSource = ( source ) => {
warning( 'Block bindings source canUserEditValue must be a function.' );
return;
}

// Check the `getFieldsList` property is correct.
if ( getFieldsList && typeof getFieldsList !== 'function' ) {
// eslint-disable-next-line no-console
warning( 'Block bindings source getFieldsList must be a function.' );
return;
// Check the `fields` property is correct.
if ( args && typeof args !== 'object' ) {
warning( 'Block bindings source args must be an object.' );
}

return unlock( dispatch( blocksStore ) ).addBlockBindingsSource( source );
Expand Down
12 changes: 6 additions & 6 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -1697,15 +1697,15 @@ describe( 'blocks', () => {
expect( getBlockBindingsSource( 'core/testing' ) ).toBeUndefined();
} );

// Check the `getFieldsList` callback is correct.
it( 'should reject invalid getFieldsList callback', () => {
// Check the `args` callback is correct.
it( 'should reject invalid args callback', () => {
registerBlockBindingsSource( {
name: 'core/testing',
label: 'testing',
getFieldsList: 'should be a function',
args: 'should be a function',
} );
expect( console ).toHaveWarnedWith(
'Block bindings source getFieldsList must be a function.'
'Block bindings source args must be a function.'
);
expect( getBlockBindingsSource( 'core/testing' ) ).toBeUndefined();
} );
Expand All @@ -1718,7 +1718,7 @@ describe( 'blocks', () => {
getValues: () => 'value',
setValues: () => 'new values',
canUserEditValue: () => true,
getFieldsList: () => {
args: () => {
return { field: 'value' };
},
};
Expand All @@ -1742,7 +1742,7 @@ describe( 'blocks', () => {
expect( source.getValues ).toBeUndefined();
expect( source.setValues ).toBeUndefined();
expect( source.canUserEditValue ).toBeUndefined();
expect( source.getFieldsList ).toBeUndefined();
expect( source.args ).toBeUndefined();
unregisterBlockBindingsSource( 'core/valid-source' );
} );

Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function addBlockBindingsSource( source ) {
getValues: source.getValues,
setValues: source.setValues,
canUserEditValue: source.canUserEditValue,
getFieldsList: source.getFieldsList,
args: source.args,
};
}

Expand Down
8 changes: 4 additions & 4 deletions packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 getFieldsList;
let args;
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
getFieldsList = action.getFieldsList;
args = action.args;
} else if ( action.name === 'core/post-meta' ) {
getFieldsList = action.getFieldsList;
args = action.args;
}
return {
...state,
Expand All @@ -413,7 +413,7 @@ export function blockBindingsSources( state = {}, action ) {
// Only set `canUserEditValue` if `setValues` is also defined.
canUserEditValue:
action.setValues && action.canUserEditValue,
getFieldsList,
args,
},
};
case 'REMOVE_BLOCK_BINDINGS_SOURCE':
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-tests/plugins/block-bindings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ registerBlockBindingsSource( {
getValues,
setValues,
canUserEditValue: () => true,
getFieldsList: () => fieldsList,
fields: () => fieldsList,
} );

registerBlockBindingsSource( {
Expand Down
4 changes: 1 addition & 3 deletions packages/editor/src/bindings/post-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,5 @@ export default {

return true;
},
getFieldsList( { select, context } ) {
return getPostMetaFields( select, context );
},
args: ( { select, context } ) => getPostMetaFields( select, context ),
};
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ test.describe( 'Registered sources', () => {
} );
} );

test.describe( 'getFieldsList', () => {
test.describe( 'fields', () => {
test( 'should be possible to update attribute value through bindings UI', async ( {
editor,
page,
Expand Down Expand Up @@ -1102,7 +1102,7 @@ test.describe( 'Registered sources', () => {
'Add Empty Field Label'
);
} );
test( 'should show source label when value is empty, cannot edit, and `getFieldsList` is undefined', async ( {
test( 'should show source label when value is empty, cannot edit, and `fields` is undefined', async ( {
editor,
} ) => {
await editor.insertBlock( {
Expand Down