{"version":3,"file":"edit.mjs","names":["coreStore","ToolsPanel","ToolsPanelItem","useBorderProps","getShadowClassesAndStyles"],"sources":["../../src/avatar/edit.jsx"],"sourcesContent":["import clsx from 'clsx';\nimport {\n\tInspectorControls,\n\tuseBlockProps,\n\t__experimentalUseBorderProps as useBorderProps,\n\t__experimentalGetShadowClassesAndStyles as getShadowClassesAndStyles,\n} from '@wordpress/block-editor';\nimport {\n\tRangeControl,\n\tResizableBox,\n\tToggleControl,\n\t__experimentalToolsPanel as ToolsPanel,\n\t__experimentalToolsPanelItem as ToolsPanelItem,\n} from '@wordpress/components';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useSelect } from '@wordpress/data';\nimport { createInterpolateElement } from '@wordpress/element';\nimport { __, isRTL } from '@wordpress/i18n';\nimport { Link, Text } from '@wordpress/ui';\nimport { addQueryArgs, removeQueryArgs } from '@wordpress/url';\nimport { useToolsPanelDropdownMenuProps } from '../utils/hooks';\nimport { useCommentAvatar, useUserAvatar } from './hooks';\nimport UserControl from './user-control';\n\n/**\n * Returns the help text explaining where avatars come from, with a link to the\n * Discussion settings where the default avatar is chosen. Only users who can\n * manage the site's settings get it, since nobody else can act on it.\n *\n * @return {React.ReactNode|null} Help text, or `null` when the user can't manage settings.\n */\nfunction useAvatarHelpText() {\n\tconst canManageSettings = useSelect(\n\t\t( select ) =>\n\t\t\tselect( coreStore ).canUser( 'update', {\n\t\t\t\tkind: 'root',\n\t\t\t\tname: 'site',\n\t\t\t} ),\n\t\t[]\n\t);\n\n\tif ( ! canManageSettings ) {\n\t\treturn null;\n\t}\n\n\treturn createInterpolateElement(\n\t\t__(\n\t\t\t'Avatars use the Gravatar service. Go to <a>Discussion settings</a> to change the default avatar.'\n\t\t),\n\t\t{\n\t\t\ta: <Link href=\"options-discussion.php\" openInNewTab />,\n\t\t}\n\t);\n}\n\n/**\n * Renders the inspector controls for the `core/avatar` block.\n *\n * @param {Object}   props               React props.\n * @param {Function} props.setAttributes Callback for updating block attributes.\n * @param {Object}   props.avatar        Avatar data returned by `useCommentAvatar` or `useUserAvatar`, with the `minSize` and `maxSize` bounds of the size control.\n * @param {Object}   props.attributes    Block attributes: `size`, `isLink`, `linkTarget` and `userId`.\n * @param {boolean}  props.selectUser    Whether to render the user selection control.\n *\n * @return {React.JSX.Element} React element.\n */\nconst AvatarInspectorControls = ( {\n\tsetAttributes,\n\tavatar,\n\tattributes,\n\tselectUser,\n} ) => {\n\tconst dropdownMenuProps = useToolsPanelDropdownMenuProps();\n\tconst helpText = useAvatarHelpText();\n\treturn (\n\t\t<InspectorControls>\n\t\t\t<ToolsPanel\n\t\t\t\tlabel={ __( 'Settings' ) }\n\t\t\t\tresetAll={ () => {\n\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\tsize: 96,\n\t\t\t\t\t\tisLink: false,\n\t\t\t\t\t\tlinkTarget: '_self',\n\t\t\t\t\t\tuserId: undefined,\n\t\t\t\t\t} );\n\t\t\t\t} }\n\t\t\t\tdropdownMenuProps={ dropdownMenuProps }\n\t\t\t>\n\t\t\t\t{ helpText && (\n\t\t\t\t\t<Text className=\"wp-block-avatar__help-text\">\n\t\t\t\t\t\t{ helpText }\n\t\t\t\t\t</Text>\n\t\t\t\t) }\n\t\t\t\t<ToolsPanelItem\n\t\t\t\t\tlabel={ __( 'Image size' ) }\n\t\t\t\t\tisShownByDefault\n\t\t\t\t\thasValue={ () => attributes?.size !== 96 }\n\t\t\t\t\tonDeselect={ () => setAttributes( { size: 96 } ) }\n\t\t\t\t>\n\t\t\t\t\t<RangeControl\n\t\t\t\t\t\tlabel={ __( 'Image size' ) }\n\t\t\t\t\t\tonChange={ ( newSize ) =>\n\t\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\t\tsize: newSize,\n\t\t\t\t\t\t\t} )\n\t\t\t\t\t\t}\n\t\t\t\t\t\tmin={ avatar.minSize }\n\t\t\t\t\t\tmax={ avatar.maxSize }\n\t\t\t\t\t\tinitialPosition={ attributes?.size }\n\t\t\t\t\t\tvalue={ attributes?.size }\n\t\t\t\t\t/>\n\t\t\t\t</ToolsPanelItem>\n\t\t\t\t<ToolsPanelItem\n\t\t\t\t\tlabel={ __( 'Link to user profile' ) }\n\t\t\t\t\tisShownByDefault\n\t\t\t\t\thasValue={ () => attributes?.isLink }\n\t\t\t\t\tonDeselect={ () => setAttributes( { isLink: false } ) }\n\t\t\t\t>\n\t\t\t\t\t<ToggleControl\n\t\t\t\t\t\tlabel={ __( 'Link to user profile' ) }\n\t\t\t\t\t\tonChange={ () =>\n\t\t\t\t\t\t\tsetAttributes( { isLink: ! attributes.isLink } )\n\t\t\t\t\t\t}\n\t\t\t\t\t\tchecked={ attributes.isLink }\n\t\t\t\t\t/>\n\t\t\t\t</ToolsPanelItem>\n\t\t\t\t{ attributes.isLink && (\n\t\t\t\t\t<ToolsPanelItem\n\t\t\t\t\t\tlabel={ __( 'Open in new tab' ) }\n\t\t\t\t\t\tisShownByDefault\n\t\t\t\t\t\thasValue={ () => attributes?.linkTarget !== '_self' }\n\t\t\t\t\t\tonDeselect={ () =>\n\t\t\t\t\t\t\tsetAttributes( { linkTarget: '_self' } )\n\t\t\t\t\t\t}\n\t\t\t\t\t>\n\t\t\t\t\t\t<ToggleControl\n\t\t\t\t\t\t\tlabel={ __( 'Open in new tab' ) }\n\t\t\t\t\t\t\tonChange={ ( value ) =>\n\t\t\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\t\t\tlinkTarget: value ? '_blank' : '_self',\n\t\t\t\t\t\t\t\t} )\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tchecked={ attributes.linkTarget === '_blank' }\n\t\t\t\t\t\t/>\n\t\t\t\t\t</ToolsPanelItem>\n\t\t\t\t) }\n\t\t\t\t{ selectUser && (\n\t\t\t\t\t<ToolsPanelItem\n\t\t\t\t\t\tlabel={ __( 'User' ) }\n\t\t\t\t\t\tisShownByDefault\n\t\t\t\t\t\thasValue={ () => !! attributes?.userId }\n\t\t\t\t\t\tonDeselect={ () =>\n\t\t\t\t\t\t\tsetAttributes( { userId: undefined } )\n\t\t\t\t\t\t}\n\t\t\t\t\t>\n\t\t\t\t\t\t<UserControl\n\t\t\t\t\t\t\tvalue={ attributes?.userId }\n\t\t\t\t\t\t\tonChange={ ( value ) => {\n\t\t\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\t\t\tuserId: value,\n\t\t\t\t\t\t\t\t} );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t/>\n\t\t\t\t\t</ToolsPanelItem>\n\t\t\t\t) }\n\t\t\t</ToolsPanel>\n\t\t</InspectorControls>\n\t);\n};\n\n/**\n * Wraps the avatar image in a placeholder link when the block links to the user\n * profile. The link is inert in the editor: it points at a fragment and its\n * click handler prevents navigation.\n *\n * @param {Object}          props          React props.\n * @param {React.ReactNode} props.children Avatar image to wrap.\n * @param {boolean}         props.isLink   Whether to wrap the children in a link.\n *\n * @return {React.ReactNode} The children, wrapped in a link when `isLink` is true.\n */\nconst AvatarLinkWrapper = ( { children, isLink } ) =>\n\tisLink ? (\n\t\t<a\n\t\t\thref=\"#avatar-pseudo-link\"\n\t\t\tclassName=\"wp-block-avatar__link\"\n\t\t\tonClick={ ( event ) => event.preventDefault() }\n\t\t>\n\t\t\t{ children }\n\t\t</a>\n\t) : (\n\t\tchildren\n\t);\n\n/**\n * Renders the avatar image inside a `ResizableBox` so its size can be adjusted\n * by dragging. Resizing keeps the aspect ratio and updates the `size` attribute.\n *\n * @param {Object}   props               React props.\n * @param {Function} props.setAttributes Callback for updating block attributes.\n * @param {Object}   props.attributes    Block attributes: `size` and `isLink`, plus the border and shadow support values applied to the image.\n * @param {Object}   props.avatar        Avatar data returned by `useCommentAvatar` or `useUserAvatar`, with the image `src` and `alt` and the `minSize` and `maxSize` resize bounds.\n * @param {Object}   props.blockProps    Props returned by `useBlockProps`, applied to the wrapper element.\n * @param {boolean}  props.isSelected    Whether the block is selected. Resize handles are only shown when it is.\n *\n * @return {React.JSX.Element} React element.\n */\nconst ResizableAvatar = ( {\n\tsetAttributes,\n\tattributes,\n\tavatar,\n\tblockProps,\n\tisSelected,\n} ) => {\n\tconst borderProps = useBorderProps( attributes );\n\tconst shadowProps = getShadowClassesAndStyles( attributes );\n\tconst doubledSizedSrc = addQueryArgs(\n\t\tremoveQueryArgs( avatar?.src, [ 's' ] ),\n\t\t{\n\t\t\ts: attributes?.size * 2,\n\t\t}\n\t);\n\treturn (\n\t\t<div { ...blockProps }>\n\t\t\t<AvatarLinkWrapper isLink={ attributes.isLink }>\n\t\t\t\t<ResizableBox\n\t\t\t\t\tsize={ {\n\t\t\t\t\t\twidth: attributes.size,\n\t\t\t\t\t\theight: attributes.size,\n\t\t\t\t\t} }\n\t\t\t\t\tshowHandle={ isSelected }\n\t\t\t\t\tonResizeStop={ ( event, direction, elt, delta ) => {\n\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\tsize: parseInt(\n\t\t\t\t\t\t\t\tattributes.size +\n\t\t\t\t\t\t\t\t\t( delta.height || delta.width ),\n\t\t\t\t\t\t\t\t10\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t} );\n\t\t\t\t\t} }\n\t\t\t\t\tlockAspectRatio\n\t\t\t\t\tenable={ {\n\t\t\t\t\t\ttop: false,\n\t\t\t\t\t\tright: ! isRTL(),\n\t\t\t\t\t\tbottom: true,\n\t\t\t\t\t\tleft: isRTL(),\n\t\t\t\t\t} }\n\t\t\t\t\tminWidth={ avatar.minSize }\n\t\t\t\t\tmaxWidth={ avatar.maxSize }\n\t\t\t\t>\n\t\t\t\t\t<img\n\t\t\t\t\t\tsrc={ doubledSizedSrc }\n\t\t\t\t\t\talt={ avatar.alt }\n\t\t\t\t\t\tclassName={ clsx(\n\t\t\t\t\t\t\t'avatar',\n\t\t\t\t\t\t\t'avatar-' + attributes.size,\n\t\t\t\t\t\t\t'photo',\n\t\t\t\t\t\t\t'wp-block-avatar__image',\n\t\t\t\t\t\t\tborderProps.className\n\t\t\t\t\t\t) }\n\t\t\t\t\t\tstyle={ {\n\t\t\t\t\t\t\t...borderProps.style,\n\t\t\t\t\t\t\t...shadowProps.style,\n\t\t\t\t\t\t} }\n\t\t\t\t\t/>\n\t\t\t\t</ResizableBox>\n\t\t\t</AvatarLinkWrapper>\n\t\t</div>\n\t);\n};\n\n/**\n * Renders the block for a comment author, resolving the avatar from the\n * `commentId` context. The user selection control is not offered here because\n * the author is determined by the comment.\n *\n * @param {Object}   props               React props.\n * @param {Object}   props.attributes    Block attributes.\n * @param {Object}   props.context       Inherited context, from which the comment ID is read.\n * @param {Function} props.setAttributes Callback for updating block attributes.\n * @param {boolean}  props.isSelected    Whether the block is selected.\n *\n * @return {React.JSX.Element} React element.\n */\nconst CommentEdit = ( { attributes, context, setAttributes, isSelected } ) => {\n\tconst { commentId } = context;\n\tconst blockProps = useBlockProps();\n\tconst avatar = useCommentAvatar( { commentId } );\n\treturn (\n\t\t<>\n\t\t\t<AvatarInspectorControls\n\t\t\t\tavatar={ avatar }\n\t\t\t\tsetAttributes={ setAttributes }\n\t\t\t\tattributes={ attributes }\n\t\t\t\tselectUser={ false }\n\t\t\t/>\n\t\t\t<ResizableAvatar\n\t\t\t\tattributes={ attributes }\n\t\t\t\tavatar={ avatar }\n\t\t\t\tblockProps={ blockProps }\n\t\t\t\tisSelected={ isSelected }\n\t\t\t\tsetAttributes={ setAttributes }\n\t\t\t/>\n\t\t</>\n\t);\n};\n\n/**\n * Renders the block for a user, resolving the avatar from the `userId`\n * attribute when set, and otherwise from the author of the post identified by\n * the `postType` and `postId` context.\n *\n * @param {Object}   props               React props.\n * @param {Object}   props.attributes    Block attributes, from which the `userId` is read.\n * @param {Object}   props.context       Inherited context, from which the post type and post ID are read.\n * @param {Function} props.setAttributes Callback for updating block attributes.\n * @param {boolean}  props.isSelected    Whether the block is selected.\n *\n * @return {React.JSX.Element} React element.\n */\nconst UserEdit = ( { attributes, context, setAttributes, isSelected } ) => {\n\tconst { postId, postType } = context;\n\tconst avatar = useUserAvatar( {\n\t\tuserId: attributes?.userId,\n\t\tpostId,\n\t\tpostType,\n\t} );\n\tconst blockProps = useBlockProps();\n\treturn (\n\t\t<>\n\t\t\t<AvatarInspectorControls\n\t\t\t\tselectUser\n\t\t\t\tattributes={ attributes }\n\t\t\t\tavatar={ avatar }\n\t\t\t\tsetAttributes={ setAttributes }\n\t\t\t/>\n\n\t\t\t<ResizableAvatar\n\t\t\t\tattributes={ attributes }\n\t\t\t\tavatar={ avatar }\n\t\t\t\tblockProps={ blockProps }\n\t\t\t\tisSelected={ isSelected }\n\t\t\t\tsetAttributes={ setAttributes }\n\t\t\t/>\n\t\t</>\n\t);\n};\n\n/**\n * Renders the `core/avatar` block on the editor. All props are forwarded to\n * either `CommentEdit` or `UserEdit`, depending on whether the block is used\n * inside a comment.\n *\n * @param {Object}      props                     React props.\n * @param {Object}      props.attributes          Block attributes.\n * @param {Object}      props.context             Inherited context.\n * @param {number|null} [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.\n * @param {string}      [props.context.postType]  The post type of the current post.\n * @param {number}      [props.context.postId]    The ID of the current post.\n * @param {Function}    props.setAttributes       Callback for updating block attributes.\n * @param {boolean}     props.isSelected          Whether the block is selected.\n *\n * @return {React.JSX.Element} React element.\n */\nexport default function Edit( props ) {\n\t// 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`).\n\tif ( props?.context?.commentId || props?.context?.commentId === null ) {\n\t\treturn <CommentEdit { ...props } />;\n\t}\n\treturn <UserEdit { ...props } />;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA+BA,SAAS,oBAAoB;CAU5B,IAAK,CATqB,WACvB,WACD,OAAQA,KAAU,CAAC,CAAC,QAAS,UAAU;EACtC,MAAM;EACN,MAAM;CACP,CAAE,GACH,CAAC,CAGqB,GACtB,OAAO;CAGR,OAAO,yBACN,GACC,kGACD,GACA,EACC,GAAG,oBAAC,MAAD;EAAM,MAAK;EAAyB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