Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Bindings: Explore improving block bindings UI useSelect logic #67885

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
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
70 changes: 38 additions & 32 deletions packages/block-editor/src/hooks/block-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getBlockBindingsSource,
getBlockBindingsSources,
getBlockType,
store as blocksStore,
} from '@wordpress/blocks';
import {
__experimentalItemGroup as ItemGroup,
Expand All @@ -17,7 +18,7 @@ import {
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useContext, Fragment } from '@wordpress/element';
import { Fragment } from '@wordpress/element';
import { useViewportMatch } from '@wordpress/compose';

/**
Expand All @@ -30,14 +31,11 @@ import {
} from '../utils/block-bindings';
import { unlock } from '../lock-unlock';
import InspectorControls from '../components/inspector-controls';
import BlockContext from '../components/block-context';
import { useBlockEditContext } from '../components/block-edit';
import { store as blockEditorStore } from '../store';

const { Menu } = unlock( componentsPrivateApis );

const EMPTY_OBJECT = {};

const useToolsPanelDropdownMenuProps = () => {
const isMobile = useViewportMatch( 'medium', '<' );
return ! isMobile
Expand Down Expand Up @@ -203,54 +201,62 @@ function EditableBlockBindingsPanelItems( {
);
}

export const BlockBindingsPanel = ( { name: blockName, metadata } ) => {
const blockContext = useContext( BlockContext );
export const BlockBindingsPanel = ( {
name: blockName,
metadata,
context,
} ) => {
const { removeAllBlockBindings } = useBlockBindingsUtils();
const bindableAttributes = getBindableAttributes( blockName );
const dropdownMenuProps = useToolsPanelDropdownMenuProps();

// `useSelect` is used purposely here to ensure `getFieldsList`
// is updated whenever there are updates in block context.
// `source.getFieldsList` may also call a selector via `select`.
const _fieldsList = {};
const { fieldsList, canUpdateBlockBindings } = useSelect(
const { canUpdateBlockBindings, registeredSources } = useSelect(
( select ) => {
return {
canUpdateBlockBindings:
select( blockEditorStore ).getSettings()
.canUpdateBlockBindings,
registeredSources: unlock(
select( blocksStore )
).getAllBlockBindingsSources(),
};
},
[]
);

/**
* Create new selector for fieldsList to avoid unnecessary re-renders.
* See: https://github.com/WordPress/gutenberg/pull/64072#discussion_r1764693730
*
* `useSelect` is used purposely here to ensure `getFieldsList` is updated
* whenever there are updates in block context.
* `source.getFieldsList` may also call a selector via `registry.select`.
*/
const fieldsList = useSelect(
( select ) => {
if ( ! bindableAttributes || bindableAttributes.length === 0 ) {
return EMPTY_OBJECT;
return;
}
const registeredSources = getBlockBindingsSources();
const _fieldsList = {};
Object.entries( registeredSources ).forEach(
( [ sourceName, { getFieldsList, usesContext } ] ) => {
( [ sourceName, { getFieldsList } ] ) => {
if ( getFieldsList ) {
// Populate context.
const context = {};
if ( usesContext?.length ) {
for ( const key of usesContext ) {
context[ key ] = blockContext[ key ];
}
}
const sourceList = getFieldsList( {
select,
context,
} );
// Only add source if the list is not empty.
if ( Object.keys( sourceList || {} ).length ) {
_fieldsList[ sourceName ] = { ...sourceList };
if ( Object.keys( sourceList ).length ) {
_fieldsList[ sourceName ] = sourceList;
}
}
}
);
return {
fieldsList:
Object.values( _fieldsList ).length > 0
? _fieldsList
: EMPTY_OBJECT,
canUpdateBlockBindings:
select( blockEditorStore ).getSettings()
.canUpdateBlockBindings,
};
return _fieldsList;
},
[ blockContext, bindableAttributes ]
[ bindableAttributes, context, registeredSources ]
);
// Return early if there are no bindable attributes.
if ( ! bindableAttributes || bindableAttributes.length === 0 ) {
Expand All @@ -270,7 +276,7 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => {

// Lock the UI when the user can't update bindings or there are no fields to connect to.
const readOnly =
! canUpdateBlockBindings || ! Object.keys( fieldsList ).length;
! canUpdateBlockBindings || ! Object.keys( fieldsList || {} ).length;

if ( readOnly && Object.keys( filteredBindings ).length === 0 ) {
return null;
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ export function createBlockEditFilter( features ) {
// We can use the index because the array length
// is fixed per page load right now.
key={ i }
context={ props.context }
name={ props.name }
isSelected={ props.isSelected }
clientId={ props.clientId }
Expand Down
81 changes: 48 additions & 33 deletions packages/editor/src/bindings/post-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { store as coreDataStore } from '@wordpress/core-data';
import { createSelector } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -34,43 +35,57 @@ import { unlock } from '../lock-unlock';
* }
* ```
*/
function getPostMetaFields( select, context ) {
const { getEditedEntityRecord } = select( coreDataStore );
const { getRegisteredPostMeta } = unlock( select( coreDataStore ) );
const getPostMetaFields = createSelector(
( select, context ) => {
const { getEditedEntityRecord } = select( coreDataStore );
const { getRegisteredPostMeta } = unlock( select( coreDataStore ) );

let entityMetaValues;
// Try to get the current entity meta values.
if ( context?.postType && context?.postId ) {
entityMetaValues = getEditedEntityRecord(
'postType',
context?.postType,
context?.postId
).meta;
}

const registeredFields = getRegisteredPostMeta( context?.postType );
const metaFields = {};
Object.entries( registeredFields || {} ).forEach( ( [ key, props ] ) => {
// Don't include footnotes or private fields.
if ( key !== 'footnotes' && key.charAt( 0 ) !== '_' ) {
metaFields[ key ] = {
label: props.title || key,
value:
// When using the entity value, an empty string IS a valid value.
entityMetaValues?.[ key ] ??
// When using the default, an empty string IS NOT a valid value.
( props.default || undefined ),
type: props.type,
};
let entityMetaValues;
// Try to get the current entity meta values.
if ( context?.postType && context?.postId ) {
entityMetaValues = getEditedEntityRecord(
'postType',
context?.postType,
context?.postId
).meta;
}
} );

if ( ! Object.keys( metaFields || {} ).length ) {
return null;
}
const registeredFields = getRegisteredPostMeta( context?.postType );
const metaFields = {};
Object.entries( registeredFields || {} ).forEach(
( [ key, props ] ) => {
// Don't include footnotes or private fields.
if ( key !== 'footnotes' && key.charAt( 0 ) !== '_' ) {
metaFields[ key ] = {
label: props.title || key,
value:
// When using the entity value, an empty string IS a valid value.
entityMetaValues?.[ key ] ??
// When using the default, an empty string IS NOT a valid value.
( props.default || undefined ),
type: props.type,
};
}
}
);

return metaFields;
}
if ( ! Object.keys( metaFields || {} ).length ) {
return null;
}

return metaFields;
},
( select, context ) => [
select( coreDataStore ).getEditedEntityRecord(
'postType',
context.postType,
context.postId
).meta,
unlock( select( coreDataStore ) ).getRegisteredPostMeta(
context.postType
),
]
);

export default {
name: 'core/post-meta',
Expand Down
Loading