-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add Columns transform from Media & Text #29415
Conversation
{ | ||
type: 'block', | ||
blocks: [ 'core/media-text' ], | ||
isMatch: ( { mediaId } ) => !! mediaId, |
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 was intended to not offer the transform when no media has been selected. I couldn't get that to work (even just returning false
here) and have left it here to inquire about it. Any ideas @ntsekouras? We could remove it. As of now, I made the transform default to inserting an empty Image block when transforming before any media has been selected.
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.
That's a tricky one :). The problem here is that isMatch
is checked in a different flow than the actual transformation. It is checked in getPossibleBlockTransformations
that is used to return the available block types to show in Block Switcher
list.
After that check that has found Columns
to be an eligible match (with 2 transformations though :) ) the actual transformation selection happens in a different place here:
gutenberg/packages/blocks/src/api/factory.js
Line 441 in 5cd4f3f
const transformation = |
which selects the first match which happens to be the first declared one. You can check that if you put your new transform below the existing one, it will always choose the old one.
The wildcard *
complicates things on how to combine other transforms. Recently I looked into a related problem here: #29296, with no clear path forwards for now.
--edit
After digging a bit more it seems that the prioritizations of transforms have been implemented a long ago here: #5701
So by adding a priority: 1
prop in your transform
will return the wanted transformation.
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.
You can check that if you put your new transform below the existing one, it will always choose the old one.
True, I had noticed that as I started this with the new transform below the existing. I've pushed another commit to put it back below and use priority
. I still couldn't get isMatch
to prevent the transform but it's unnecessary for this PR as I think it is always preferable to use this transform even if no media has been selected.
Thanks for looking into this!
Size Change: +3.08 kB (0%) Total Size: 1.39 MB
ℹ️ View Unchanged
|
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.
Thanks for working on this @stokesman!
It seems that with priority
prop your problem will be solved. I think it's okay to transform in 2 columns even if no Image is set.
Indeed, it still seems better than the generic transform. The small rub was not knowing the type of media block to insert. We could not insert a media block for those conditions but 🤷♂️ . |
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.
Thanks for your work here @stokesman! 💯
I pushed a small addition to pass the text and link colors as well, as you did with backgroundColor. Green CI and let's 🚢
Good catch @ntsekouras, thanks! |
This PR adds the ability to transform a Media & Text block into a Columns block with two Columns and all the relevant fixings from the original block.
Motivation
Since #26185 it has been possible to “transform” any single block into a Columns block. It's a generic operation that just wraps the block inside a single column Columns block. It seems unlikely to be the expected behavior when applied to a Media & Text block.
How has this been tested?
Manually transforming various Media & Text blocks and verifying any applicable attributes are transferred as expected.
Screenshots
columns-from-media-text.mp4
Types of changes
New feature
Checklist: