import type { BlockEditProps } from '@wordpress/blocks'; import { useSelect } from '@wordpress/data'; import { store as coreStore, useEntityProp } from '@wordpress/core-data'; import { __ } from '@wordpress/i18n'; import { RichText, InspectorControls, BlockControls, useBlockProps, HeadingLevelDropdown, useBlockEditingMode, } from '@wordpress/block-editor'; import { ToggleControl, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components'; import { decodeEntities } from '@wordpress/html-entities'; import { useToolsPanelDropdownMenuProps } from '../utils/hooks'; import useDeprecatedTextAlign from '../utils/deprecated-text-align-attributes'; import type { SiteTitleAttributes } from './types'; export default function SiteTitleEdit( props: BlockEditProps< SiteTitleAttributes > ) { useDeprecatedTextAlign( props ); const { attributes, setAttributes } = props; const { level, levelOptions, isLink, linkTarget, fitText } = attributes; const canUserEdit = useSelect( ( select ) => select( coreStore ).canUser( 'update', { kind: 'root', name: 'site', } ), [] ); // Users who cannot edit the site settings cannot read them either, so the // title comes from the base entity instead. const [ title, setSiteTitle ] = useEntityProp< string >( 'root', canUserEdit ? 'site' : '__unstableBase', canUserEdit ? 'title' : 'name', undefined ); const dropdownMenuProps = useToolsPanelDropdownMenuProps(); const blockEditingMode = useBlockEditingMode(); const TagName = ( level === 0 ? 'p' : `h${ level }` ) as 'p' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'; const blockProps = useBlockProps( { className: ! canUserEdit && ! title ? 'wp-block-site-title__placeholder' : undefined, } ); const siteTitleContent = canUserEdit ? ( setSiteTitle( newTitle.trim() ) } allowedFormats={ [] } disableLineBreaks // Rich text only sets its `pre-wrap` default on mount, so // both values are explicit for Fit text to toggle cleanly. style={ { whiteSpace: fitText ? 'inherit' : 'pre-wrap', } } /> ) : ( { isLink ? ( event.preventDefault() } > { decodeEntities( title! ) || __( 'Site Title placeholder' ) } ) : ( { decodeEntities( title! ) || __( 'Site Title placeholder' ) } ) } ); return ( <> { blockEditingMode === 'default' && ( setAttributes( { level: newLevel } ) } /> ) } { setAttributes( { isLink: true, linkTarget: '_self', } ); } } dropdownMenuProps={ dropdownMenuProps } > ! isLink } label={ __( 'Make title link to home' ) } onDeselect={ () => setAttributes( { isLink: true } ) } isShownByDefault > setAttributes( { isLink: ! isLink } ) } checked={ isLink } /> { isLink && ( linkTarget !== '_self' } label={ __( 'Open in new tab' ) } onDeselect={ () => setAttributes( { linkTarget: '_self' } ) } isShownByDefault > setAttributes( { linkTarget: value ? '_blank' : '_self', } ) } checked={ linkTarget === '_blank' } /> ) } { siteTitleContent } ); }