@wordpress/block-library
Version:
Block library for the WordPress editor.
158 lines (146 loc) • 3.58 kB
JavaScript
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { __, _x, isRTL } from '@wordpress/i18n';
import {
ToolbarButton,
ToggleControl,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';
import {
AlignmentControl,
BlockControls,
InspectorControls,
RichText,
useBlockProps,
useSetting,
} from '@wordpress/block-editor';
import { createBlock } from '@wordpress/blocks';
import { formatLtr } from '@wordpress/icons';
/**
* Internal dependencies
*/
import { useOnEnter } from './use-enter';
const name = 'core/paragraph';
function ParagraphRTLControl( { direction, setDirection } ) {
return (
isRTL() && (
<ToolbarButton
icon={ formatLtr }
title={ _x( 'Left to right', 'editor button' ) }
isActive={ direction === 'ltr' }
onClick={ () => {
setDirection( direction === 'ltr' ? undefined : 'ltr' );
} }
/>
)
);
}
function ParagraphBlock( {
attributes,
mergeBlocks,
onReplace,
onRemove,
setAttributes,
clientId,
} ) {
const { align, content, direction, dropCap, placeholder } = attributes;
const isDropCapFeatureEnabled = useSetting( 'typography.dropCap' );
const blockProps = useBlockProps( {
ref: useOnEnter( { clientId, content } ),
className: classnames( {
'has-drop-cap': dropCap,
[ `has-text-align-${ align }` ]: align,
} ),
style: { direction },
} );
return (
<>
<BlockControls group="block">
<AlignmentControl
value={ align }
onChange={ ( newAlign ) =>
setAttributes( { align: newAlign } )
}
/>
<ParagraphRTLControl
direction={ direction }
setDirection={ ( newDirection ) =>
setAttributes( { direction: newDirection } )
}
/>
</BlockControls>
{ isDropCapFeatureEnabled && (
<InspectorControls __experimentalGroup="typography">
<ToolsPanelItem
hasValue={ () => !! dropCap }
label={ __( 'Drop cap' ) }
onDeselect={ () =>
setAttributes( { dropCap: undefined } )
}
resetAllFilter={ () => ( { dropCap: undefined } ) }
panelId={ clientId }
>
<ToggleControl
label={ __( 'Drop cap' ) }
checked={ !! dropCap }
onChange={ () =>
setAttributes( { dropCap: ! dropCap } )
}
help={
dropCap
? __( 'Showing large initial letter.' )
: __(
'Toggle to show a large initial letter.'
)
}
/>
</ToolsPanelItem>
</InspectorControls>
) }
<RichText
identifier="content"
tagName="p"
{ ...blockProps }
value={ content }
onChange={ ( newContent ) =>
setAttributes( { content: newContent } )
}
onSplit={ ( value, isOriginal ) => {
let newAttributes;
if ( isOriginal || value ) {
newAttributes = {
...attributes,
content: value,
};
}
const block = createBlock( name, newAttributes );
if ( isOriginal ) {
block.clientId = clientId;
}
return block;
} }
onMerge={ mergeBlocks }
onReplace={ onReplace }
onRemove={ onRemove }
aria-label={
content
? __( 'Paragraph block' )
: __(
'Empty block; start writing or type forward slash to choose a block'
)
}
data-empty={ content ? false : true }
placeholder={ placeholder || __( 'Type / to choose a block' ) }
data-custom-placeholder={ placeholder ? true : undefined }
__unstableEmbedURLOnPaste
__unstableAllowPrefixTransformations
/>
</>
);
}
export default ParagraphBlock;