Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix multiline transforms
  • Loading branch information
ellatrix committed Oct 10, 2018
commit 974211956af1204f0b011f3925c4e638b684ac98
3 changes: 2 additions & 1 deletion packages/block-library/src/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createBlobURL } from '@wordpress/blob';
import { createBlock } from '@wordpress/blocks';
import { select } from '@wordpress/data';
import { RichText } from '@wordpress/editor';
import { create, getTextContent } from '@wordpress/rich-text';

/**
* Internal dependencies
Expand Down Expand Up @@ -220,7 +221,7 @@ export const settings = {
// ensure download attribute is still set when fileName
// is undefined. Using '' here as `true` still leaves
// the attribute unset.
download={ RichText.getTextContent( fileName ) }
download={ getTextContent( create( { html: fileName } ) ) }
>
<RichText.Content
value={ downloadButtonText }
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export const settings = {

merge( attributes, attributesToMerge ) {
return {
content: RichText.concat( attributes.content, attributesToMerge.content ),
content: attributes.content + attributesToMerge.content,
};
},

Expand Down
25 changes: 14 additions & 11 deletions packages/block-library/src/list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
BlockControls,
RichText,
} from '@wordpress/editor';
import { replace, join, split, create, toHTMLString } from '@wordpress/rich-text';

const listContentSchema = {
...getPhrasingContentSchema(),
Expand Down Expand Up @@ -73,9 +74,9 @@ export const settings = {
blocks: [ 'core/paragraph' ],
transform: ( blockAttributes ) => {
return createBlock( 'core/list', {
values: RichText.join( blockAttributes.map( ( { content } ) =>
RichText.replace( content, /\n/g, '\u2028' )
), '\u2028' ),
values: toHTMLString( join( blockAttributes.map( ( { content } ) =>
replace( create( { html: content } ), /\n/g, '\u2028' )
), '\u2028' ), 'li' ),
} );
},
},
Expand Down Expand Up @@ -130,9 +131,12 @@ export const settings = {
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( { values } ) =>
RichText.split( values, '\u2028' ).map( ( content ) =>
createBlock( 'core/paragraph', { content } )
),
split( create( { html: values, multilineTag: 'li' } ), '\u2028' )
.map( ( piece ) =>
createBlock( 'core/paragraph', {
content: toHTMLString( piece ),
} )
),
},
{
type: 'block',
Expand Down Expand Up @@ -181,16 +185,15 @@ export const settings = {
],

merge( attributes, attributesToMerge ) {
const { values, content } = attributesToMerge;
const valueToMerge = values || content;
const { values } = attributesToMerge;

if ( RichText.isEmpty( valueToMerge ) ) {
if ( ! values || values === '<li></li>' ) {
return attributes;
}

return {
...attributes,
values: RichText.join( [ attributes.values, valueToMerge ], '\u2028' ),
values: attributes.values + values,
};
},

Expand Down Expand Up @@ -328,7 +331,7 @@ export const settings = {
blocks.push( createBlock( 'core/paragraph' ) );
}

if ( ! RichText.isEmpty( after ) ) {
if ( after !== '<li></li>' ) {
blocks.push( createBlock( 'core/list', {
ordered,
values: after,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export const settings = {

merge( attributes, attributesToMerge ) {
return {
content: RichText.concat( attributes.content, attributesToMerge.content ),
content: attributes.content + attributesToMerge.content,
};
},

Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/preformatted/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const settings = {

merge( attributes, attributesToMerge ) {
return {
content: RichText.concat( attributes.content, attributesToMerge.content ),
content: attributes.content + attributesToMerge.content,
};
},
};
27 changes: 17 additions & 10 deletions packages/block-library/src/quote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
AlignmentToolbar,
RichText,
} from '@wordpress/editor';
import { join, split, create, toHTMLString } from '@wordpress/rich-text';

const blockAttributes = {
value: {
Expand Down Expand Up @@ -54,7 +55,9 @@ export const settings = {
blocks: [ 'core/paragraph' ],
transform: ( attributes ) => {
return createBlock( 'core/quote', {
value: RichText.join( attributes.map( ( { content } ) => content ), '\u2028' ),
value: toHTMLString( join( attributes.map( ( { content } ) =>
create( { html: content } )
), '\u2028' ), 'p' ),
} );
},
},
Expand Down Expand Up @@ -95,9 +98,12 @@ export const settings = {
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( { value } ) =>
RichText.split( value, '\u2028' ).map( ( content ) =>
createBlock( 'core/paragraph', { content } )
),
split( create( { html: value, multilineTag: 'p' } ), '\u2028' )
.map( ( piece ) =>
createBlock( 'core/paragraph', {
content: toHTMLString( piece ),
} )
),
},
{
type: 'block',
Expand All @@ -106,22 +112,23 @@ export const settings = {
// If there is no quote content, use the citation as the
// content of the resulting heading. A nonexistent citation
// will result in an empty heading.
if ( RichText.isEmpty( value ) ) {
if ( value === '<p></p>' ) {
return createBlock( 'core/heading', {
content: citation,
} );
}

const values = RichText.split( value, '\u2028' );
const pieces = split( create( { html: value, multilineTag: 'p' } ), '\u2028' );
const quotePieces = pieces.slice( 1 );

return [
createBlock( 'core/heading', {
content: values[ 0 ],
content: toHTMLString( pieces[ 0 ] ),
} ),
createBlock( 'core/quote', {
...attrs,
citation,
value: RichText.join( values.slice( 1 ), '\u2028' ),
value: toHTMLString( quotePieces.length ? join( pieces.slice( 1 ), '\u2028' ) : create(), 'p' ),
} ),
];
},
Expand Down Expand Up @@ -193,8 +200,8 @@ export const settings = {
merge( attributes, attributesToMerge ) {
return {
...attributes,
value: RichText.join( [ attributes.value, attributesToMerge.value ], '\u2028' ),
citation: RichText.concat( attributes.citation, attributesToMerge.citation ),
value: attributes.value + attributesToMerge.value,
citation: attributes.citation + attributesToMerge.citation,
};
},

Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/verse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const settings = {

merge( attributes, attributesToMerge ) {
return {
content: RichText.concat( attributes.content, attributesToMerge.content ),
content: attributes.content + attributesToMerge.content,
};
},
};
31 changes: 2 additions & 29 deletions packages/editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ import {
insert,
isEmptyLine,
unstableToDom,
concat,
join,
replace,
} from '@wordpress/rich-text';
import { decodeEntities } from '@wordpress/html-entities';

Expand Down Expand Up @@ -786,9 +783,9 @@ export class RichText extends Component {
}

const record = this.formatToValue( value );
const prevRecord = this.formatToValue( prevProps.value );

if ( this.isActive() ) {
const prevRecord = this.formatToValue( prevProps.value );
const length = getTextContent( prevRecord ).length;
record.start = length;
record.end = length;
Expand Down Expand Up @@ -1017,36 +1014,12 @@ RichTextContainer.isEmpty = ( value = '' ) => {
return ! value || value.length === 0;
}

if ( typeof value === 'string' ) {
return value.length === 0;
}

return isEmpty( value );
return value.length === 0;
};

RichTextContainer.Content.defaultProps = {
format: 'string',
value: '',
};

RichTextContainer.getTextContent = ( html ) => {
return getTextContent( create( { html } ) );
};

RichTextContainer.concat = ( ...pieces ) => {
return toHTMLString( concat( ...pieces.map( ( html ) => create( { html } ) ) ) );
};

RichTextContainer.join = ( pieces, ...args ) => {
return toHTMLString( join( pieces.map( ( html ) => create( { html } ) ), ...args ) );
};

RichTextContainer.split = ( html, ...args ) => {
return split( create( { html } ), ...args ).map( ( value ) => toHTMLString( value ) );
};

RichTextContainer.replace = ( html, ...args ) => {
return toHTMLString( replace( create( { html } ), ...args ) );
};

export default RichTextContainer;