diff --git a/packages/block-editor/src/hooks/block-fields/index.js b/packages/block-editor/src/hooks/block-fields/index.js index 72fac94ee1ac27..01acb9db3031b6 100644 --- a/packages/block-editor/src/hooks/block-fields/index.js +++ b/packages/block-editor/src/hooks/block-fields/index.js @@ -4,6 +4,7 @@ import { privateApis as blocksPrivateApis, getBlockType, + store as blocksStore, } from '@wordpress/blocks'; import { __experimentalHStack as HStack, @@ -19,6 +20,7 @@ import { __ } from '@wordpress/i18n'; */ import { store as blockEditorStore } from '../../store'; import { unlock } from '../../lock-unlock'; +import BlockContext from '../../components/block-context'; import BlockIcon from '../../components/block-icon'; import useBlockDisplayTitle from '../../components/block-title/use-block-display-title'; import useBlockDisplayInformation from '../../components/use-block-display-information'; @@ -72,9 +74,34 @@ function BlockFields( { const blockTypeFields = blockType?.[ fieldsKey ]; + const blockContext = useContext( BlockContext ); + const attributes = useSelect( - ( select ) => select( blockEditorStore ).getBlockAttributes( clientId ), - [ clientId ] + ( select ) => { + const _attributes = + select( blockEditorStore ).getBlockAttributes( clientId ); + if ( ! _attributes?.metadata?.bindings ) { + return _attributes; + } + + const { getBlockBindingsSource } = unlock( select( blocksStore ) ); + return Object.entries( _attributes.metadata.bindings ).reduce( + ( acc, [ attribute, binding ] ) => { + const source = getBlockBindingsSource( binding.source ); + if ( ! source ) { + return acc; + } + const values = source.getValues( { + select, + context: blockContext, + bindings: { [ attribute ]: binding }, + } ); + return { ...acc, ...values }; + }, + _attributes + ); + }, + [ blockContext, clientId ] ); const computedForm = useMemo( () => { diff --git a/packages/e2e-tests/plugins/block-bindings.php b/packages/e2e-tests/plugins/block-bindings.php index 944bfc332c447e..92331d2b654d27 100644 --- a/packages/e2e-tests/plugins/block-bindings.php +++ b/packages/e2e-tests/plugins/block-bindings.php @@ -46,11 +46,6 @@ function gutenberg_test_block_bindings_registration() { plugins_url( 'block-bindings/index.js', __FILE__ ), array( 'wp-blocks', - 'wp-block-editor', - 'wp-components', - 'wp-compose', - 'wp-element', - 'wp-hooks', ), filemtime( plugin_dir_path( __FILE__ ) . 'block-bindings/index.js' ), true diff --git a/packages/e2e-tests/plugins/block-bindings/index.js b/packages/e2e-tests/plugins/block-bindings/index.js index c0bd9d67d6f901..d87198c04fe8da 100644 --- a/packages/e2e-tests/plugins/block-bindings/index.js +++ b/packages/e2e-tests/plugins/block-bindings/index.js @@ -1,9 +1,4 @@ const { registerBlockBindingsSource } = wp.blocks; -const { InspectorControls } = wp.blockEditor; -const { PanelBody, TextControl } = wp.components; -const { createHigherOrderComponent } = wp.compose; -const { createElement: el, Fragment } = wp.element; -const { addFilter } = wp.hooks; const { fieldsList } = window.testingBindings || {}; const getValues = ( { bindings } ) => { @@ -64,42 +59,3 @@ registerBlockBindingsSource( { getValues, canUserEditValue: () => true, } ); - -const withBlockBindingsInspectorControl = createHigherOrderComponent( - ( BlockEdit ) => { - return ( props ) => { - if ( ! props.attributes?.metadata?.bindings?.content ) { - return el( BlockEdit, props ); - } - - return el( - Fragment, - {}, - el( BlockEdit, props ), - el( - InspectorControls, - {}, - el( - PanelBody, - { title: 'Bindings' }, - el( TextControl, { - __next40pxDefaultSize: true, - label: 'Content', - value: props.attributes.content, - onChange: ( content ) => - props.setAttributes( { - content, - } ), - } ) - ) - ) - ); - }; - } -); - -addFilter( - 'editor.BlockEdit', - 'testing/bindings-inspector-control', - withBlockBindingsInspectorControl -); diff --git a/test/e2e/specs/editor/various/block-bindings/post-meta.spec.js b/test/e2e/specs/editor/various/block-bindings/post-meta.spec.js index 5506f9b2ded7dc..9a8f591e0cc349 100644 --- a/test/e2e/specs/editor/various/block-bindings/post-meta.spec.js +++ b/test/e2e/specs/editor/various/block-bindings/post-meta.spec.js @@ -367,6 +367,12 @@ test.describe( 'Post Meta source', () => { } ); test.describe( 'Movie CPT post', () => { + test.beforeAll( async ( { requestUtils } ) => { + await requestUtils.setGutenbergExperiments( [ + 'gutenberg-content-only-inspector-fields', + ] ); + } ); + test.beforeEach( async ( { admin } ) => { // CHECK HOW TO CREATE A MOVIE. await admin.createNewPost( { @@ -375,6 +381,11 @@ test.describe( 'Post Meta source', () => { } ); } ); + test.afterAll( async ( { requestUtils } ) => { + // Ensure experiments are disabled after test. + await requestUtils.setGutenbergExperiments( [] ); + } ); + test( 'should show the custom field value of that specific post', async ( { editor, } ) => { @@ -536,7 +547,7 @@ test.describe( 'Post Meta source', () => { ).toHaveText( 'new value' ); } ); - test( 'should be possible to edit the value of the connected custom fields in the inspector control registered by the plugin', async ( { + test( 'should be possible to edit the value of the connected custom fields in the inspector control registered by Block Fields experiment', async ( { editor, page, } ) => { @@ -558,9 +569,9 @@ test.describe( 'Post Meta source', () => { }, } ); const contentInput = page.getByRole( 'textbox', { - name: 'Content', + label: 'Content', } ); - await expect( contentInput ).toHaveValue( + await expect( contentInput ).toHaveText( 'Movie field default value' ); await contentInput.fill( 'new value' ); @@ -583,6 +594,7 @@ test.describe( 'Post Meta source', () => { await editor.insertBlock( { name: 'core/paragraph', } ); + await page.getByRole( 'tab', { name: 'Settings' } ).click(); await page.getByLabel( 'Attributes options' ).click(); await page .getByRole( 'menuitemcheckbox', { @@ -611,6 +623,7 @@ test.describe( 'Post Meta source', () => { await editor.insertBlock( { name: 'core/paragraph', } ); + await page.getByRole( 'tab', { name: 'Settings' } ).click(); await page.getByLabel( 'Attributes options' ).click(); await page .getByRole( 'menuitemcheckbox', {