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

Add possibility to manage innerBlocks while migrating deprecated blocks. #5932

Merged
merged 1 commit into from
Apr 10, 2018

Conversation

jorgefilipecosta
Copy link
Member

@jorgefilipecosta jorgefilipecosta commented Apr 2, 2018

This PR was extracted from #5452, to make reviewing easier as this ones just focus on changes to the blocks API.

Description

Before the migrate function received the existing attributes and returned the new attributes now, migrate receives an object with attributes and innerBlocks and returns another object with the migrated attributes & innerBlocks.
This allows, for example, to migrate an attribute to an innerBlock, or the contrary move an inner block to be an attribute.
These changes were used to migrate existing cover image blocks to the nested version.

How Has This Been Tested?

To test the cover image migration merge this changes in a temporary branch with the nesting in cover image PR.

Add a post with following code:

<!-- wp:cover-image {"url":"http://via.placeholder.com/350x150","contentAlign":"right","id":14,"dimRatio":0} -->
<div class="wp-block-cover-image has-right-content" style="background-image:url(http://via.placeholder.com/350x150)">
    <p class="wp-block-cover-image-text">aaaaa</p>
</div>
<!-- /wp:cover-image -->

<!-- wp:cover-image {"url":"http://via.placeholder.com/350x150","id":14} -->
<div class="wp-block-cover-image has-background-dim" style="background-image:url(http://via.placeholder.com/350x150)">
    <p class="wp-block-cover-image-text">aaaaa</p>
</div>
<!-- /wp:cover-image -->

<!-- wp:paragraph {"fontSize":56} -->
<p style="font-size:56px">dsfdsfs</p>
<!-- /wp:paragraph -->

Open the post in the visual editor and verify no invalid block error appears and the cover image blocks and paragraphs were migrated.

@jorgefilipecosta jorgefilipecosta added [Type] Enhancement A suggestion for improvement. [Feature] Block API API that allows to express the block paradigm. Backwards Compatibility Issues or PRs that impact backwards compatability labels Apr 2, 2018
@jorgefilipecosta jorgefilipecosta self-assigned this Apr 2, 2018
@jorgefilipecosta jorgefilipecosta force-pushed the add/innerblocks-deprecation-migration branch from 8edc1bb to aa7fc43 Compare April 2, 2018 16:19
@jorgefilipecosta jorgefilipecosta force-pushed the add/nesting-cover-image branch from fa54943 to 59efeb4 Compare April 3, 2018 16:43
@@ -181,12 +181,14 @@ export function getAttributesFromDeprecatedVersion( blockType, innerHTML, attrib
try {
// Handle migration of older attributes into current version if necessary.
const deprecatedBlockAttributes = getBlockAttributes( deprecatedBlockType, innerHTML, attributes );
const migratedBlockAttributes = deprecatedBlockType.migrate ? deprecatedBlockType.migrate( deprecatedBlockAttributes ) : deprecatedBlockAttributes;
const migratedBlockAttributesAndInnerBlocks = deprecatedBlockType.migrate ?
deprecatedBlockType.migrate( { attributes: deprecatedBlockAttributes, innerBlocks } ) :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny that to avoid breaking changes, we're introducing another breaking change here :) (especially the return value)

Options here:

  • We're ok with this, assuming people didn't use migrate yet.
  • We're not ok and we try to support the old API. Here's how I would achieve it:
    • Instead of passing an object, pass the innerBlocks as a second argument.
    • If you want to update both attributes and innerBlocks, return an array with two elements, if you return just an object assume we update only the attributes.

Thoughs @aduth @mcsf

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still forming an opinion, but leaning toward number 2. In practice, I doubt that migrate has been used a lot in the wild up till now, but that's entirely speculative (thus epistemically irresponsible? 😄), and I think it's bad karma to be changing a migration API at this stage.

@jorgefilipecosta jorgefilipecosta force-pushed the add/nesting-cover-image branch from 59efeb4 to 991e56c Compare April 4, 2018 13:23
@jorgefilipecosta jorgefilipecosta force-pushed the add/innerblocks-deprecation-migration branch from aa7fc43 to 291d97c Compare April 4, 2018 13:25
@jorgefilipecosta jorgefilipecosta added this to the 2.7 milestone Apr 5, 2018
@jorgefilipecosta jorgefilipecosta force-pushed the add/nesting-cover-image branch 2 times, most recently from 5789d23 to da419d3 Compare April 6, 2018 17:49
@jorgefilipecosta jorgefilipecosta force-pushed the add/innerblocks-deprecation-migration branch 3 times, most recently from 7059339 to 21545b9 Compare April 7, 2018 01:45
@jorgefilipecosta jorgefilipecosta force-pushed the add/nesting-cover-image branch from da419d3 to df69227 Compare April 7, 2018 01:48
@jorgefilipecosta jorgefilipecosta force-pushed the add/innerblocks-deprecation-migration branch from 21545b9 to 38bcf8d Compare April 7, 2018 01:49
@youknowriad youknowriad mentioned this pull request Apr 10, 2018
3 tasks
@jorgefilipecosta
Copy link
Member Author

This PR was updated to be back compatible I followed the suggestion and now we receive two arguments and return an array with attributes and inner blocks or an object with just the attributes.

const migratedBlockAttributesAndInnerBlocks = deprecatedBlockType.migrate &&
deprecatedBlockType.migrate( deprecatedBlockAttributes, innerBlocks );

if ( isArray( migratedBlockAttributesAndInnerBlocks ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: Can we simplify the logic here or is it too clever?

const [ migratedAttributes, migratedInnerBlocks = innerBlocks ] = castArray( migratedBlockAttributesAndInnerBlocks );

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this version castArray simplifies the logic, I updated to use it :)

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@jorgefilipecosta jorgefilipecosta force-pushed the add/nesting-cover-image branch from df69227 to e541d8c Compare April 10, 2018 10:45
@jorgefilipecosta jorgefilipecosta force-pushed the add/innerblocks-deprecation-migration branch 2 times, most recently from 5d5d9ad to fdc77b1 Compare April 10, 2018 10:53
@jorgefilipecosta
Copy link
Member Author

Thank you for the reviews @youknowriad and @mcsf!

@jorgefilipecosta jorgefilipecosta force-pushed the add/innerblocks-deprecation-migration branch from fdc77b1 to 8fb5d8e Compare April 10, 2018 18:02
@jorgefilipecosta jorgefilipecosta changed the base branch from add/nesting-cover-image to master April 10, 2018 18:02
@jorgefilipecosta jorgefilipecosta force-pushed the add/innerblocks-deprecation-migration branch from 8fb5d8e to 8e46963 Compare April 10, 2018 18:05
…ks. Added migration logic to migrate existing cover images into the new nested version.

Before the migrate function received the existing attributes and returned the new attributes now, migrate receives an object with attributes and innerBlocks and returns another object with the migrated attributes & innerBlocks.
@jorgefilipecosta jorgefilipecosta force-pushed the add/innerblocks-deprecation-migration branch from 8e46963 to 58ea91d Compare April 10, 2018 18:21
@jorgefilipecosta jorgefilipecosta merged commit ab7a429 into master Apr 10, 2018
@jorgefilipecosta jorgefilipecosta deleted the add/innerblocks-deprecation-migration branch April 10, 2018 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backwards Compatibility Issues or PRs that impact backwards compatability [Feature] Block API API that allows to express the block paradigm. [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants