UNPKG

@wordpress/block-library

Version:
1 lines • 5.39 kB
{"version":3,"file":"edit.mjs","names":["coreStore","ToolsPanel","ToolsPanelItem"],"sources":["../../src/comment-author-name/edit.jsx"],"sourcesContent":["import { __, _x } from '@wordpress/i18n';\nimport { useSelect } from '@wordpress/data';\nimport { InspectorControls, useBlockProps } from '@wordpress/block-editor';\nimport { store as coreStore } from '@wordpress/core-data';\nimport {\n\tToggleControl,\n\t__experimentalToolsPanel as ToolsPanel,\n\t__experimentalToolsPanelItem as ToolsPanelItem,\n} from '@wordpress/components';\nimport { useToolsPanelDropdownMenuProps } from '../utils/hooks';\nimport useDeprecatedTextAlign from '../utils/deprecated-text-align-attributes';\n\n/**\n * Renders the `core/comment-author-name` block on the editor.\n *\n * @param {Object} props React props.\n * @param {Object} props.setAttributes Callback for updating block attributes.\n * @param {Object} props.attributes Block attributes.\n * @param {string} props.attributes.isLink Whether the author name should be linked.\n * @param {string} props.attributes.linkTarget Target of the link.\n * @param {Object} props.context Inherited context.\n * @param {string} props.context.commentId The comment ID.\n *\n * @return {React.JSX.Element} React element.\n */\nexport default function Edit( props ) {\n\tconst {\n\t\tattributes: { isLink, linkTarget },\n\t\tcontext: { commentId },\n\t\tsetAttributes,\n\t} = props;\n\tuseDeprecatedTextAlign( props );\n\tconst dropdownMenuProps = useToolsPanelDropdownMenuProps();\n\tconst blockProps = useBlockProps();\n\tlet displayName = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEntityRecord } = select( coreStore );\n\n\t\t\tconst comment = getEntityRecord( 'root', 'comment', commentId );\n\t\t\tconst authorName = comment?.author_name;\n\n\t\t\tif ( comment && ! authorName ) {\n\t\t\t\tconst user = getEntityRecord( 'root', 'user', comment.author );\n\t\t\t\treturn user?.name ?? __( 'Anonymous' );\n\t\t\t}\n\t\t\treturn authorName ?? '';\n\t\t},\n\t\t[ commentId ]\n\t);\n\n\tconst inspectorControls = (\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\tisLink: true,\n\t\t\t\t\t\tlinkTarget: '_self',\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<ToolsPanelItem\n\t\t\t\t\tlabel={ __( 'Link to authors URL' ) }\n\t\t\t\t\tisShownByDefault\n\t\t\t\t\thasValue={ () => ! isLink }\n\t\t\t\t\tonDeselect={ () =>\n\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\tisLink: true,\n\t\t\t\t\t\t} )\n\t\t\t\t\t}\n\t\t\t\t>\n\t\t\t\t\t<ToggleControl\n\t\t\t\t\t\tlabel={ __( 'Link to authors URL' ) }\n\t\t\t\t\t\tonChange={ () => setAttributes( { isLink: ! isLink } ) }\n\t\t\t\t\t\tchecked={ isLink }\n\t\t\t\t\t/>\n\t\t\t\t</ToolsPanelItem>\n\t\t\t\t{ 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={ () => linkTarget !== '_self' }\n\t\t\t\t\t\tonDeselect={ () =>\n\t\t\t\t\t\t\tsetAttributes( {\n\t\t\t\t\t\t\t\tlinkTarget: '_self',\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\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={ 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</ToolsPanel>\n\t\t</InspectorControls>\n\t);\n\n\tif ( ! commentId || ! displayName ) {\n\t\tdisplayName = _x( 'Comment Author', 'block title' );\n\t}\n\n\tconst displayAuthor = isLink ? (\n\t\t<a\n\t\t\thref=\"#comment-author-pseudo-link\"\n\t\t\tonClick={ ( event ) => event.preventDefault() }\n\t\t>\n\t\t\t{ displayName }\n\t\t</a>\n\t) : (\n\t\tdisplayName\n\t);\n\treturn (\n\t\t<>\n\t\t\t{ inspectorControls }\n\t\t\t<div { ...blockProps }>{ displayAuthor }</div>\n\t\t</>\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAwB,KAAM,OAAQ;CACrC,MAAM,EACL,YAAY,EAAE,QAAQ,cACtB,SAAS,EAAE,aACX,kBACG;CACJ,uBAAwB,KAAM;CAC9B,MAAM,oBAAoB,+BAA+B;CACzD,MAAM,aAAa,cAAc;CACjC,IAAI,cAAc,WACf,WAAY;EACb,MAAM,EAAE,oBAAoB,OAAQA,KAAU;EAE9C,MAAM,UAAU,gBAAiB,QAAQ,WAAW,SAAU;EAC9D,MAAM,aAAa,SAAS;EAE5B,IAAK,WAAW,CAAE,YAEjB,OADa,gBAAiB,QAAQ,QAAQ,QAAQ,MAC5C,CAAC,EAAE,QAAQ,GAAI,WAAY;EAEtC,OAAO,cAAc;CACtB,GACA,CAAE,SAAU,CACb;CAEA,MAAM,oBACL,oBAAC,mBAAD,EAAA,UACC,qBAACC,0BAAD;EACC,OAAQ,GAAI,UAAW;EACvB,gBAAiB;GAChB,cAAe;IACd,QAAQ;IACR,YAAY;GACb,CAAE;EACH;EACoB;EARrB,UAAA,CAUC,oBAACC,8BAAD;GACC,OAAQ,GAAI,qBAAsB;GAClC,kBAAA;GACA,gBAAiB,CAAE;GACnB,kBACC,cAAe,EACd,QAAQ,KACT,CAAE;GAGH,UAAA,oBAAC,eAAD;IACC,OAAQ,GAAI,qBAAsB;IAClC,gBAAiB,c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