import { useEntityProp } from '@wordpress/core-data'; import { dateI18n, humanTimeDiff, getSettings as getDateSettings, } from '@wordpress/date'; import { InspectorControls, useBlockProps, __experimentalDateFormatPicker as DateFormatPicker, } from '@wordpress/block-editor'; import { __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, ToggleControl, } from '@wordpress/components'; import { __, _x } from '@wordpress/i18n'; import type { BlockEditProps } from '@wordpress/blocks'; import { useToolsPanelDropdownMenuProps } from '../utils/hooks'; import type { CommentDateAttributes, CommentDateBlockContext } from './types'; /** * Renders the `core/comment-date` block on the editor. */ export default function Edit( { attributes: { format, isLink }, context: { commentId }, setAttributes, }: BlockEditProps< CommentDateAttributes > & { context: CommentDateBlockContext; } ) { const blockProps = useBlockProps(); const dropdownMenuProps = useToolsPanelDropdownMenuProps(); let [ date ] = useEntityProp< string >( 'root', 'comment', 'date', commentId ); const [ siteFormat = getDateSettings().formats.date ] = useEntityProp< string >( 'root', 'site', 'date_format' ); const inspectorControls = ( { setAttributes( { format: undefined, isLink: true, } ); } } dropdownMenuProps={ dropdownMenuProps } > format !== undefined } onDeselect={ () => setAttributes( { format: undefined } ) } isShownByDefault > setAttributes( { format: nextFormat } ) } /> ! isLink } onDeselect={ () => setAttributes( { isLink: true } ) } isShownByDefault > setAttributes( { isLink: ! isLink } ) } checked={ isLink } /> ); if ( ! commentId || ! date ) { date = _x( 'Comment Date', 'block title' ); } let commentDate = // @ts-expect-error Bug: the comment date is a string, so it is never a `Date`. date instanceof Date ? ( ) : ( ); if ( isLink ) { commentDate = ( event.preventDefault() } > { commentDate } ); } return ( <> { inspectorControls }
{ commentDate }
); }