Transforms: Allow multiple transforms per block type #66258
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.
What?
This PR enables the transform menu/command to offer multiple transformations for the same block, either eligible variations (via a new "switcher" scope), or named transforms that suit more niche use cases.
Why?
Transforming directly into a desired block variation can be considered a QoL enhancement, but also because some custom block variations may be too specific to count as the active one compared to other variations. For example, a custom Group block variation may apply alignment/style changes, and be independent of the layout settings. At best you can transform it to the desired layout, then select the variation from the transform menu, only for the original to show as still active.
Some more complex blocks could warrant offering multiple transformation results regardless of the source block, particularly if they split/wrap the source block in other blocks. For example, a custom List Grid block that offers transforming the selection into a grid of either Snippet Item blocks or Profile Item blocks.
How?
The
getBlockTransformItemsselector now returns items based on valid block transformations, not block types with valid transformations, and includes the specific transformation to use. By default, the results will be the same; however, additional items can be listed by one of two means:nameattribute (and optional title/icon overrides), and will be listed alongside the default one.The "default" transformation is, as before, the highest priority valid transformation candidate (named ones are not considered).
When selected, the transformation is applied via the new
getBlockTransformationResults()utility, which abstracts the results half ofswitchToBlockType().In addition,
switchToBlockType()now supports a variation argument; it will apply the variation to the transformation results if it exists.Testing Instructions
Open the editor to a page.
Select a paragraph block and open the block switcher. The block variation "Plain Quote" will appear as an option. Selecting it should wrap it in a Quote block with the Plain style already applied.
Insert a heading above the paragraph, then select it plus any number of blocks after it. Open the block switcher, the "Columns" transform will appear, but also the "Heading + Content Columns" transform as well. Selecting the latter should create a 2 column layout with the heading in the first and the remaining blocks in the second.
Screenshots or screencast
The Plain Quote variation transform in action.
chrome_CdlzQt7GqO.mp4
The Heading + Content Columns alternative transform in action.
chrome_fOOKBXNZYS.mp4