/** * Groups a zone's placeable elements for the Elements tab's card grid. * * "Basic" holds the elements nearly every header (or footer) uses; anything else the * schema offers — including elements a plugin registers, which this file has * never heard of — falls into "Advanced", so nothing is ever left out of the * grid. Order within a group follows the lists below, then schema order. */ import { __ } from '@wordpress/i18n'; export interface ElementDef { id: string; label: string; } export interface ElementGroup { key: string; label: string; elements: ElementDef[]; } const BASIC: Record< 'header' | 'footer', string[] > = { header: [ 'logo', 'primary-menu', 'secondary-menu', 'secondary-menu-2', 'secondary-menu-3', 'secondary-menu-4', 'secondary-menu-5', 'secondary-menu-6', 'secondary-menu-7', 'secondary-menu-8', 'search', 'account', 'cart', 'button', 'text-html', 'divider', ], footer: [ 'logo', 'link-list', 'link-list-2', 'link-list-3', 'link-list-4', 'link-list-5', 'link-list-6', 'link-list-7', 'link-list-8', 'social', 'copyright', 'text-html', 'primary-menu', 'newsletter', 'payment', ], }; const ADVANCED_ORDER = [ 'toggle', 'offcanvas-menu', 'social', 'language-switcher', 'image', 'image-2', 'image-3', 'image-4', 'image-5', 'image-6', 'image-7', 'image-8', ]; // Shorter names for the cards, where the full label wraps awkwardly in a // ~90px tile. The dock's chips and the settings modal keep the full label. const CARD_LABELS: Record< string, string > = { logo: __( 'Logo', 'aureon' ), 'primary-menu': __( 'Menu', 'aureon' ), toggle: __( 'Menu Toggle', 'aureon' ), }; export const cardLabel = ( el: ElementDef ): string => CARD_LABELS[ el.id ] ?? el.label; const rank = ( order: string[], id: string ) => { const index = order.indexOf( id ); return -1 === index ? order.length : index; }; export function groupElements( elements: ElementDef[], zone: 'header' | 'footer' = 'header' ): ElementGroup[] { const order = BASIC[ zone ]; const basic = elements .filter( ( el ) => order.includes( el.id ) ) .sort( ( a, b ) => rank( order, a.id ) - rank( order, b.id ) ); const advanced = elements .filter( ( el ) => ! order.includes( el.id ) ) .sort( ( a, b ) => rank( ADVANCED_ORDER, a.id ) - rank( ADVANCED_ORDER, b.id ) ); return [ { key: 'basic', label: __( 'Basic', 'aureon' ), elements: basic }, { key: 'advanced', label: __( 'Advanced', 'aureon' ), elements: advanced, }, ].filter( ( group ) => group.elements.length > 0 ); }