Skip to content

Commit d8d6992

Browse files
committed
Quote block: Update transforms since the content is now a React.children object
- Avoid data loss while switching blocks
1 parent 2d553b1 commit d8d6992

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

blocks/library/heading/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { isString } from 'lodash';
5+
16
/**
27
* Internal dependencies
38
*/
@@ -38,9 +43,9 @@ registerBlock( 'core/heading', {
3843
blocks: [ 'core/text' ],
3944
transform: ( { content, align } ) => {
4045
if ( Array.isArray( content ) ) {
41-
// TODO this appears to always be true?
42-
// TODO reject the switch if more than one paragraph
43-
content = content[ 0 ];
46+
content = wp.element.concatChildren( content.map( ( elt ) =>
47+
! elt || isString( elt ) || elt.type !== 'p' ? elt : elt.props.children
48+
) );
4449
}
4550
return {
4651
nodeName: 'H2',
@@ -56,7 +61,7 @@ registerBlock( 'core/heading', {
5661
blocks: [ 'core/text' ],
5762
transform: ( { content, align } ) => {
5863
return {
59-
content: [ content ],
64+
content,
6065
align
6166
};
6267
}

blocks/library/quote/index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { isString } from 'lodash';
5+
16
/**
27
* Internal dependencies
38
*/
@@ -54,9 +59,9 @@ registerBlock( 'core/quote', {
5459
{
5560
type: 'block',
5661
blocks: [ 'core/heading' ],
57-
transform: ( { content = '' } ) => {
62+
transform: ( { content } ) => {
5863
return {
59-
value: [ content ]
64+
value: content
6065
};
6166
}
6267
}
@@ -66,20 +71,23 @@ registerBlock( 'core/quote', {
6671
type: 'block',
6772
blocks: [ 'core/text' ],
6873
transform: ( { value, citation } ) => {
69-
let content = value ? value : [];
70-
content = citation && citation.trim() ? content.concat( citation ) : content;
7174
return {
72-
content
75+
content: wp.element.concatChildren( value, citation )
7376
};
7477
}
7578
},
7679
{
7780
type: 'block',
7881
blocks: [ 'core/heading' ],
79-
transform: ( { value } ) => {
82+
transform: ( { value, citation } ) => {
83+
if ( Array.isArray( value ) ) {
84+
value = wp.element.concatChildren( value.map( ( elt ) =>
85+
! elt || isString( elt ) || elt.type !== 'p' ? elt : elt.props.children
86+
) );
87+
}
8088
return {
8189
nodeName: 'H2',
82-
content: value && value[ 0 ]
90+
content: wp.element.concatChildren( value, citation )
8391
};
8492
}
8593
}

element/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function renderToString( element ) {
7575
export function concatChildren( ...childrens ) {
7676
return childrens.reduce( ( memo, children, i ) => {
7777
Children.forEach( children, ( child, j ) => {
78-
if ( 'string' !== typeof child ) {
78+
if ( child && 'string' !== typeof child ) {
7979
child = cloneElement( child, {
8080
key: [ i, j ].join()
8181
} );

languages/gutenberg.pot

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ msgstr ""
2828
msgid "Freeform"
2929
msgstr ""
3030

31-
#: blocks/library/heading/index.js:10
31+
#: blocks/library/heading/index.js:15
3232
msgid "Heading"
3333
msgstr ""
3434

35-
#: blocks/library/heading/index.js:25
35+
#: blocks/library/heading/index.js:30
3636
msgid "Heading %s"
3737
msgstr ""
3838

@@ -70,11 +70,11 @@ msgstr ""
7070
msgid "Justify"
7171
msgstr ""
7272

73-
#: blocks/library/quote/index.js:11
73+
#: blocks/library/quote/index.js:16
7474
msgid "Quote"
7575
msgstr ""
7676

77-
#: blocks/library/quote/index.js:35
77+
#: blocks/library/quote/index.js:40
7878
msgid "Quote style %d"
7979
msgstr ""
8080

0 commit comments

Comments
 (0)