-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
|
@@ -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 ] = [], | ||
} = 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sourceValue
>metaValue
. I'd suggestsourcePropValue
as well.