/** * Types and helpers for the dynamic header model. * * The header is an ordered list of rows; each row an ordered list of columns * holding element ids. The drag-and-drop hook works on a flat container map * (columnId → element ids), so these helpers convert between the two while * preserving row/column structure (ids, align, width, general). */ import type { Containers } from './use-canvas-dnd'; import type { BorderSide } from '../../controls/border-field'; /** A builder layout tier — each template keeps a row list per device. */ export type DeviceKey = 'desktop' | 'tablet' | 'mobile'; export type ColumnAlign = 'left' | 'center' | 'right'; export interface ColumnDesign { background?: string; border_color?: string; border_width?: number; /** * Which edges the border is drawn on. Undefined means every side — both the * default and what a column stored before this setting existed implies. */ border_sides?: BorderSide[]; padding?: BoxValue; /** Lay the column's elements out side by side (default) or stacked. */ layout?: ColumnLayout; } export type ColumnLayout = 'row' | 'stack'; export interface HeaderColumn { id: string; align: ColumnAlign; width: number; design?: ColumnDesign; elements: string[]; } export type ContentWidth = 'contained' | 'full'; export type VerticalAlign = 'top' | 'center' | 'bottom'; export interface RowGeneral { enabled: boolean; content_width?: ContentWidth; vertical_align?: VerticalAlign; /** Pin this row (consecutive sticky rows pin together as one unit). */ sticky?: boolean; /** Render this row with a see-through background (floats over the page). */ transparent?: boolean; } /** CSS-length box (BoxControl-native): each side is e.g. "10px" or "". */ export interface BoxValue { top?: string; right?: string; bottom?: string; left?: string; } export interface RowDesign { background?: string; border_bottom_color?: string; border_bottom_size?: number; padding?: BoxValue; margin?: BoxValue; } export interface HeaderRow { id: string; general: RowGeneral; design?: RowDesign; columns: HeaderColumn[]; } /** * Short, collision-resistant id for a new row/column. * @param prefix */ export const uid = ( prefix: string ): string => `${ prefix }-${ Math.random().toString( 36 ).slice( 2, 8 ) }`; /** * Flatten every column into a `columnId → elements` container map. * @param rows */ export const columnsToContainers = ( rows: HeaderRow[] ): Containers => { const containers: Containers = {}; rows.forEach( ( row ) => row.columns.forEach( ( col ) => { containers[ col.id ] = [ ...( col.elements ?? [] ) ]; } ) ); return containers; }; /** * Write element arrays from a container map back onto the rows structure. * @param rows * @param containers */ export const mergeElementsIntoRows = ( rows: HeaderRow[], containers: Containers ): HeaderRow[] => rows.map( ( row ) => ( { ...row, columns: row.columns.map( ( col ) => ( { ...col, elements: containers[ col.id ] ?? col.elements ?? [], } ) ), } ) ); /** * A fresh empty column. * @param align */ export const newColumn = ( align: ColumnAlign = 'left' ): HeaderColumn => ( { id: uid( 'col' ), align, width: 0, elements: [], } ); /** A fresh row with a single empty column. */ export const newRow = (): HeaderRow => ( { id: uid( 'row' ), general: { enabled: true, content_width: 'contained', vertical_align: 'center', }, design: {}, columns: [ newColumn() ], } ); /** * Whether a rows list has at least one element placed in any column. Used to * decide if a device layout is "effectively empty" and should be re-seeded when * its "Customize this layout" gate is enabled (an empty rows array and a rows * array whose every column is empty both count as empty). * @param rows */ export const rowsHaveElements = ( rows: HeaderRow[] | undefined ): boolean => Array.isArray( rows ) && rows.some( ( row ) => row.columns?.some( ( col ) => ( col.elements?.length ?? 0 ) > 0 ) ); /** * Seed layout applied when a device's "Customize this layout" is first enabled: * a single row with the Logo on the left and the Account link + Menu Toggle on * the right (a spare centre column in between). This is the collapsed form of * the default "right-aligned nav" desktop layout — the full menu folds into the * toggle (which opens the off-canvas list) while the account link stays inline — * so the canvas is immediately useful and consistent across devices. */ export const seededDeviceRows = (): HeaderRow[] => { const row = newRow(); return [ { ...row, columns: [ { ...newColumn( 'left' ), elements: [ 'logo' ] }, newColumn( 'center' ), { ...newColumn( 'right' ), elements: [ 'account', 'toggle' ] }, ], }, ]; }; /** The header's fixed row slots, top to bottom. */ export const ROW_SLOTS = 3; /** * An empty three-column row with stable ids, standing in for a slot the saved * layout doesn't have yet. Stable (not `uid()`) because it's rebuilt every * render and the drag-and-drop containers are keyed by column id. * @param slot */ const placeholderRow = ( slot: string ): HeaderRow => ( { id: `row-slot-${ slot }`, general: { enabled: true, content_width: 'contained', vertical_align: 'center', }, design: {}, columns: ( [ 'left', 'center', 'right' ] as ColumnAlign[] ).map( ( align ) => ( { id: `col-slot-${ slot }-${ align }`, align, width: 0, elements: [], } ) ), } ); /** * Pad a saved rows list out to the three fixed slots — Top Bar, Main Header, * Bottom Bar — so the builder can always show all three. * * Layouts saved before the slots existed have one row (the main header) or * two (main + bottom bar); their rows keep those meanings and the missing * slots are filled with empty placeholders around them. Empty rows never * render on the front end, so the padded list renders identically; it is * only written back once something is actually edited. * @param rows */ export const padToSlots = ( rows: HeaderRow[] ): HeaderRow[] => { if ( rows.length >= ROW_SLOTS ) { return rows; } const top = placeholderRow( 'top' ); const bottom = placeholderRow( 'bottom' ); if ( 0 === rows.length ) { return [ top, placeholderRow( 'main' ), bottom ]; } if ( 1 === rows.length ) { return [ top, rows[ 0 ], bottom ]; } return [ top, ...rows ]; };