Skip to content
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 Deprecations: Provide extra data for isEligible check #48815

Merged
merged 4 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions docs/reference-guides/block-api/block-deprecation.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,21 @@ Deprecations are defined on a block type as its `deprecated` property, an array
- `attributes` (Object): The [attributes definition](/docs/reference-guides/block-api/block-attributes.md) of the deprecated form of the block.
- `supports` (Object): The [supports definition](/docs/reference-guides/block-api/block-registration.md) of the deprecated form of the block.
- `save` (Function): The [save implementation](/docs/reference-guides/block-api/block-edit-save.md) of the deprecated form of the block.
- `migrate` (Function, Optional): A function which, given the old attributes and inner blocks is expected to return either the new attributes or a tuple array of `[ attributes, innerBlocks ]` compatible with the block. As mentioned above, a deprecation's `migrate` will not be run if its `save` function does not return a valid block so you will need to make sure your migrations are available in all the deprecations where they are relevant.
- `isEligible` (Function, Optional): A function which, given the attributes and inner blocks of the parsed block, returns true if the deprecation can handle the block migration even if the block is valid. This is particularly useful in cases where a block is technically valid even once deprecated, but still requires updates to its attributes or inner blocks. This function is not called when the results of all previous deprecations' `save` functions were invalid.
- `migrate`: (Function, Optional). A function which, given the old attributes and inner blocks is expected to return either the new attributes or a tuple array of attributes and inner blocks compatible with the block. As mentioned above, a deprecation's `migrate` will not be run if its `save` function does not return a valid block so you will need to make sure your migrations are available in all the deprecations where they are relevant.
- _Parameters_
- `attributes`: The block's old attributes.
- `innerBlocks`: The block's old inner blocks.
- _Return_
- `Object | Array`: Either the updated block attributes or tuple array `[attributes, innerBlocks]`.
- `isEligible`: (Function, Optional). A function which returns `true` if the deprecation can handle the block migration even if the block is valid. It is particularly useful in cases where a block is technically valid even once deprecated, but still requires updates to its attributes or inner blocks. This function is **not** called when the results of all previous deprecations' save functions were invalid.
- _Parameters_
- `attributes`: The raw block attributes as parsed from the serialized HTML, and before the block type code is applied.
- `innerBlocks`: The block's current inner blocks.
- `data`: An object containing properties representing the block node and its resulting block object.
- `data.blockNode`: The raw form of the block as a result of parsing the serialized HTML.
- `data.block`: The block object, which is the result of applying the block type to the `blockNode`.
- _Return_
- `boolean`: Whether or not this otherwise valid block is eligible to be migrated by this deprecation.

It's important to note that `attributes`, `supports`, and `save` are not automatically inherited from the current version, since they can impact parsing and serialization of a block, so they must be defined on the deprecated object in order to be processed during a migration.

Expand All @@ -63,7 +76,7 @@ registerBlockType( 'gutenberg/block-with-deprecated-version', {
// ... other block properties go here

attributes,

supports,

save( props ) {
Expand All @@ -73,7 +86,7 @@ registerBlockType( 'gutenberg/block-with-deprecated-version', {
deprecated: [
{
attributes,

supports,

save( props ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export function applyBlockDeprecatedVersions( block, rawBlock, blockType ) {
const { isEligible = stubFalse } = deprecatedDefinitions[ i ];
if (
block.isValid &&
! isEligible( parsedAttributes, block.innerBlocks )
! isEligible( parsedAttributes, block.innerBlocks, {
blockNode: rawBlock,
block,
} )
) {
continue;
}
Expand Down