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
Next Next commit
RichText: Add the RichText.Content component to be used in conjunctio…
…n with RichText
  • Loading branch information
youknowriad committed Apr 20, 2018
commit 90852ca14eb8cd6350dedc639beb2c237763355f
6 changes: 5 additions & 1 deletion blocks/library/audio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ export const settings = {
return (
<figure>
<audio controls="controls" src={ src } />
{ caption && caption.length > 0 && <figcaption>{ caption }</figcaption> }
{ caption && caption.length > 0 && (
<figcaption>
<RichText.Content>{ caption }</RichText.Content>
</figcaption>
) }
</figure>
);
},
Expand Down
4 changes: 2 additions & 2 deletions blocks/library/button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export const settings = {
return (
<div className={ `align${ align }` }>
<a className={ linkClass } href={ url } title={ title } style={ buttonStyle }>
{ text }
<RichText.Content>{ text }</RichText.Content>
</a>
</div>
);
Expand All @@ -232,7 +232,7 @@ export const settings = {
return (
<div className={ `align${ align }` } style={ { backgroundColor: color } }>
<a href={ url } title={ title } style={ { color: textColor } }>
{ text }
<RichText.Content>{ text }</RichText.Content>
</a>
</div>
);
Expand Down
8 changes: 6 additions & 2 deletions blocks/library/cover-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ export const settings = {
return (
<div className={ classes } style={ style }>
{ title && title.length > 0 && (
<p className="wp-block-cover-image-text">{ title }</p>
<p className="wp-block-cover-image-text">
<RichText.Content>{ title }</RichText.Content>
</p>
) }
</div>
);
Expand Down Expand Up @@ -264,7 +266,9 @@ export const settings = {

return (
<section className={ classes } style={ style }>
<h2>{ title }</h2>
<h2>
<RichText.Content>{ title }</RichText.Content>
</h2>
</section>
);
},
Expand Down
6 changes: 5 additions & 1 deletion blocks/library/embed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,11 @@ function getEmbedBlockSettings( { title, description, icon, category = 'embed',
return (
<figure className={ embedClassName }>
{ `\n${ url }\n` /* URL needs to be on its own line. */ }
{ caption && caption.length > 0 && <figcaption>{ caption }</figcaption> }
{ caption && caption.length > 0 && (
<figcaption>
<RichText.Content>{ caption }</RichText.Content>
</figcaption>
) }
</figure>
);
},
Expand Down
7 changes: 6 additions & 1 deletion blocks/library/gallery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { mediaUpload } from '@wordpress/utils';
import './editor.scss';
import './style.scss';
import { createBlock } from '../../api';
import RichText from '../../rich-text';
import { default as GalleryBlock, defaultColumnsNumber } from './block';

const blockAttributes = {
Expand Down Expand Up @@ -186,7 +187,11 @@ export const settings = {
<li key={ image.id || image.url } className="blocks-gallery-item">
<figure>
{ href ? <a href={ href }>{ img }</a> : img }
{ image.caption && image.caption.length > 0 && <figcaption>{ image.caption }</figcaption> }
{ image.caption && image.caption.length > 0 && (
<figcaption>
<RichText.Content>{ image.caption }</RichText.Content>
</figcaption>
) }
</figure>
</li>
);
Expand Down
2 changes: 1 addition & 1 deletion blocks/library/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export const settings = {

return (
<Tag style={ { textAlign: align } } >
{ content }
<RichText.Content>{ content }</RichText.Content>
</Tag>
);
},
Expand Down
13 changes: 11 additions & 2 deletions blocks/library/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { __ } from '@wordpress/i18n';
import './style.scss';
import './editor.scss';
import { createBlock, getBlockAttributes, getBlockType } from '../../api';
import RichText from '../../rich-text';
import ImageBlock from './block';

export const name = 'core/image';
Expand Down Expand Up @@ -173,7 +174,11 @@ export const settings = {
return (
<figure className={ align ? `align${ align }` : null }>
{ href ? <a href={ href }>{ image }</a> : image }
{ caption && caption.length > 0 && <figcaption>{ caption }</figcaption> }
{ caption && caption.length > 0 && (
<figcaption>
<RichText.Content>{ caption }</RichText.Content>
</figcaption>
) }
</figure>
);
},
Expand All @@ -197,7 +202,11 @@ export const settings = {
return (
<figure className={ align ? `align${ align }` : null } style={ figureStyle }>
{ href ? <a href={ href }>{ image }</a> : image }
{ caption && caption.length > 0 && <figcaption>{ caption }</figcaption> }
{ caption && caption.length > 0 && (
<figcaption>
<RichText.Content>{ caption }</RichText.Content>
</figcaption>
) }
</figure>
);
},
Expand Down
11 changes: 6 additions & 5 deletions blocks/library/list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { find, compact, get, initial, last, isEmpty } from 'lodash';
/**
* WordPress dependencies
*/
import { Component, createElement, Fragment } from '@wordpress/element';
import { Component, Fragment } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
Expand Down Expand Up @@ -304,11 +304,12 @@ export const settings = {

save( { attributes } ) {
const { nodeName, values } = attributes;
const Tag = nodeName.toLowerCase();

return createElement(
nodeName.toLowerCase(),
null,
values
return (
<Tag>
<RichText.Content>{ values }</RichText.Content>
</Tag>
);
},
};
6 changes: 5 additions & 1 deletion blocks/library/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,10 @@ export const settings = {
textAlign: align,
};

return <p style={ styles } className={ className ? className : undefined }>{ content }</p>;
return (
<p style={ styles } className={ className ? className : undefined }>
<RichText.Content>{ content }</RichText.Content>
</p>
);
},
};
2 changes: 1 addition & 1 deletion blocks/library/preformatted/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ export const settings = {
save( { attributes } ) {
const { content } = attributes;

return <pre>{ content }</pre>;
return <pre><RichText.Content>{ content }</RichText.Content></pre>;
},
};
4 changes: 2 additions & 2 deletions blocks/library/pullquote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const settings = {
<p key={ i }>{ paragraph.children && paragraph.children.props.children }</p>
) }
{ citation && citation.length > 0 && (
<cite>{ citation }</cite>
<cite><RichText.Content>{ citation }</RichText.Content></cite>
) }
</blockquote>
);
Expand All @@ -150,7 +150,7 @@ export const settings = {
<p key={ i }>{ paragraph.children && paragraph.children.props.children }</p>
) }
{ citation && citation.length > 0 && (
<footer>{ citation }</footer>
<footer><RichText.Content>{ citation }</RichText.Content></footer>
) }
</blockquote>
);
Expand Down
8 changes: 6 additions & 2 deletions blocks/library/quote/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ export const settings = {
<p key={ i }>{ paragraph.children && paragraph.children.props.children }</p>
) ) }
{ citation && citation.length > 0 && (
<cite>{ citation }</cite>
<cite>
<RichText.Content>{ citation }</RichText.Content>
</cite>
) }
</blockquote>
);
Expand Down Expand Up @@ -275,7 +277,9 @@ export const settings = {
<p key={ i }>{ paragraph.children && paragraph.children.props.children }</p>
) ) }
{ citation && citation.length > 0 && (
<footer>{ citation }</footer>
<footer>
<RichText.Content>{ citation }</RichText.Content>
</footer>
) }
</blockquote>
);
Expand Down
6 changes: 5 additions & 1 deletion blocks/library/subhead/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export const settings = {
save( { attributes, className } ) {
const { content } = attributes;

return <p className={ className }>{ content }</p>;
return (
<p className={ className }>
<RichText.Content>{ content }</RichText.Content>
</p>
);
},
};
3 changes: 2 additions & 1 deletion blocks/library/table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import './style.scss';
import TableBlock from './table-block';
import BlockControls from '../../block-controls';
import BlockAlignmentToolbar from '../../block-alignment-toolbar';
import RichText from '../../rich-text';

export const name = 'core/table';

Expand Down Expand Up @@ -85,7 +86,7 @@ export const settings = {
const { content, align } = attributes;
return (
<table className={ align ? `align${ align }` : null }>
{ content }
<RichText.Content>{ content }</RichText.Content>
</table>
);
},
Expand Down
6 changes: 5 additions & 1 deletion blocks/library/text-columns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ export const settings = {
<div className={ `align${ width } columns-${ columns }` }>
{ times( columns, ( index ) =>
<div className="wp-block-column" key={ `column-${ index }` }>
<p>{ content && content[ index ].children }</p>
<p>
<RichText.Content>
{ content && content[ index ].children }
</RichText.Content>
</p>
</div>
) }
</div>
Expand Down
6 changes: 5 additions & 1 deletion blocks/library/verse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export const settings = {
},

save( { attributes, className } ) {
return <pre className={ className }>{ attributes.content }</pre>;
return (
<pre className={ className }>
<RichText.Content>{ attributes.content }</RichText.Content>
</pre>
);
},
};
6 changes: 5 additions & 1 deletion blocks/library/video/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ export const settings = {

<figure className={ align ? `align${ align }` : null }>
{ src && <video controls src={ src } /> }
{ caption && caption.length > 0 && <figcaption>{ caption }</figcaption> }
{ caption && caption.length > 0 && (
<figcaption>
<RichText.Content>{ caption }</RichText.Content>
</figcaption>
) }
</figure>
);
},
Expand Down
21 changes: 20 additions & 1 deletion blocks/rich-text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ a traditional `input` field, usually when the user exits the field.

*Optional.* A list of autocompleters to use instead of the default.

## RichText.Content

When using RichText in the edit function of blocks, the usage of `RichText.Content` is recommended in the save function of your blocks to save the correct HTML.


## Example

{% codetabs %}
Expand All @@ -95,6 +100,12 @@ wp.blocks.registerBlockType( /* ... */, {
}
} );
},

save: function() {
return wp.element.createElement( 'h2, {},
Copy link
Member

Choose a reason for hiding this comment

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

Missing single quote after h2.

wp.element.createElement( wp.blocks.RichText.Content, {}, props.attributes.content )
);
}
} );
```
{% ESNext %}
Expand Down Expand Up @@ -122,6 +133,14 @@ registerBlockType( /* ... */, {
/>
);
},

save( { attributes } ) {
return (
<h2>
<RichText.Content>{attributes.content}</RichText.Content>
</h2>
);
}
} );
```
{% end %}
{% end %}
15 changes: 13 additions & 2 deletions blocks/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import 'element-closest';
/**
* WordPress dependencies
*/
import { Component, Fragment, compose } from '@wordpress/element';
import { Component, Fragment, compose, RawHTML } from '@wordpress/element';
import { keycodes, createBlobURL, isHorizontalEdge, getRectangleFromRange, getScrollContainer } from '@wordpress/utils';
import { withSafeTimeout, Slot } from '@wordpress/components';
import { withSelect } from '@wordpress/data';
Expand Down Expand Up @@ -876,7 +876,7 @@ RichText.defaultProps = {
format: 'element',
};

export default compose( [
const RichTextContainer = compose( [
Copy link
Member

Choose a reason for hiding this comment

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

Can we just reassign RichText here? RichText = compose( [ ] )( RichText );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was not possible because we use the inner RichText in unit tests.

withBlockEditContext,
withSelect( ( select, { isSelected, blockEditContext } ) => {
const { isViewportMatch = identity } = select( 'core/viewport' ) || {};
Expand All @@ -888,3 +888,14 @@ export default compose( [
} ),
withSafeTimeout,
] )( RichText );

RichTextContainer.Content = ( { children, format = 'element' } ) => {
switch ( format ) {
case 'string':
return <RawHTML>{ children }</RawHTML>;
default:
return children;
}
};

export default RichTextContainer;
12 changes: 9 additions & 3 deletions docs/blocks/block-controls-toolbars-and-inspector.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ registerBlockType( 'gutenberg-boilerplate-es5/hello-world-step-04', {
var content = props.attributes.content,
alignment = props.attributes.alignment;

return el( 'p', { className: props.className, style: { textAlign: alignment } }, content );
return el( 'p', { className: props.className, style: { textAlign: alignment } },
el( RichText.Content, {}, content )
);
},
} );
```
Expand Down Expand Up @@ -143,13 +145,17 @@ registerBlockType( 'gutenberg-boilerplate-esnext/hello-world-step-04', {
save( { attributes, className } ) {
const { content, alignment } = attributes;

return <p className={ className } style={ { textAlign: alignment } }>{ content }</p>;
return (
<p className={ className } style={ { textAlign: alignment } }>
<RichText.Content>{ content }</RichText.Content>
</p>
);
},
} );
```
{% end %}

Note that `BlockControls` is only visible when the block is currently selected.
Note that `BlockControls` is only visible when the block is currently selected.

## Inspector

Expand Down
Loading