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

Transform multiple heading blocks to list or paragraphs #24977

Merged
merged 2 commits into from
Sep 2, 2020
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
28 changes: 16 additions & 12 deletions packages/block-library/src/heading/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ const transforms = {
from: [
{
type: 'block',
isMultiBlock: true,
blocks: [ 'core/paragraph' ],
transform: ( { content, anchor } ) => {
return createBlock( name, {
content,
anchor,
} );
},
transform: ( attributes ) =>
attributes.map( ( { content, anchor } ) =>
createBlock( name, {
content,
anchor,
} )
),
},
{
type: 'raw',
Expand Down Expand Up @@ -69,13 +71,15 @@ const transforms = {
to: [
{
type: 'block',
isMultiBlock: true,
blocks: [ 'core/paragraph' ],
transform: ( { content, anchor } ) => {
return createBlock( 'core/paragraph', {
content,
anchor,
} );
},
transform: ( attributes ) =>
attributes.map( ( { content, anchor } ) =>
createBlock( 'core/paragraph', {
content,
anchor,
} )
),
},
],
};
Expand Down
19 changes: 18 additions & 1 deletion packages/block-library/src/list/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const transforms = {
{
type: 'block',
isMultiBlock: true,
blocks: [ 'core/paragraph' ],
blocks: [ 'core/paragraph', 'core/heading' ],
transform: ( blockAttributes ) => {
return createBlock( 'core/list', {
values: toHTMLString( {
Expand Down Expand Up @@ -157,6 +157,23 @@ const transforms = {
} )
),
},
{
type: 'block',
blocks: [ 'core/heading' ],
transform: ( { values } ) =>
split(
create( {
html: values,
multilineTag: 'li',
multilineWrapperTags: [ 'ul', 'ol' ],
} ),
__UNSTABLE_LINE_SEPARATOR
).map( ( piece ) =>
createBlock( 'core/heading', {
content: toHTMLString( { value: piece } ),
} )
),
},
{
type: 'block',
blocks: [ 'core/quote' ],
Expand Down
29 changes: 18 additions & 11 deletions packages/e2e-tests/specs/editor/various/block-switcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ describe( 'Block Switcher', () => {
expect( await hasBlockSwitcher() ).toBeTruthy();

// Verify the correct block transforms appear.
expect( await getAvailableBlockTransforms() ).toEqual( [
'Group',
'Paragraph',
'Quote',
'Pullquote',
] );
expect( await getAvailableBlockTransforms() ).toEqual(
expect.arrayContaining( [
'Group',
'Paragraph',
'Quote',
'Heading',
'Pullquote',
] )
);
} );

it( 'Should show the expected block transforms on the list block when the quote block is removed', async () => {
Expand All @@ -47,11 +50,14 @@ describe( 'Block Switcher', () => {
expect( await hasBlockSwitcher() ).toBeTruthy();

// Verify the correct block transforms appear.
expect( await getAvailableBlockTransforms() ).toEqual( [
'Group',
'Paragraph',
'Pullquote',
] );
expect( await getAvailableBlockTransforms() ).toEqual(
expect.arrayContaining( [
'Group',
'Paragraph',
'Pullquote',
'Heading',
] )
);
} );

it( 'Should not show the block switcher if all the blocks the list block transforms into are removed', async () => {
Expand All @@ -62,6 +68,7 @@ describe( 'Block Switcher', () => {
'core/pullquote',
'core/paragraph',
'core/group',
'core/heading',
].map( ( block ) => wp.blocks.unregisterBlockType( block ) );
} );

Expand Down