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: Enable bindings for all attributes with 'role: content' property #66266

Draft
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
const blockEditContext = useBlockEditContext();
const hasBlockBindings = !! blockEditContext[ blockBindingsKey ];
const bindingsStyle =
hasBlockBindings && canBindBlock( name )
hasBlockBindings && canBindBlock()
? {
'--wp-admin-theme-color': 'var(--wp-block-synced-color)',
'--wp-admin-theme-color--rgb':
Expand Down
5 changes: 1 addition & 4 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,7 @@ export function RichTextWrapper(

const { disableBoundBlock, bindingsPlaceholder, bindingsLabel } = useSelect(
( select ) => {
if (
! blockBindings?.[ identifier ] ||
! canBindBlock( blockName )
) {
if ( ! blockBindings?.[ identifier ] || ! canBindBlock() ) {
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/block-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export const BlockBindingsPanel = ( { name: blockName, metadata } ) => {
const filteredBindings = { ...bindings };
Object.keys( filteredBindings ).forEach( ( key ) => {
if (
! canBindAttribute( blockName, key ) ||
! canBindAttribute() ||
filteredBindings[ key ].source === 'core/pattern-overrides'
) {
delete filteredBindings[ key ];
Expand Down
38 changes: 19 additions & 19 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { store as blocksStore } from '@wordpress/blocks';
import { store as blocksStore, getBlockType } from '@wordpress/blocks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useRegistry, useSelect } from '@wordpress/data';
import { useCallback, useMemo, useContext } from '@wordpress/element';
Expand Down Expand Up @@ -70,30 +70,34 @@ function replacePatternOverrideDefaultBindings( blockName, bindings ) {
* Based on the given block name,
* check if it is possible to bind the block.
*
* @param {string} blockName - The block name.
* @return {boolean} Whether it is possible to bind the block to sources.
*/
export function canBindBlock( blockName ) {
return blockName in BLOCK_BINDINGS_ALLOWED_BLOCKS;
export function canBindBlock() {
return true;
}

/**
* Based on the given block name and attribute name,
* check if it is possible to bind the block attribute.
*
* @param {string} blockName - The block name.
* @param {string} attributeName - The attribute name.
* @return {boolean} Whether it is possible to bind the block attribute.
*/
export function canBindAttribute( blockName, attributeName ) {
return (
canBindBlock( blockName ) &&
BLOCK_BINDINGS_ALLOWED_BLOCKS[ blockName ].includes( attributeName )
);
export function canBindAttribute() {
return true;
}

export function getBindableAttributes( blockName ) {
return BLOCK_BINDINGS_ALLOWED_BLOCKS[ blockName ];
const bindableAttributes = [];
const blockType = getBlockType( blockName );
if ( blockType.attributes ) {
for ( const attribute in blockType.attributes ) {
if ( blockType.attributes[ attribute ].role === 'content' ) {
bindableAttributes.push( attribute );
}
}
}

return bindableAttributes;
}

export const withBlockBindingSupport = createHigherOrderComponent(
Expand Down Expand Up @@ -133,10 +137,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
) ) {
const { source: sourceName, args: sourceArgs } = binding;
const source = sources[ sourceName ];
if (
! source ||
! canBindAttribute( name, attributeName )
) {
if ( ! source || ! canBindAttribute() ) {
continue;
}

Expand Down Expand Up @@ -301,11 +302,10 @@ export const withBlockBindingSupport = createHigherOrderComponent(
* to upgrade bound attributes.
*
* @param {WPBlockSettings} settings - Registered block settings.
* @param {string} name - Block name.
* @return {WPBlockSettings} Filtered block settings.
*/
function shimAttributeSource( settings, name ) {
if ( ! canBindBlock( name ) ) {
function shimAttributeSource( settings ) {
if ( ! canBindBlock() ) {
return settings;
}

Expand Down
Loading