From 4e2788394031843d51aada9cf4a3e26e5bcce7bc Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Tue, 17 Jan 2023 17:09:22 +0900 Subject: [PATCH 1/2] Post Author Block: Show default avatar placeholder and size control --- .../block-library/src/post-author/edit.js | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/packages/block-library/src/post-author/edit.js b/packages/block-library/src/post-author/edit.js index 9573eedde66cf..6815eb108005b 100644 --- a/packages/block-library/src/post-author/edit.js +++ b/packages/block-library/src/post-author/edit.js @@ -22,6 +22,7 @@ import { import { useSelect, useDispatch } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; import { store as coreStore } from '@wordpress/core-data'; +import { addQueryArgs } from '@wordpress/url'; const minimumUsersForCombobox = 25; @@ -30,6 +31,14 @@ const AUTHORS_QUERY = { per_page: 100, }; +const DEFAULT_AVATAR_SIZES = [ 24, 48, 96 ].map( ( size ) => ( { + value: size, + label: `${ size } x ${ size }`, +} ) ); + +const DEFAULT_AVATAR_URL = + 'https://www.gravatar.com/avatar/00000000000000000000000000000000?d=mp&f=y'; + function PostAuthorEdit( { isSelected, context: { postType, postId, queryId }, @@ -58,18 +67,27 @@ function PostAuthorEdit( { const { editEntityRecord } = useDispatch( coreStore ); - const { textAlign, showAvatar, showBio, byline, isLink, linkTarget } = - attributes; - const avatarSizes = []; - const authorName = authorDetails?.name || __( 'Post Author' ); - if ( authorDetails?.avatar_urls ) { - Object.keys( authorDetails.avatar_urls ).forEach( ( size ) => { - avatarSizes.push( { + const { + textAlign, + avatarSize, + showAvatar, + showBio, + byline, + isLink, + linkTarget, + } = attributes; + const avatarSizes = authorDetails?.avatar_urls + ? Object.keys( authorDetails.avatar_urls ).map( ( size ) => ( { value: size, label: `${ size } x ${ size }`, - } ); - } ); - } + } ) ) + : DEFAULT_AVATAR_SIZES; + const authorName = authorDetails?.name || __( 'Post Author' ); + const avatarUrl = authorDetails?.avatar_urls + ? authorDetails.avatar_urls[ avatarSize ] + : addQueryArgs( DEFAULT_AVATAR_URL, { + s: avatarSize, + } ); const blockProps = useBlockProps( { className: classnames( { @@ -130,7 +148,7 @@ function PostAuthorEdit( { { setAttributes( { @@ -175,16 +193,12 @@ function PostAuthorEdit( {
- { showAvatar && authorDetails?.avatar_urls && ( + { showAvatar && (
{
) } From 4b5988ed05788838d86f169c22bc6392f4f74aef Mon Sep 17 00:00:00 2001 From: Tetsuaki Hamano Date: Fri, 20 Jan 2023 23:51:04 +0900 Subject: [PATCH 2/2] Refactoring --- packages/block-library/src/post-author/edit.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/post-author/edit.js b/packages/block-library/src/post-author/edit.js index 6815eb108005b..5b94bb418004a 100644 --- a/packages/block-library/src/post-author/edit.js +++ b/packages/block-library/src/post-author/edit.js @@ -12,6 +12,7 @@ import { InspectorControls, RichText, useBlockProps, + store as blockEditorStore, } from '@wordpress/block-editor'; import { ComboboxControl, @@ -22,7 +23,6 @@ import { import { useSelect, useDispatch } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; import { store as coreStore } from '@wordpress/core-data'; -import { addQueryArgs } from '@wordpress/url'; const minimumUsersForCombobox = 25; @@ -36,9 +36,6 @@ const DEFAULT_AVATAR_SIZES = [ 24, 48, 96 ].map( ( size ) => ( { label: `${ size } x ${ size }`, } ) ); -const DEFAULT_AVATAR_URL = - 'https://www.gravatar.com/avatar/00000000000000000000000000000000?d=mp&f=y'; - function PostAuthorEdit( { isSelected, context: { postType, postId, queryId }, @@ -46,7 +43,7 @@ function PostAuthorEdit( { setAttributes, } ) { const isDescendentOfQueryLoop = Number.isFinite( queryId ); - const { authorId, authorDetails, authors } = useSelect( + const { authorId, authorDetails, authors, defaultAutorUrl } = useSelect( ( select ) => { const { getEditedEntityRecord, getUser, getUsers } = select( coreStore ); @@ -55,11 +52,15 @@ function PostAuthorEdit( { postType, postId )?.author; + const { getSettings } = select( blockEditorStore ); + const { avatarURL } = + getSettings().__experimentalDiscussionSettings; return { authorId: _authorId, authorDetails: _authorId ? getUser( _authorId ) : null, authors: getUsers( AUTHORS_QUERY ), + defaultAutorUrl: avatarURL, }; }, [ postType, postId ] @@ -85,9 +86,7 @@ function PostAuthorEdit( { const authorName = authorDetails?.name || __( 'Post Author' ); const avatarUrl = authorDetails?.avatar_urls ? authorDetails.avatar_urls[ avatarSize ] - : addQueryArgs( DEFAULT_AVATAR_URL, { - s: avatarSize, - } ); + : defaultAutorUrl; const blockProps = useBlockProps( { className: classnames( {