import clsx from 'clsx'; import type { ReactNode } from 'react'; import { InspectorControls, useBlockProps, __experimentalUseBorderProps as useBorderProps, __experimentalGetShadowClassesAndStyles as getShadowClassesAndStyles, } from '@wordpress/block-editor'; import type { BlockEditProps } from '@wordpress/blocks'; import { RangeControl, ResizableBox, ToggleControl, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components'; import { store as coreStore } from '@wordpress/core-data'; import { useSelect } from '@wordpress/data'; import { createInterpolateElement } from '@wordpress/element'; import { __, isRTL } from '@wordpress/i18n'; import { Link, Text } from '@wordpress/ui'; import { addQueryArgs, removeQueryArgs } from '@wordpress/url'; import { useToolsPanelDropdownMenuProps } from '../utils/hooks'; import { useCommentAvatar, useUserAvatar } from './hooks'; import UserControl from './user-control'; import type { AvatarAttributes, AvatarBlockContext, AvatarData } from './types'; /** * Returns the help text explaining where avatars come from, with a link to the * Discussion settings where the default avatar is chosen. Only users who can * manage the site's settings get it, since nobody else can act on it. * * @return Help text, or `null` when the user can't manage settings. */ function useAvatarHelpText() { const canManageSettings = useSelect( ( select ) => select( coreStore ).canUser( 'update', { kind: 'root', name: 'site', } ), [] ); if ( ! canManageSettings ) { return null; } return createInterpolateElement( __( 'Avatars use the Gravatar service. Go to Discussion settings to change the default avatar.' ), { a: , } ); } /** * Renders the inspector controls for the `core/avatar` block. * * @param props React props. * @param props.setAttributes Callback for updating block attributes. * @param props.avatar Avatar data returned by `useCommentAvatar` or `useUserAvatar`, with the `minSize` and `maxSize` bounds of the size control. * @param props.attributes Block attributes: `size`, `isLink`, `linkTarget` and `userId`. * @param props.selectUser Whether to render the user selection control. * * @return React element. */ const AvatarInspectorControls = ( { setAttributes, avatar, attributes, selectUser, }: { setAttributes: ( attrs: Partial< AvatarAttributes > ) => void; avatar: AvatarData; attributes: AvatarAttributes; selectUser: boolean; } ) => { const dropdownMenuProps = useToolsPanelDropdownMenuProps(); const helpText = useAvatarHelpText(); return ( { setAttributes( { size: 96, isLink: false, linkTarget: '_self', userId: undefined, } ); } } dropdownMenuProps={ dropdownMenuProps } > { helpText && ( { helpText } ) } attributes?.size !== 96 } onDeselect={ () => setAttributes( { size: 96 } ) } > setAttributes( { size: newSize, } ) } // @ts-expect-error Bug: `minSize` is an avatar URL key, so it is a string. min={ avatar.minSize } max={ avatar.maxSize } initialPosition={ attributes?.size } value={ attributes?.size } /> attributes?.isLink } onDeselect={ () => setAttributes( { isLink: false } ) } > setAttributes( { isLink: ! attributes.isLink } ) } checked={ attributes.isLink } /> { attributes.isLink && ( attributes?.linkTarget !== '_self' } onDeselect={ () => setAttributes( { linkTarget: '_self' } ) } > setAttributes( { linkTarget: value ? '_blank' : '_self', } ) } checked={ attributes.linkTarget === '_blank' } /> ) } { selectUser && ( !! attributes?.userId } onDeselect={ () => setAttributes( { userId: undefined } ) } > { setAttributes( { userId: value, } ); } } /> ) } ); }; /** * Wraps the avatar image in a placeholder link when the block links to the user * profile. The link is inert in the editor: it points at a fragment and its * click handler prevents navigation. * * @param props React props. * @param props.children Avatar image to wrap. * @param props.isLink Whether to wrap the children in a link. * * @return The children, wrapped in a link when `isLink` is true. */ const AvatarLinkWrapper = ( { children, isLink, }: { children: ReactNode; isLink: boolean; } ) => isLink ? ( event.preventDefault() } > { children } ) : ( children ); /** * Renders the avatar image inside a `ResizableBox` so its size can be adjusted * by dragging. Resizing keeps the aspect ratio and updates the `size` attribute. * * @param props React props. * @param props.setAttributes Callback for updating block attributes. * @param props.attributes Block attributes: `size` and `isLink`, plus the border and shadow support values applied to the image. * @param props.avatar Avatar data returned by `useCommentAvatar` or `useUserAvatar`, with the image `src` and `alt` and the `minSize` and `maxSize` resize bounds. * @param props.blockProps Props returned by `useBlockProps`, applied to the wrapper element. * @param props.isSelected Whether the block is selected. Resize handles are only shown when it is. * * @return React element. */ const ResizableAvatar = ( { setAttributes, attributes, avatar, blockProps, isSelected, }: { setAttributes: ( attrs: Partial< AvatarAttributes > ) => void; attributes: AvatarAttributes; avatar: AvatarData; blockProps: Record< string, unknown >; isSelected: boolean; } ) => { const borderProps = useBorderProps( attributes ); const shadowProps = getShadowClassesAndStyles( attributes ); const doubledSizedSrc = addQueryArgs( // @ts-expect-error `removeQueryArgs` takes the keys as rest arguments; the array works only because `delete` stringifies it. removeQueryArgs( avatar?.src, [ 's' ] ), { s: attributes?.size * 2, } ); return (
{ setAttributes( { size: parseInt( String( attributes.size + ( delta.height || delta.width ) ), 10 ), } ); } } lockAspectRatio enable={ { top: false, right: ! isRTL(), bottom: true, left: isRTL(), } } minWidth={ avatar.minSize } maxWidth={ avatar.maxSize } > {
); }; /** * Renders the block for a comment author, resolving the avatar from the * `commentId` context. The user selection control is not offered here because * the author is determined by the comment. * * @param props React props. * @param props.attributes Block attributes. * @param props.context Inherited context, from which the comment ID is read. * @param props.setAttributes Callback for updating block attributes. * @param props.isSelected Whether the block is selected. * * @return React element. */ const CommentEdit = ( { attributes, context, setAttributes, isSelected, }: BlockEditProps< AvatarAttributes > & { context: AvatarBlockContext } ) => { const { commentId } = context; const blockProps = useBlockProps(); const avatar = useCommentAvatar( { commentId } ); return ( <> ); }; /** * Renders the block for a user, resolving the avatar from the `userId` * attribute when set, and otherwise from the author of the post identified by * the `postType` and `postId` context. * * @param props React props. * @param props.attributes Block attributes, from which the `userId` is read. * @param props.context Inherited context, from which the post type and post ID are read. * @param props.setAttributes Callback for updating block attributes. * @param props.isSelected Whether the block is selected. * * @return React element. */ const UserEdit = ( { attributes, context, setAttributes, isSelected, }: BlockEditProps< AvatarAttributes > & { context: AvatarBlockContext } ) => { const { postId, postType } = context; const avatar = useUserAvatar( { userId: attributes?.userId, postId, postType, } ); const blockProps = useBlockProps(); return ( <> ); }; /** * Renders the `core/avatar` block on the editor. All props are forwarded to * either `CommentEdit` or `UserEdit`, depending on whether the block is used * inside a comment. * * @param props React props. * @param props.attributes Block attributes. * @param props.context Inherited context. * @param props.context.commentId The comment ID when the block is used inside a comment. `null` in the Site Editor, where no comment is resolved but the comment variant is still rendered. * @param props.context.postType The post type of the current post. * @param props.context.postId The ID of the current post. * @param props.setAttributes Callback for updating block attributes. * @param props.isSelected Whether the block is selected. * * @return React element. */ export default function Edit( props: BlockEditProps< AvatarAttributes > & { context: AvatarBlockContext } ) { // Don't show the Comment Edit controls if we have a comment ID set, or if we're in the Site Editor (where it is `null`). if ( props?.context?.commentId || props?.context?.commentId === null ) { return ; } return ; }