import type { MouseEvent, ReactNode } from 'react'; import clsx from 'clsx'; import { Placeholder, Spinner as WCSpinner, ToggleControl, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components'; import { useInstanceId } from '@wordpress/compose'; import { InspectorControls, useBlockProps, RichText, } from '@wordpress/block-editor'; import { decodeEntities } from '@wordpress/html-entities'; import { __, sprintf } from '@wordpress/i18n'; import { pin } from '@wordpress/icons'; import { useEntityRecords } from '@wordpress/core-data'; import type { Taxonomy, Term } from '@wordpress/core-data'; import { useDispatch } from '@wordpress/data'; import { SelectControl, VisuallyHidden } from '@wordpress/ui'; import { store as noticeStore } from '@wordpress/notices'; import type { BlockEditProps } from '@wordpress/blocks'; import { useToolsPanelDropdownMenuProps } from '../utils/hooks'; import type { CategoriesAttributes } from './types'; export default function CategoriesEdit( { attributes: { displayAsDropdown, showHierarchy, showPostCounts, showOnlyTopLevel, showEmpty, label, showLabel, taxonomy: taxonomySlug, }, setAttributes, className, clientId, }: BlockEditProps< CategoriesAttributes > & { className?: string } ) { const selectId = useInstanceId( CategoriesEdit, 'blocks-category-select' ); const { records: allTaxonomies, // @ts-expect-error Bug: `useEntityRecords` returns `isResolving`, so this is always `undefined`. isResolvingTaxonomies, } = useEntityRecords< Taxonomy >( 'root', 'taxonomy', { per_page: -1 } ); const taxonomies = allTaxonomies?.filter( ( t ) => t.visibility.public ); const taxonomyItems = ( taxonomies ?? [] ).map( ( t ) => ( { label: t.name, value: t.slug, } ) ); const taxonomy = taxonomies?.find( ( t ) => t.slug === taxonomySlug ); const isHierarchicalTaxonomy = ! isResolvingTaxonomies && taxonomy?.hierarchical; const query: Record< string, unknown > = { per_page: -1, hide_empty: ! showEmpty, context: 'view', }; if ( isHierarchicalTaxonomy && showOnlyTopLevel ) { query.parent = 0; } const { records: categories, isResolving } = useEntityRecords< Term >( 'taxonomy', taxonomySlug, query ); const { createWarningNotice } = useDispatch( noticeStore ); const showRedirectionPreventedNotice = ( event: MouseEvent< HTMLAnchorElement > ) => { event.preventDefault(); createWarningNotice( __( 'Links are disabled in the editor.' ), { id: `block-library/core/categories/redirection-prevented/${ clientId }`, type: 'snackbar', } ); }; const getCategoriesList = ( parentId: number | null ): Term[] => { if ( ! categories?.length ) { return []; } if ( parentId === null ) { return categories; } return categories.filter( ( { parent } ) => parent === parentId ); }; const toggleAttribute = ( attributeName: keyof CategoriesAttributes ) => ( newValue: boolean ) => setAttributes( { [ attributeName ]: newValue } ); const renderCategoryName = ( name?: string ) => ! name ? __( '(Untitled)' ) : decodeEntities( name ).trim(); const renderCategoryList = () => { const parentId = isHierarchicalTaxonomy && showHierarchy ? 0 : null; const categoriesList = getCategoriesList( parentId ); return categoriesList.map( ( category ) => renderCategoryListItem( category ) ); }; const renderCategoryListItem = ( category: Term ) => { const childCategories = getCategoriesList( category.id ); const { id, link, count, name } = category; return (
  • { renderCategoryName( name ) } { showPostCounts && ` (${ count })` } { isHierarchicalTaxonomy && showHierarchy && !! childCategories.length && ( ) }
  • ); }; const renderCategoryDropdown = () => { const parentId = isHierarchicalTaxonomy && showHierarchy ? 0 : null; const categoriesList = getCategoriesList( parentId ); return ( <> { showLabel ? ( setAttributes( { label: html } ) } /> ) : ( // eslint-disable-next-line jsx-a11y/label-has-associated-control }> { label ? label : taxonomy?.name } ) } ); }; const renderCategoryDropdownItem = ( category: Term, level: number ): ReactNode => { const { id, count, name } = category; const childCategories = getCategoriesList( id ); return [ , isHierarchicalTaxonomy && showHierarchy && !! childCategories.length && childCategories.map( ( childCategory ) => renderCategoryDropdownItem( childCategory, level + 1 ) ), ]; }; const TagName = !! categories?.length && ! displayAsDropdown && ! isResolving ? 'ul' : 'div'; const classes = clsx( className, `wp-block-categories-taxonomy-${ taxonomySlug }`, { 'wp-block-categories-list': !! categories?.length && ! displayAsDropdown && ! isResolving, 'wp-block-categories-dropdown': !! categories?.length && displayAsDropdown && ! isResolving, } ); const blockProps = useBlockProps( { className: classes, } ); const dropdownMenuProps = useToolsPanelDropdownMenuProps(); return ( { setAttributes( { taxonomy: 'category', displayAsDropdown: false, showHierarchy: false, showPostCounts: false, showOnlyTopLevel: false, showEmpty: false, showLabel: true, } ); } } dropdownMenuProps={ dropdownMenuProps } > { taxonomyItems.length > 0 && ( { return taxonomySlug !== 'category'; } } label={ __( 'Taxonomy' ) } onDeselect={ () => { setAttributes( { taxonomy: 'category' } ); } } isShownByDefault > item.value === taxonomySlug ) ?? taxonomyItems[ 0 ] } onValueChange={ ( item ) => item && setAttributes( { taxonomy: item.value!, } ) } /> ) } !! displayAsDropdown } label={ __( 'Display as dropdown' ) } onDeselect={ () => setAttributes( { displayAsDropdown: false } ) } isShownByDefault > { displayAsDropdown && ( ! showLabel } label={ __( 'Show label' ) } onDeselect={ () => setAttributes( { showLabel: true } ) } isShownByDefault > ) } !! showPostCounts } label={ __( 'Show post counts' ) } onDeselect={ () => setAttributes( { showPostCounts: false } ) } isShownByDefault > { isHierarchicalTaxonomy && ( !! showOnlyTopLevel } label={ __( 'Show only top level terms' ) } onDeselect={ () => setAttributes( { showOnlyTopLevel: false } ) } isShownByDefault > ) } !! showEmpty } label={ __( 'Show empty terms' ) } onDeselect={ () => setAttributes( { showEmpty: false } ) } isShownByDefault > { isHierarchicalTaxonomy && ! showOnlyTopLevel && ( !! showHierarchy } label={ __( 'Show hierarchy' ) } onDeselect={ () => setAttributes( { showHierarchy: false } ) } isShownByDefault > ) } { isResolving && ( ) } { ! isResolving && categories?.length === 0 && (

    { taxonomy!.labels.no_terms }

    ) } { ! isResolving && categories?.length! > 0 && ( displayAsDropdown ? renderCategoryDropdown() : renderCategoryList() ) }
    ); }