/** * Structural operations on a builder's rows — add/delete/move rows, add/delete * columns, per-row General/Design and per-column align/width/design patches, * and placing an element into a column. * * Shared by the header's and footer's docked builders, so both edit the same * row model the same way. Every operation * writes the whole rows array back to `rowsPath`. */ import { arrayMove } from '@dnd-kit/sortable'; import { newRow, newColumn, type HeaderRow, type ColumnAlign, type RowGeneral, type RowDesign, type ColumnDesign, } from './canvas-model'; import type { SetPath } from '../../types'; interface UseRowOpsArgs { rows: HeaderRow[]; rowsPath: string; setPath: SetPath; maxRows: number; maxCols: number; } export function useRowOps( { rows, rowsPath, setPath, maxRows, maxCols, }: UseRowOpsArgs ) { const commitRows = ( next: HeaderRow[] ) => setPath( rowsPath, next ); const mapRow = ( rowId: string, fn: ( r: HeaderRow ) => HeaderRow ) => commitRows( rows.map( ( r ) => ( r.id === rowId ? fn( r ) : r ) ) ); const mapColumn = ( rowId: string, colId: string, fn: ( c: HeaderRow[ 'columns' ][ number ] ) => HeaderRow[ 'columns' ][ number ] ) => mapRow( rowId, ( r ) => ( { ...r, columns: r.columns.map( ( c ) => ( c.id === colId ? fn( c ) : c ) ), } ) ); return { commitRows, addRow: ( row: HeaderRow = newRow() ) => { if ( rows.length < maxRows ) { commitRows( [ ...rows, row ] ); } }, deleteRow: ( rowId: string ) => { if ( rows.length > 1 ) { commitRows( rows.filter( ( r ) => r.id !== rowId ) ); } }, moveRow: ( fromId: string, toId: string ) => { const from = rows.findIndex( ( r ) => r.id === fromId ); const to = rows.findIndex( ( r ) => r.id === toId ); if ( -1 !== from && -1 !== to && from !== to ) { commitRows( arrayMove( rows, from, to ) ); } }, // Back to a fresh row's settings — general, style, and each column's // width and style. Elements and column alignment (where each column // sits) are kept; Clear row is the one that empties it. resetRow: ( rowId: string ) => mapRow( rowId, ( r ) => ( { ...r, general: { ...newRow().general }, design: {}, columns: r.columns.map( ( c ) => ( { ...c, width: 0, design: {}, } ) ), } ) ), clearRow: ( rowId: string ) => mapRow( rowId, ( r ) => ( { ...r, columns: r.columns.map( ( c ) => ( { ...c, elements: [] } ) ), } ) ), setRowGeneral: ( rowId: string, patch: Partial< RowGeneral > ) => mapRow( rowId, ( r ) => ( { ...r, general: { ...r.general, ...patch }, } ) ), setRowDesign: ( rowId: string, patch: Partial< RowDesign > ) => mapRow( rowId, ( r ) => ( { ...r, design: { ...( r.design ?? {} ), ...patch }, } ) ), addColumn: ( rowId: string ) => mapRow( rowId, ( r ) => r.columns.length < maxCols ? { ...r, columns: [ ...r.columns, newColumn() ] } : r ), deleteColumn: ( rowId: string, colId: string ) => mapRow( rowId, ( r ) => r.columns.length > 1 ? { ...r, columns: r.columns.filter( ( c ) => c.id !== colId ), } : r ), setColumnAlign: ( rowId: string, colId: string, align: ColumnAlign ) => mapColumn( rowId, colId, ( c ) => ( { ...c, align } ) ), setColumnWidth: ( rowId: string, colId: string, width: number ) => mapColumn( rowId, colId, ( c ) => ( { ...c, width } ) ), setColumnDesign: ( rowId: string, colId: string, patch: Partial< ColumnDesign > ) => mapColumn( rowId, colId, ( c ) => ( { ...c, design: { ...( c.design ?? {} ), ...patch }, } ) ), // Append an (unplaced) element to the end of a column. addElement: ( rowId: string, colId: string, elementId: string ) => mapColumn( rowId, colId, ( c ) => ( { ...c, elements: [ ...( c.elements ?? [] ), elementId ], } ) ), }; } export type RowOps = ReturnType< typeof useRowOps >;