UNPKG

@wordpress/block-library

Version:
8 lines (7 loc) 5.49 kB
{ "version": 3, "sources": ["../../src/comment-date/edit.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useEntityProp } from '@wordpress/core-data';\nimport {\n\tdateI18n,\n\thumanTimeDiff,\n\tgetSettings as getDateSettings,\n} from '@wordpress/date';\nimport {\n\tInspectorControls,\n\tuseBlockProps,\n\t__experimentalDateFormatPicker as DateFormatPicker,\n} from '@wordpress/block-editor';\nimport {\n\t__experimentalToolsPanel as ToolsPanel,\n\t__experimentalToolsPanelItem as ToolsPanelItem,\n\tToggleControl,\n} from '@wordpress/components';\nimport { __, _x } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport { useToolsPanelDropdownMenuProps } from '../utils/hooks';\n\n/**\n * Renders the `core/comment-date` 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.format Format of the date.\n * @param {string} props.attributes.isLink Whether the author name should be linked.\n * @param {Object} props.context Inherited context.\n * @param {string} props.context.commentId The comment ID.\n *\n * @return {JSX.Element} React element.\n */\nexport default function Edit( {\n\tattributes: { format, isLink },\n\tcontext: { commentId },\n\tsetAttributes,\n} ) {\n\tconst blockProps = useBlockProps();\n\tconst dropdownMenuProps = useToolsPanelDropdownMenuProps();\n\n\tlet [ date ] = useEntityProp( 'root', 'comment', 'date', commentId );\n\tconst [ siteFormat = getDateSettings().formats.date ] = useEntityProp(\n\t\t'root',\n\t\t'site',\n\t\t'date_format'\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\tformat: undefined,\n\t\t\t\t\t\tisLink: true,\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={ __( 'Date format' ) }\n\t\t\t\t\thasValue={ () => format !== undefined }\n\t\t\t\t\tonDeselect={ () => setAttributes( { format: undefined } ) }\n\t\t\t\t\tisShownByDefault\n\t\t\t\t>\n\t\t\t\t\t<DateFormatPicker\n\t\t\t\t\t\tformat={ format }\n\t\t\t\t\t\tdefaultFormat={ siteFormat }\n\t\t\t\t\t\tonChange={ ( nextFormat ) =>\n\t\t\t\t\t\t\tsetAttributes( { format: nextFormat } )\n\t\t\t\t\t\t}\n\t\t\t\t\t/>\n\t\t\t\t</ToolsPanelItem>\n\n\t\t\t\t<ToolsPanelItem\n\t\t\t\t\tlabel={ __( 'Link to comment' ) }\n\t\t\t\t\thasValue={ () => ! isLink }\n\t\t\t\t\tonDeselect={ () => setAttributes( { isLink: true } ) }\n\t\t\t\t\tisShownByDefault\n\t\t\t\t>\n\t\t\t\t\t<ToggleControl\n\t\t\t\t\t\tlabel={ __( 'Link to comment' ) }\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</ToolsPanel>\n\t\t</InspectorControls>\n\t);\n\n\tif ( ! commentId || ! date ) {\n\t\tdate = _x( 'Comment Date', 'block title' );\n\t}\n\n\tlet commentDate =\n\t\tdate instanceof Date ? (\n\t\t\t<time dateTime={ dateI18n( 'c', date ) }>\n\t\t\t\t{ format === 'human-diff'\n\t\t\t\t\t? humanTimeDiff( date )\n\t\t\t\t\t: dateI18n( format || siteFormat, date ) }\n\t\t\t</time>\n\t\t) : (\n\t\t\t<time>{ date }</time>\n\t\t);\n\n\tif ( isLink ) {\n\t\tcommentDate = (\n\t\t\t<a\n\t\t\t\thref=\"#comment-date-pseudo-link\"\n\t\t\t\tonClick={ ( event ) => event.preventDefault() }\n\t\t\t>\n\t\t\t\t{ commentDate }\n\t\t\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