Skip to content

Commit

Permalink
Use args
Browse files Browse the repository at this point in the history
  • Loading branch information
cbravobernal committed Dec 18, 2024
1 parent 9e0250c commit a1112a2
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 33 deletions.
3 changes: 1 addition & 2 deletions lib/compat/wordpress-6.7/block-bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check warning on line 20 in lib/compat/wordpress-6.7/block-bindings.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 8 space(s) between "'args'" and double arrow, but found 3.
);
}
$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?.fields?.( {
const fieldsList = blockBindingsSource?.args?.( {
select,
context: blockBindingsContext,
} );
Expand Down
14 changes: 7 additions & 7 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 `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 ) => {
Expand All @@ -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 ) {
Expand All @@ -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 ) {
Expand Down
12 changes: 3 additions & 9 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,
fields,
args,
} = source;

const existingSource = unlock(
Expand Down Expand Up @@ -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 );
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 `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(

Check failure on line 1707 in packages/blocks/src/api/test/registration.js

View workflow job for this annotation

GitHub Actions / JavaScript (Node.js 22) 4/4

Error: expect(jest.fn()).toHaveWarnedWith(expected) Expected mock function to be called with: ["Block bindings source args must be a function."] but it was called with: ["Block bindings source args must be an object."] console.warn() should not be used unless explicitly expected See https://www.npmjs.com/package/@wordpress/jest-console for details. at Object.toHaveWarnedWith (/home/runner/work/gutenberg/gutenberg/packages/blocks/src/api/test/registration.js:1707:22) at Promise.then.completed (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/utils.js:300:28) at new Promise (<anonymous>) at callAsyncCircusFn (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/utils.js:233:10) at _callCircusTest (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:315:40) at processTicksAndRejections (node:internal/process/task_queues:105:5) at _runTest (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:251:3) at _runTestsForDescribeBlock (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:125:9) at _runTestsForDescribeBlock (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:120:9) at _runTestsForDescribeBlock (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:120:9) at run (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:70:3) at runAndTransformResultsToJestFormat (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21) at jestAdapter (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19) at runTestInternal (/home/runner/work/gutenberg/gutenberg/node_modules/jest-runner/build/runTest.js:367:16) at runTest (/home/runner/work/gutenberg/gutenberg/node_modules/jest-runner/build/runTest.js:444:34) at Object.worker (/home/runner/work/gutenberg/gutenberg/node_modules/jest-runner/build/testWorker.js:106:12)

Check failure on line 1707 in packages/blocks/src/api/test/registration.js

View workflow job for this annotation

GitHub Actions / JavaScript (Node.js 20) 4/4

Error: expect(jest.fn()).toHaveWarnedWith(expected) Expected mock function to be called with: ["Block bindings source args must be a function."] but it was called with: ["Block bindings source args must be an object."] console.warn() should not be used unless explicitly expected See https://www.npmjs.com/package/@wordpress/jest-console for details. at Object.toHaveWarnedWith (/home/runner/work/gutenberg/gutenberg/packages/blocks/src/api/test/registration.js:1707:22) at Promise.then.completed (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/utils.js:300:28) at new Promise (<anonymous>) at callAsyncCircusFn (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/utils.js:233:10) at _callCircusTest (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:315:40) at processTicksAndRejections (node:internal/process/task_queues:95:5) at _runTest (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:251:3) at _runTestsForDescribeBlock (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:125:9) at _runTestsForDescribeBlock (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:120:9) at _runTestsForDescribeBlock (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:120:9) at run (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:70:3) at runAndTransformResultsToJestFormat (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21) at jestAdapter (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19) at runTestInternal (/home/runner/work/gutenberg/gutenberg/node_modules/jest-runner/build/runTest.js:367:16) at runTest (/home/runner/work/gutenberg/gutenberg/node_modules/jest-runner/build/runTest.js:444:34) at Object.worker (/home/runner/work/gutenberg/gutenberg/node_modules/jest-runner/build/testWorker.js:106:12)
'Block bindings source fields 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,
fields: () => {
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.fields ).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,
fields: source.fields,
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 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,
Expand All @@ -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':
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;
},
fields( { select, context } ) {
return getPostMetaFields( select, context );
},
args: ( { select, context } ) => getPostMetaFields( select, context ),
};

0 comments on commit a1112a2

Please sign in to comment.