import { InspectorControls, RichText, useBlockProps, } from '@wordpress/block-editor'; import { registerBlockType, type BlockEditProps, type BlockSaveProps, } from '@wordpress/blocks'; import { Button, PanelBody, TextControl } from '@wordpress/components'; const NAME = 'recomaze/howto'; interface Step { title: string; content: string; } type Attributes = { description: string; hours: number; minutes: number; steps: Step[]; }; const totalTime = (hours: number, minutes: number): string => { const parts = []; if (hours > 0) { parts.push(`${hours} ${hours === 1 ? 'hour' : 'hours'}`); } if (minutes > 0) { parts.push(`${minutes} ${minutes === 1 ? 'minute' : 'minutes'}`); } return parts.join(' '); }; const Edit = ({ attributes, setAttributes, }: BlockEditProps): JSX.Element => { const steps = attributes.steps ?? []; const change = (index: number, patch: Partial): void => setAttributes({ steps: steps.map((step, at) => at === index ? { ...step, ...patch } : step ), }); return ( <> setAttributes({ hours: Math.max(0, Number(value) || 0) }) } __nextHasNoMarginBottom __next40pxDefaultSize /> setAttributes({ minutes: Math.max(0, Number(value) || 0) }) } help="Shown under the description and marked up as the total time." __nextHasNoMarginBottom __next40pxDefaultSize />
setAttributes({ description })} />
    {steps.map((step, index) => (
  1. change(index, { title })} /> change(index, { content })} />
  2. ))}
); }; const Save = ({ attributes }: BlockSaveProps): JSX.Element => { const time = totalTime(attributes.hours ?? 0, attributes.minutes ?? 0); return (
{attributes.description.trim() !== '' && ( )} {time !== '' && (

Total time: {time}

)}
    {(attributes.steps ?? []) .filter( step => step.title.trim() !== '' || step.content.trim() !== '' ) .map((step, index) => (
  1. {step.title.trim() !== '' && ( )}
  2. ))}
); }; registerBlockType( { name: NAME, title: 'How-to', category: 'text', attributes: { description: { type: 'string', default: '' }, hours: { type: 'number', default: 0 }, minutes: { type: 'number', default: 0 }, steps: { type: 'array', default: [] }, }, }, { edit: Edit, save: Save } );