/** * Header templates — multi-header model + list operations. * * A header template is one complete header: its layout (desktop/tablet/mobile * rows + off-canvas list) and its own display conditions. The PHP resolver * renders the first enabled template whose conditions match (see * Header_Builder::resolve_active_template). Element settings stay global * (`header.elements`), shared across templates. * * NOTE: distinct from the starter *layout presets* in `header-templates.ts` * (Classic/Centered/Top bar), which seed a single canvas — those are applied * *into* a template here via the "Templates" button. */ import { __, sprintf } from '@wordpress/i18n'; import { uid, newColumn, type HeaderRow } from './canvas-model'; import type { DisplayRule } from './display-conditions'; export interface HeaderTemplate { id: string; name: string; enabled: boolean; display?: { rules?: DisplayRule[] }; rows?: HeaderRow[]; rows_tablet?: HeaderRow[]; tablet_custom?: boolean; rows_mobile?: HeaderRow[]; mobile_custom?: boolean; mobile?: string[]; } /** A starter desktop layout for a brand-new template: logo left, menu right. */ const starterRows = (): HeaderRow[] => [ { id: uid( 'row' ), general: { enabled: true }, columns: [ { ...newColumn( 'left' ), elements: [ 'logo' ] }, newColumn( 'center' ), { ...newColumn( 'right' ), elements: [ 'primary-menu' ] }, ], }, ]; /** * The footer's starter layout — the registered default footer: link list, * social icons and copyright, each on its own centered row. */ const starterFooterRows = (): HeaderRow[] => [ 'link-list', 'social', 'copyright' ].map( ( element ) => ( { id: uid( 'row' ), general: { enabled: true }, columns: [ { ...newColumn( 'center' ), elements: [ element ] } ], } ) ); /** * A fresh template seeded with a usable layout. * @param index Zero-based position (drives the default name). * @param zone Which zone the template belongs to — footer templates carry no * off-canvas list and take a footer default name. */ export const newTemplate = ( index: number, zone: 'header' | 'footer' = 'header' ): HeaderTemplate => ( { id: uid( 'tpl' ), name: 'footer' === zone ? /* translators: %d: template number. */ sprintf( __( 'Footer %d', 'aureon' ), index + 1 ) : /* translators: %d: template number. */ sprintf( __( 'Header %d', 'aureon' ), index + 1 ), enabled: true, display: { rules: [] }, rows: 'footer' === zone ? starterFooterRows() : starterRows(), rows_tablet: [], tablet_custom: false, rows_mobile: [], mobile_custom: false, ...( 'footer' === zone ? {} : { mobile: [ 'offcanvas-menu' ] } ), } ); /** * A deep copy of a template with a new id and a "(copy)" name. * @param template Source template. */ export const duplicateTemplate = ( template: HeaderTemplate ): HeaderTemplate => ( { ...structuredClone( template ), id: uid( 'tpl' ), /* translators: %s: source template name. */ name: sprintf( __( '%s (copy)', 'aureon' ), template.name ), } ); /** * Move the item at `from` to `to`, returning a new array (unchanged if out of * range). * @param templates Template list. * @param from Source index. * @param to Target index. */ export const moveTemplate = ( templates: HeaderTemplate[], from: number, to: number ): HeaderTemplate[] => { if ( to < 0 || to >= templates.length ) { return templates; } const next = [ ...templates ]; const [ moved ] = next.splice( from, 1 ); next.splice( to, 0, moved ); return next; };