Block Transforms: Add metadata transformation#72462
Conversation
|
Size Change: +171 B (+0.01%) Total Size: 2.27 MB
ℹ️ View Unchanged
|
|
Note: I think this PR should be backported to the 6.9 release, as it fixes issues with the hiding blocks feature and Notes feature. |
| addFilter( | ||
| 'blocks.switchToBlockType.transformedBlock', | ||
| 'core/color/addTransforms', | ||
| 'core/customClassName/addTransforms', |
There was a problem hiding this comment.
Nit: I think this was a copy-paste error, so I fixed it.
| if ( | ||
| hasBlockSupport( newBlockType, 'className' ) && | ||
| attributes.className | ||
| ) { | ||
| transformedAttributes.className = attributes.className; | ||
| } |
There was a problem hiding this comment.
We added this in #72196 to preserve custom classnames, but it was redundant because there was already a hook to convert class names.
15ddd7e to
f491a8d
Compare
|
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. |
|
Flaky tests detected in f5995f7. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/18615566825
|
| if ( Object.keys( preservedMetadata ).length > 0 ) { | ||
| return { | ||
| ...result, | ||
| attributes: cleanEmptyObject( { |
There was a problem hiding this comment.
There is an interesting issue here that you can test by transforming a single paragraph with some metadata to a Heading block.
Rich text attributes are created with Object.defineProperty, which defaults to non-enumerable attributes. This causes the content not to be added here.
Now, the simple solution would be to wrap only the metadata in cleanEmptyObject. I have no idea what the implications would be to update Rich text attributes. Curious though if @ellatrix remembers whether there was a specific reason for this.
There was a problem hiding this comment.
Upon further consideration, I realized that the CleanEmptyObject itself is unnecessary here. This is because this return statement is only executed if preservedMetadata is not empty.
Fixed in f7b214e
| */ | ||
| import { addTransforms } from '../metadata'; | ||
|
|
||
| describe( 'metadata', () => { |
There was a problem hiding this comment.
Cool to see some tests here. Thanks!
* Block Transforms: Add metadata transformation * Fix e2e tests * Remove unnecessary cleanEmptyObject Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
|
I just cherry-picked this PR to the wp/6.9 branch to get it included in the next release: 15df30c |
* Block Transforms: Add metadata transformation * Fix e2e tests * Remove unnecessary cleanEmptyObject Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org>
What?
This PR uses the
blocks.switchToBlockType.transformedBlockhook to preserve three metadatanoteID,blockVisibility, andnameduring block transformation.Why?
We took the first step in #72196 to try to prevent as many missing attributes as possible when transforming blocks. This resolved the issue for block transformations that use the getTransformedAttributes function, but other transformations may still result in missing attributes.
How?
I realized that there is a
blocks.witchToBlockType.transformedBlockshook, which runs on every block transformation. This hook is used in many places to preserve block attributes. Using it ensures that metadata is preserved during any block conversion. However, please note that metadata will not be inherited if the following conditions are met:This logic is similar to that for custom class names.
Testing Instructions
Here is an example that tests whether attributes are preserved:
Screenshots or screencast
ba954cb2abda37743fdd71e65f8a289b.mp4
Next Plan
I am considering automatically preserving all attributes except for the block bindings API via
blocks.switchToBlockType.transformedBlockand eventually removing thegetTransformedAttributesfunction itself.