-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Block Fields: Remove normalization code and tidy up #74532
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
Conversation
| if ( fieldDef?.mapping ) { | ||
| Object.keys( fieldDef.mapping ).forEach( | ||
| ( key ) => { | ||
| if ( key === 'href' || key === 'url' ) { | ||
| updateValue[ key ] = | ||
| updatedAttrs.url; | ||
| } else if ( key === 'rel' ) { | ||
| updateValue[ key ] = | ||
| updatedAttrs.rel; | ||
| } else if ( | ||
| key === 'target' || | ||
| key === 'linkTarget' | ||
| ) { | ||
| updateValue[ key ] = | ||
| updatedAttrs.linkTarget; | ||
| } | ||
| } | ||
| ); | ||
| } |
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.
This code isn't required as the setValues function already does this by iterating over mapping.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
| if ( | ||
| key === 'href' || | ||
| key === 'url' || | ||
| key === 'rel' || | ||
| key === 'target' || | ||
| key === 'linkTarget' | ||
| ) { | ||
| removeValue[ key ] = undefined; | ||
| } |
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.
The if statement isn't needed as mapping should already have the correct key names.
| } ); | ||
| onChange( mappedChanges ); | ||
| }; | ||
| const { allowedTypes = [], multiple = false } = fieldDef.args || {}; |
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.
Fixes the allowedTypes by reading them from args instead of config.
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.
Does media-thumbnail also need to read from fieldDef.args like we're doing here?
allowedTypes is empty otherwise.
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.
Good catch, the wrong icon was being shown for videos and other types. Pushed a fix for that 👍
| if ( | ||
| key === 'id' || | ||
| key === 'src' || | ||
| key === 'url' | ||
| ) { | ||
| resetValue[ key ] = undefined; | ||
| } else if ( key === 'caption' || key === 'alt' ) { | ||
| resetValue[ key ] = ''; | ||
| } |
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.
Removing this doesn't seem to cause any issues in my testing. Instead every key is now set to undefined on a reset.
|
Size Change: -606 B (-0.02%) Total Size: 3.05 MB
ℹ️ View Unchanged
|
|
|
||
| // Turn off featured image when resetting (only if it's in the mapping) | ||
| if ( hasFeaturedImageSupport ) { | ||
| resetValue.featuredImage = false; |
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.
featuredImage also gets set back to undefined. Blocks already implement a default of false, so no need to set it to false.
| const newValue = { | ||
| ...selectedMedia, | ||
| mediaType: selectedMedia.media_type, | ||
| }; |
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.
I've deleted a lot of the code that was previously here. Now uses keys and values in selectedMedia rather than trying to change them. The field.setValue implementation will map back to the keys that the block expects.
That is, apart from media_type - field ids are usually camel cased, so I've added a mediaType property here to support that.
34d4364 to
9b442f5
Compare
| // If the field defines a `mapping`, then custom `getValue` and `setValue` | ||
| // implementations are provided. | ||
| // These functions map from the inconsistent attribute keys found on blocks | ||
| // to consistent keys that the field can use internally (and back again). | ||
| // When `mapping` isn't provided, we can use the field API's default | ||
| // implementation of these functions. | ||
| if ( fieldDef.mapping ) { |
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.
Only add getValue / setValue when needed now, which is when there's a mapping.
Also added a comment to explain this.
| if ( ! blockTypeFields?.length ) { | ||
| // TODO - we might still want to show a placeholder for blocks with no fields. | ||
| // for example, a way to select the block. | ||
| return null; | ||
| } |
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.
Moved this early return a little bit earlier.
ramonjd
left a comment
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.
Nice clean up @talldan
allowedTypes and multiple were not being handled correctly by media fields (it always defaulted to image).
Cover block backgroundType was not being correctly set.
I checked both these values and they appear to be working as described.
Only left a couple of comments - I think the MediaThumbnail needs to reference field.args, and I wasn't sure about the varying object props passed tosetValue and getValue
Tentative ✅ if you need to change that small thing before merged. It's behind the experiment as well.
🚀
| } ); | ||
| onChange( mappedChanges ); | ||
| }; | ||
| const { allowedTypes = [], multiple = false } = fieldDef.args || {}; |
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.
Does media-thumbnail also need to read from fieldDef.args like we're doing here?
allowedTypes is empty otherwise.
| updateAttributes( updateValue ); | ||
| onChange( | ||
| field.setValue( { | ||
| item: data, |
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.
Looking at field.setValue = ( { value } ) => { in packages/block-editor/src/hooks/block-fields/index.js above... does item need to passed?
It's done in several places, so I assume so and (?) but just checking since the implementation appears different to getValue.
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.
I kept it as the type requires it:
| setValue?: ( args: { item: Item; value: any } ) => DeepPartial< Item >; |
Though it's not TS code, it still feels like good practice.
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.
This feels like a really significant cleanup, nice work! It's all smoke testing well for me (e.g. media in images, cover block, media & text, link fields, etc), and as Ramon mentioned, it's all behind an experiment so LGTM 🚀
What?
This is a fairly substantial code quality PR for Block Fields, based on some discoveries made while working on a more experimental PR - #74435.
Also fixes a couple of small issues, so I've labelled it a bug fix.
Let me know if you'd like to break it up and I can try, but some of the changes are based on some of the other changes, so I found it easier to roll it into one PR.
Why?
The goal of this PR is to move a few steps closer to the regular DataForm API and remove any special code.
How?
Removals:
normalizeMediaValue,denormalizeMediaValue,normalizeLinkValue,denormalizeLinkValue- I found these function are largely doubling up work that was already being done (infield.setValueandfield.getValue), so they're removed.hideLabelFromVision- As discovered in Block Fields: show all form fields by default #74486, this property doesn't do anything, so it's now removed.field.config- I found that this property also doesn't do anything, the config is passed through viacreateConfiguredControl.config.defaultValues- I've removed all handling ofdefaultValues. I've realized that it's much easier to unset attribute values usingundefinedinstead, the block itself will then pass its default. 💡Small changes:
field.getValue/field.setValue- these functions can be simplified by only setting them on thefieldobject whenmappingexists. Whenmappingdoesn't exist we can use the default implementation that the fields api provides.createConfiguredControl- I've changed the function signature slightly, I found it makes it a little easier to read.mappingwithin the controls as possible.mappingis a special thing that was invented for Block Fields. The only place it's needed currently is for resetting links or media, since we need to know which properties to set back toundefined.onChange( field.setValue( // ... ) )inline. This matches the style used by fields in the dataforms package, so the idea is to make it a closer implementation.Fixes:
allowedTypesandmultiplewere not being handled correctly bymediafields (it always defaulted to image).backgroundTypewas not being correctly set.Testing Instructions