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

Update: Refactor paragraph block to be a functional component #18125

Merged
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
313 changes: 156 additions & 157 deletions packages/block-library/src/paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import {
PanelBody,
ToggleControl,
Expand All @@ -27,7 +26,7 @@ import {
} from '@wordpress/block-editor';
import { createBlock } from '@wordpress/blocks';
import { compose } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';
import { useSelect } from '@wordpress/data';

const { getComputedStyle } = window;

Expand All @@ -45,174 +44,174 @@ const applyFallbackStyles = withFallbackStyles( ( node, ownProps ) => {
};
} );

class ParagraphBlock extends Component {
constructor() {
super( ...arguments );
function ParagraphRTLToolbar( { direction, setDirection } ) {
const isRTL = useSelect( ( select ) => {
return select( 'core/block-editor' ).getSettings().isRTL;
} );

this.toggleDropCap = this.toggleDropCap.bind( this );
}

toggleDropCap() {
const { attributes, setAttributes } = this.props;
setAttributes( { dropCap: ! attributes.dropCap } );
}

getDropCapHelp( checked ) {
return checked ? __( 'Showing large initial letter.' ) : __( 'Toggle to show a large initial letter.' );
}
return ( isRTL && (
<Toolbar
controls={ [
{
icon: 'editor-ltr',
title: _x( 'Left to right', 'editor button' ),
isActive: direction === 'ltr',
onClick() {
setDirection( direction === 'ltr' ? undefined : 'ltr' );
},
},
] }
/>
) );
}

render() {
const {
attributes,
setAttributes,
mergeBlocks,
onReplace,
className,
backgroundColor,
textColor,
setBackgroundColor,
setTextColor,
fallbackBackgroundColor,
fallbackTextColor,
fallbackFontSize,
fontSize,
setFontSize,
isRTL,
} = this.props;
function ParagraphPanelColor( {
backgroundColor,
fallbackBackgroundColor,
fallbackTextColor,
fontSize,
setBackgroundColor,
setTextColor,
textColor,
} ) {
return (
<PanelColorSettings
title={ __( 'Color Settings' ) }
initialOpen={ false }
colorSettings={ [
{
value: backgroundColor,
onChange: setBackgroundColor,
label: __( 'Background Color' ),
},
{
value: textColor,
onChange: setTextColor,
label: __( 'Text Color' ),
},
] }
>
<ContrastChecker
{ ...{
textColor,
backgroundColor,
fallbackTextColor,
fallbackBackgroundColor,
fontSize,
} }
/>
</PanelColorSettings>
);
}

const {
align,
content,
dropCap,
placeholder,
direction,
} = attributes;
function ParagraphBlock( {
attributes,
backgroundColor,
className,
fallbackBackgroundColor,
fallbackFontSize,
fallbackTextColor,
fontSize,
mergeBlocks,
onReplace,
setAttributes,
setBackgroundColor,
setFontSize,
setTextColor,
textColor,
} ) {
const {
align,
content,
dropCap,
placeholder,
direction,
} = attributes;

return (
<>
<BlockControls>
<AlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
return (
<>
<BlockControls>
<AlignmentToolbar
value={ align }
onChange={ ( newAlign ) => setAttributes( { align: newAlign } ) }
/>
<ParagraphRTLToolbar
direction={ direction }
setDirection={ ( newDirection ) => setAttributes( { direction: newDirection } ) }
/>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Text Settings' ) } className="blocks-font-size">
<FontSizePicker
fallbackFontSize={ fallbackFontSize }
value={ fontSize.size }
onChange={ setFontSize }
/>
{ isRTL && (
<Toolbar
controls={ [
{
icon: 'editor-ltr',
title: _x( 'Left to right', 'editor button' ),
isActive: direction === 'ltr',
onClick() {
const nextDirection = direction === 'ltr' ? undefined : 'ltr';
setAttributes( {
direction: nextDirection,
} );
},
},
] }
/>
) }
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Text Settings' ) } className="blocks-font-size">
<FontSizePicker
fallbackFontSize={ fallbackFontSize }
value={ fontSize.size }
onChange={ setFontSize }
/>
<ToggleControl
label={ __( 'Drop Cap' ) }
checked={ !! dropCap }
onChange={ this.toggleDropCap }
help={ this.getDropCapHelp }
/>
</PanelBody>
<PanelColorSettings
title={ __( 'Color Settings' ) }
initialOpen={ false }
colorSettings={ [
{
value: backgroundColor.color,
onChange: setBackgroundColor,
label: __( 'Background Color' ),
},
{
value: textColor.color,
onChange: setTextColor,
label: __( 'Text Color' ),
},
] }
>
<ContrastChecker
{ ...{
textColor: textColor.color,
backgroundColor: backgroundColor.color,
fallbackTextColor,
fallbackBackgroundColor,
} }
fontSize={ fontSize.size }
/>
</PanelColorSettings>
</InspectorControls>
<RichText
identifier="content"
tagName="p"
className={ classnames( 'wp-block-paragraph', className, {
'has-text-color': textColor.color,
'has-background': backgroundColor.color,
'has-drop-cap': dropCap,
[ `has-text-align-${ align }` ]: align,
[ backgroundColor.class ]: backgroundColor.class,
[ textColor.class ]: textColor.class,
[ fontSize.class ]: fontSize.class,
} ) }
style={ {
backgroundColor: backgroundColor.color,
color: textColor.color,
fontSize: fontSize.size ? fontSize.size + 'px' : undefined,
direction,
} }
value={ content }
onChange={ ( nextContent ) => {
setAttributes( {
content: nextContent,
} );
} }
onSplit={ ( value ) => {
if ( ! value ) {
return createBlock( name );
<ToggleControl
label={ __( 'Drop Cap' ) }
checked={ !! dropCap }
onChange={ () => setAttributes( { dropCap: ! dropCap } ) }
help={ dropCap ?
__( 'Showing large initial letter.' ) :
__( 'Toggle to show a large initial letter.' )
}

return createBlock( name, {
...attributes,
content: value,
} );
} }
onMerge={ mergeBlocks }
onReplace={ onReplace }
onRemove={ onReplace ? () => onReplace( [] ) : undefined }
aria-label={ content ? __( 'Paragraph block' ) : __( 'Empty block; start writing or type forward slash to choose a block' ) }
placeholder={ placeholder || __( 'Start writing or type / to choose a block' ) }
__unstableEmbedURLOnPaste
/>
</PanelBody>
<ParagraphPanelColor
backgroundColor={ backgroundColor.color }
fallbackBackgroundColor={ fallbackBackgroundColor }
fallbackTextColor={ fallbackTextColor }
fontSize={ fontSize.size }
setBackgroundColor={ setBackgroundColor }
setTextColor={ setTextColor }
textColor={ textColor.color }
/>
</>
);
}
</InspectorControls>
<RichText
identifier="content"
tagName="p"
className={ classnames( 'wp-block-paragraph', className, {
'has-text-color': textColor.color,
'has-background': backgroundColor.color,
'has-drop-cap': dropCap,
[ `has-text-align-${ align }` ]: align,
[ backgroundColor.class ]: backgroundColor.class,
[ textColor.class ]: textColor.class,
[ fontSize.class ]: fontSize.class,
} ) }
style={ {
backgroundColor: backgroundColor.color,
color: textColor.color,
fontSize: fontSize.size ? fontSize.size + 'px' : undefined,
direction,
} }
value={ content }
onChange={ ( newContent ) => setAttributes( { content: newContent } ) }
onSplit={ ( value ) => {
if ( ! value ) {
return createBlock( name );
}

return createBlock( name, {
...attributes,
content: value,
} );
} }
onMerge={ mergeBlocks }
onReplace={ onReplace }
onRemove={ onReplace ? () => onReplace( [] ) : undefined }
aria-label={ content ? __( 'Paragraph block' ) : __( 'Empty block; start writing or type forward slash to choose a block' ) }
placeholder={ placeholder || __( 'Start writing or type / to choose a block' ) }
__unstableEmbedURLOnPaste
/>
</>
);
}

const ParagraphEdit = compose( [
withColors( 'backgroundColor', { textColor: 'color' } ),
withFontSizes( 'fontSize' ),
applyFallbackStyles,
withSelect( ( select ) => {
const { getSettings } = select( 'core/block-editor' );

return {
isRTL: getSettings().isRTL,
};
} ),
] )( ParagraphBlock );

export default ParagraphEdit;