|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +import clsx from 'clsx'; |
| 5 | + |
| 6 | +/** |
| 7 | + * WordPress dependencies |
| 8 | + */ |
| 9 | +import { __ } from '@wordpress/i18n'; |
| 10 | +import { |
| 11 | + useBlockProps, |
| 12 | + BlockControls, |
| 13 | + AlignmentControl, |
| 14 | + InspectorControls, |
| 15 | + HeadingLevelDropdown, |
| 16 | +} from '@wordpress/block-editor'; |
| 17 | +import { |
| 18 | + ToggleControl, |
| 19 | + __experimentalToolsPanel as ToolsPanel, |
| 20 | + __experimentalToolsPanelItem as ToolsPanelItem, |
| 21 | +} from '@wordpress/components'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Internal dependencies |
| 25 | + */ |
| 26 | +import { useToolsPanelDropdownMenuProps } from '../utils/hooks'; |
| 27 | +import { useTermName } from './use-term-name'; |
| 28 | + |
| 29 | +export default function TermNameEdit( { |
| 30 | + attributes, |
| 31 | + setAttributes, |
| 32 | + context: { termId, taxonomy }, |
| 33 | +} ) { |
| 34 | + const { textAlign, level = 0, isLink } = attributes; |
| 35 | + const { term } = useTermName( termId, taxonomy ); |
| 36 | + |
| 37 | + const termName = term?.name || __( 'Term Name' ); |
| 38 | + |
| 39 | + const blockProps = useBlockProps( { |
| 40 | + className: clsx( { |
| 41 | + [ `has-text-align-${ textAlign }` ]: textAlign, |
| 42 | + } ), |
| 43 | + } ); |
| 44 | + |
| 45 | + const dropdownMenuProps = useToolsPanelDropdownMenuProps(); |
| 46 | + |
| 47 | + const TagName = level === 0 ? 'p' : `h${ level }`; |
| 48 | + |
| 49 | + let termNameDisplay = termName; |
| 50 | + if ( isLink ) { |
| 51 | + termNameDisplay = ( |
| 52 | + <a |
| 53 | + href="#term-name-pseudo-link" |
| 54 | + onClick={ ( e ) => e.preventDefault() } |
| 55 | + > |
| 56 | + { termName } |
| 57 | + </a> |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + return ( |
| 62 | + <> |
| 63 | + <BlockControls group="block"> |
| 64 | + <HeadingLevelDropdown |
| 65 | + value={ level } |
| 66 | + options={ [ 0, 1, 2, 3, 4, 5, 6 ] } |
| 67 | + onChange={ ( newLevel ) => { |
| 68 | + setAttributes( { level: newLevel } ); |
| 69 | + } } |
| 70 | + /> |
| 71 | + <AlignmentControl |
| 72 | + value={ textAlign } |
| 73 | + onChange={ ( nextAlign ) => { |
| 74 | + setAttributes( { textAlign: nextAlign } ); |
| 75 | + } } |
| 76 | + /> |
| 77 | + </BlockControls> |
| 78 | + <InspectorControls> |
| 79 | + <ToolsPanel |
| 80 | + label={ __( 'Settings' ) } |
| 81 | + resetAll={ () => { |
| 82 | + setAttributes( { |
| 83 | + isLink: false, |
| 84 | + } ); |
| 85 | + } } |
| 86 | + dropdownMenuProps={ dropdownMenuProps } |
| 87 | + > |
| 88 | + <ToolsPanelItem |
| 89 | + hasValue={ () => !! isLink } |
| 90 | + label={ __( 'Make term name a link' ) } |
| 91 | + onDeselect={ () => setAttributes( { isLink: false } ) } |
| 92 | + isShownByDefault |
| 93 | + > |
| 94 | + <ToggleControl |
| 95 | + __nextHasNoMarginBottom |
| 96 | + label={ __( 'Make term name a link' ) } |
| 97 | + onChange={ () => |
| 98 | + setAttributes( { isLink: ! isLink } ) |
| 99 | + } |
| 100 | + checked={ isLink } |
| 101 | + /> |
| 102 | + </ToolsPanelItem> |
| 103 | + </ToolsPanel> |
| 104 | + </InspectorControls> |
| 105 | + <TagName { ...blockProps }>{ termNameDisplay }</TagName> |
| 106 | + </> |
| 107 | + ); |
| 108 | +} |
0 commit comments