-
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
Block API: Deprecate block transform
function in favor of convert
#19401
Conversation
This was more valuable in describing innerBlocks as a distinct argument from attributes. Now that the documentation has been updated to reflect that a full block object is passed, there's little value in distinctly describing the innerBlocks availability.
Nested destructuring tends to be difficult to read, and doesn't provide much value in this context
As an implementer, any progress toward introducing consistency in the transform API would be greatly appreciated. I think the |
Regarding Shortcodes, I'd ideally like to see this same type of function signature, with a (Any one-to-one shortcode-to-block transform will fall down, I believe, because there's sometimes hierarchies of content in shortcodes which would be represented differently in blocks. As an example, I just converted a wrapping |
Love the simplicity here |
Looks like this needs to be rebased with |
@mtias or @youknowriad - what’s the plan regarding this API change? |
@gziolo Do you want me to pick this up again? I originally wrote As I see it the two APIs could be complementary with |
@getdave, I would double-check with @mtias, @youknowriad, or @mcsf first as they might have a better overview of why this PR never landed. If you think it's still necessary then it would be fantastic if you could take over it. |
I'll bring this up in Core Editor chat on Wednesday. |
+1 for stabilizing this, though I'd also recommend adding/testing more multi-select block conversions in the core block library to test the API before it's finalized. For example, a selection of an image and a paragraph selection -> media and text transform. I do think the multi-selection wildcard transform needs additional iteration but that can potentially wait for follow up. There's a couple of common cases where a container block may want to be able to group arbitrary blocks, but not nest itself. |
I also want to recommend adding wildcard support for Currently, both APIs can handle this. The only thing that is missing is the Group block already does this by adding a custom menu item in the "Options" dropdown. |
I think we should also pass more than just an array of attributes to the |
I'm not very comfortable with the burden of having both |
As there's been a few requests in Core Editor chat, I'm going to try and move this forward. Firstly, a quick summary of what this is all about. A Quick SummaryWhat we're discussing is the argument signature of the respective "transformation" methods:
gutenberg/packages/blocks/src/api/factory.js Lines 505 to 508 in 17fd83b
gutenberg/packages/blocks/src/api/factory.js Lines 501 to 503 in 17fd83b
The reason This is because
In contrast, Suggested options to move forwardThere doesn't seem to be a clear consensus on the way forward otherwise this PR wouldn't have been around for so long (indeed it's over 2 years since I introduced the Above, @mtias has noted:
That seems to suggest we need to land on having a single primary API for this "transformation" operation. That makes sense as it would be unclear for consumers to have two top-level APIs for the same operation. Therefore I am going to propose we choose from one of the following two options: Option 1 - deprecate
|
I'd vote for deprecating transform. Looking at it, the API seems to have some problems. Especially the second parameter, which for a multiple block transform is It seems weird to receive an array of arrays of inner blocks like that, and there's no indication of what the wrapping block is for each array, just some attributes that are in a separate parameter. It is possible to specify multiple block types in a multi-block transform (via the wildcard or by specifying more than one block type in the blocks array), so this seems like a shortcoming. Plus, i'm assuming the innerBlocks has the full block object, so that's another inconsistency.
The only alternative I can think of is to add a property to the transform to opt in to a different |
@talldan's thoughts make sense to me, I'm also in favor of the more sane "convert" API. I do have some concerns about how widespread this deprecation could be though but maybe it's fine since it's just a single warning. |
This was brought up in the Core Editor chat in Slack so I'll wait a couple of days for any further opinions before outlining a plan to move this forward. |
I wonder if rather than moving core blocks over to While the changes are fairly simple I personally find fewer changes makes it easier to be more thorough in a review. It'll also result in fewer conflicts. |
Thanks for the recap and your persistence, @getdave. To the question of which API suits us best, IMO that's certainly On one hand, block transforms or transformations have become established terminology in Gutenberg. On the other, it's not easy to communicate a switch from an API (transforms) to a new API that means roughly-but-not-exactly the same (convert). This is because, in reality, we have in our hands a compatibility-breaking version change of an API disguised as a renaming. In that sense, Option 2 is an easier proposition for API consumers. As hinted in the above paragraph, part of the issue is that we don't have versioning built into these APIs (as with most non-REST APIs of Gutenberg, with the notable exceptions of block.json and theme.json). That is something hard to solve without harming the developer experience of API consumers. (For a moment, I considered a different proposal that tried to marry Options 1 and 2: what if we use function arity as a signal? A So I'm not sure what the exit is.
Maybe we just bite the bullet and effectively deprecate |
blocksArray.map( ( currentBlock ) => currentBlock.innerBlocks ), | ||
); | ||
} | ||
} else if ( has( transformation, '__experimentalConvert' ) ) { |
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 is in the stable product a resolution of #40316 will be needed prior to merge.
This idea isn't as bad as it first seems when you consider that |
Closes #15972
Related: #17743
Previously: #14908, #11979
This pull request seeks to stabilize and promote what is currently implemented as the
__experimentalConvert
function member of a block transformation. In doing so, it deprecates the existingtransform
method.convert
differs fromtransform
in that it will receive the full block object (or an array of block objects if the transform hasisMultiBlock: true
). By contrast, thetransform
function would receive anattributes
object andinnerBlocks
array (or an array ofattributes
objects and an array ofinnerBlocks
arrays if the transform hasisMultiBlock: true
). The multi-block transform signature was especially cumbersome to use, a consequence of the fact that it was added at a later time (#11979, see also a similar reflection on this at the time at #11979 (review)).Existing use of
transform
should be unaffected, but will log with a deprecation warning. All existing core block transforms have been updated. Likewise, the documentation has been updated to reflect the recommended usage. Furthermore, it has been enhanced to detail supported usage of either transforming from or to multiple blocks.Needs Decision: Throughout the process of putting together this branch, I've rediscovered the many inconsistencies we've carried with us in how we support transforms of varying types and use-cases. Much of this was summarized at #15972 (comment) and subsequent comments. There are also related enhancement requests at #17758 and #14755. Even with these changes, there are still some inconsistencies around how we perform various block operations, where some functions (transform
isMatch
, deprecationmigrate
, shortcode transformconvert
will still receive an "attributes" object). I don't know that all of these would make sense to similarly port to a signature which receives a full block object. In the case of the shortcode transform, "attributes" holds a different meaning, and it and the "files" transform don't operate on blocks as the source. But, as mentioned in #17758, a shortcode does have a similar need for an "inner value" as part of its transformation. It would be worth having a discussion to settle on a clear picture for if and how we consider block operations in how it makes sense to devise a function signature.Testing Instructions:
Verify there are no regressions in the behavior of block transforms.