@wordpress/block-library
Version:
Block library for the WordPress editor.
67 lines (59 loc) • 1.37 kB
JavaScript
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import {
AlignmentControl,
BlockControls,
useBlockProps,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
function PostAuthorBiographyEdit( {
context: { postType, postId },
attributes: { textAlign },
setAttributes,
} ) {
const { authorDetails } = useSelect(
( select ) => {
const { getEditedEntityRecord, getUser } = select( coreStore );
const _authorId = getEditedEntityRecord(
'postType',
postType,
postId
)?.author;
return {
authorDetails: _authorId ? getUser( _authorId ) : null,
};
},
[ postType, postId ]
);
const blockProps = useBlockProps( {
className: clsx( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );
const displayAuthorBiography =
authorDetails?.description || __( 'Author Biography' );
return (
<>
<BlockControls group="block">
<AlignmentControl
value={ textAlign }
onChange={ ( nextAlign ) => {
setAttributes( { textAlign: nextAlign } );
} }
/>
</BlockControls>
<div
{ ...blockProps }
dangerouslySetInnerHTML={ { __html: displayAuthorBiography } }
/>
</>
);
}
export default PostAuthorBiographyEdit;