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

Experiment: Allow editing of custom fields in block bindings #58723

Closed
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
136 changes: 84 additions & 52 deletions packages/block-editor/src/hooks/use-bindings-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import { getBlockType } from '@wordpress/blocks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useRegistry, useSelect } from '@wordpress/data';
import { useMemo, useCallback } from '@wordpress/element';
import { addFilter } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../store';
import { useBlockEditContext } from '../components/block-edit/context';
import { unlock } from '../lock-unlock';

/** @typedef {import('@wordpress/compose').WPHigherOrderComponent} WPHigherOrderComponent */
Expand All @@ -32,69 +32,101 @@ const BLOCK_BINDINGS_ALLOWED_BLOCKS = {
const createEditFunctionWithBindingsAttribute = () =>
createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const { clientId, name: blockName } = useBlockEditContext();
const { name, attributes, setAttributes, ...otherProps } = props;
const { getBlockBindingsSource } = unlock(
useSelect( blockEditorStore )
);
const { getBlockAttributes, updateBlockAttributes } =
useSelect( blockEditorStore );

const updatedAttributes = getBlockAttributes( clientId );
if ( updatedAttributes?.metadata?.bindings ) {
Object.entries( updatedAttributes.metadata.bindings ).forEach(
( [ attributeName, settings ] ) => {
const source = getBlockBindingsSource(
settings.source
);

if ( source ) {
// Second argument (`updateMetaValue`) will be used to update the value in the future.
const {
placeholder,
useValue: [ metaValue = null ] = [],
} = source.useSource( props, settings.args );

if ( placeholder && ! metaValue ) {
// If the attribute is `src` or `href`, a placeholder can't be used because it is not a valid url.
// Adding this workaround until attributes and metadata fields types are improved and include `url`.
const htmlAttribute =
getBlockType( blockName ).attributes[
attributeName
].attribute;
if (
htmlAttribute === 'src' ||
htmlAttribute === 'href'
) {
updatedAttributes[ attributeName ] = null;
} else {
updatedAttributes[ attributeName ] =
placeholder;
}
}
const blockType = getBlockType( name );

if ( metaValue ) {
updatedAttributes[ attributeName ] = metaValue;
}
}
const boundAttributes = {};
if ( attributes?.metadata?.bindings ) {
Object.entries( attributes?.metadata?.bindings ).forEach(
( [ attribute, binding ] ) => {
boundAttributes[ attribute ] = getBlockBindingsSource(
binding.source
).useSource( props, binding.args );
}
);
}

const attributesWithBindings = useMemo( () => {
return {
...attributes,
...Object.fromEntries(
BLOCK_BINDINGS_ALLOWED_BLOCKS[ name ].map(
( attributeName ) => {
// Check bindings.
if ( boundAttributes[ attributeName ] ) {
const {
placeholder,
useValue: [ sourceValue = null ] = [],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sourceValue > metaValue. I'd suggest sourcePropValue as well.

} = boundAttributes[ attributeName ];

const blockTypeAttribute =
blockType.attributes[ attributeName ];

if ( placeholder && ! sourceValue ) {
// If the attribute is `src` or `href`, a placeholder can't be used because it is not a valid url.
// Adding this workaround until attributes and metadata fields types are improved and include `url`.

const htmlAttribute =
blockTypeAttribute.attribute;
if (
htmlAttribute === 'src' ||
htmlAttribute === 'href'
) {
return [ attributeName, null ];
}
return [ attributeName, placeholder ];
}

if ( sourceValue ) {
// TODO: If it is rich-text, I think we can't edit it this way.
return [ attributeName, sourceValue ];
}
}
return [
attributeName,
attributes[ attributeName ],
];
}
)
),
};
}, [ attributes, blockType.attributes, boundAttributes, name ] );

const updatedSetAttributes = useCallback(
( nextAttributes ) => {
Object.entries( nextAttributes ?? {} )
.filter(
( [ attribute ] ) => attribute in boundAttributes
)
.forEach( ( [ attribute, value ] ) => {
const {
useValue: [ , setSourceValue = null ] = [],
} = boundAttributes[ attribute ];
if ( setSourceValue ) {
setSourceValue( value );
}
} );
setAttributes( nextAttributes );
},
[ setAttributes, attributesWithBindings, boundAttributes ]
);

const registry = useRegistry();

return (
<>
<BlockEdit
key="edit"
attributes={ updatedAttributes }
setAttributes={ ( newAttributes, blockId ) =>
registry.batch( () =>
updateBlockAttributes( blockId, newAttributes )
)
}
{ ...props }
/>
</>
<BlockEdit
attributes={ attributesWithBindings }
setAttributes={ ( newAttributes ) => {
registry.batch( () =>
updatedSetAttributes( newAttributes )
);
} }
Comment on lines +123 to +127
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm right, this scenario will cover the case when the attributes are edited from the BlockEdit function representation. This means that if, for instance, you change the attribute value by dispatching an action, the updatedSetAttribute will never be called.

{ ...otherProps }
/>
);
},
'useBoundAttributes'
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/bindings/post-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ export default {
useValue: [ metaValue, updateMetaValue ],
};
},
lockAttributesEditing: true,
lockAttributesEditing: false,
};
Loading