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

Migrate existing single button blocks to buttons/button block (wrapped) #19919

Closed
strarsis opened this issue Jan 28, 2020 · 9 comments · Fixed by #20063
Closed

Migrate existing single button blocks to buttons/button block (wrapped) #19919

strarsis opened this issue Jan 28, 2020 · 9 comments · Fixed by #20063
Labels
[Block] Buttons Affects the Buttons Block Good First Issue An issue that's suitable for someone looking to contribute for the first time [Type] Enhancement A suggestion for improvement.

Comments

@strarsis
Copy link
Contributor

strarsis commented Jan 28, 2020

Describe the bug
When editing pages that contain singular button blocks that had been edited before the buttons block has been introduced, there is no automatic or easy way for converting/upgrading these singular button blocks into button blocks that are wrapped by a buttons block.
The easiest approach would just mimic what is already done with any other elements and wrapping it into a group block: When a singular button block is selected that isn't wrapped by a buttons block yet, the transform menu should not only offer "Wrap into group" as with all other blocks, but also "Wrap into buttons block".
Additionally, an extra option that auto-wraps all the singular button blocks into buttons blocks would be a nice addition. Some pages can have lots of these singular buttons.

To reproduce
Edit a page that had been created when Gutenberg had no buttons block yet.
Select a singular, unwrapped button block, open the transform options.
Notice that there is no transformation that wraps this singular button block into a buttons block.

Expected behavior
A transform option for wrapping a singular button block into a buttons block.

@talldan talldan added [Block] Buttons Affects the Buttons Block [Type] Enhancement A suggestion for improvement. Good First Issue An issue that's suitable for someone looking to contribute for the first time labels Jan 28, 2020
@talldan
Copy link
Contributor

talldan commented Jan 28, 2020

Yep, makes sense that there's a transform from Button to Buttons. 👍

@Ringish
Copy link
Contributor

Ringish commented Feb 4, 2020

I'm working on this!

@talldan
Copy link
Contributor

talldan commented Feb 5, 2020

Thanks @Ringish. If you need any code to reference, there's a similar transform from Image to Galleries:

from: [
{
type: 'block',
isMultiBlock: true,
blocks: [ 'core/image' ],
transform: ( attributes ) => {
// Init the align and size from the first item which may be either the placeholder or an image.
let { align, sizeSlug } = attributes[ 0 ];
// Loop through all the images and check if they have the same align and size.
align = every( attributes, [ 'align', align ] )
? align
: undefined;
sizeSlug = every( attributes, [ 'sizeSlug', sizeSlug ] )
? sizeSlug
: undefined;
const validImages = filter( attributes, ( { url } ) => url );
return createBlock( 'core/gallery', {
images: validImages.map(
( { id, url, alt, caption } ) => ( {
id,
url,
alt,
caption,
} )
),
ids: validImages.map( ( { id } ) => id ),
align,
sizeSlug,
} );
},
},

isMultiBlock: true is pretty handy as it means a single selected image or multiple selected images can be converted to a gallery. Seems like the same should be possible for the button block.

and the docs for transforms are here:
https://developer.wordpress.org/block-editor/developers/block-api/block-registration/#transforms-optional

@Ringish
Copy link
Contributor

Ringish commented Feb 5, 2020

@talldan @strarsis
Got it working on my fork and the branch here.
buttons

Is this correct and expected behaviour?

@talldan
Copy link
Contributor

talldan commented Feb 6, 2020

@Ringish That looks great! You should be able to open a pull request against this repo using the branch on your fork.

Let me know if you need any assistance with that. I anticipate some tests will need to be updated, this fixture here I think just needs to be manually updated with the new transform:
https://github.com/WordPress/gutenberg/blob/master/packages/e2e-tests/fixtures/block-transforms.js

You can run unit tests locally by using npm run test or npm run test:watch, and end to end tests are npm run test-e2e or npm run test-e2e:watch.

@strarsis
Copy link
Contributor Author

strarsis commented Feb 6, 2020

Maybe it would make sense to add a 'vertical' list as the buttons block default style so the button blocks wrapped by buttons block are still on separate lines.

@talldan
Copy link
Contributor

talldan commented Feb 7, 2020

@strarsis True. At the moment you can add separate Buttons blocks, but that doesn't seem completely intuitive.

I think this would have to be a separate issue though, is that something you'd be happy to create?

@strarsis
Copy link
Contributor Author

strarsis commented Feb 7, 2020

@talldan: #20081

@talldan
Copy link
Contributor

talldan commented Feb 7, 2020

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Buttons Affects the Buttons Block Good First Issue An issue that's suitable for someone looking to contribute for the first time [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants