/**
* Header/footer template switcher.
*
* A pill in the builder toolbar showing the template being edited. It opens
* the manager: every template in priority order (top = highest, used first
* when several match a page), each with a drag handle to reorder, an enable
* toggle, and rename / duplicate / delete actions. Renaming is an explicit
* edit — pencil, type, Save (or Enter); Cancel (or Esc) leaves the name as it
* was — and deleting asks first.
*
* Structural changes write the whole templates array; field edits write the
* template's own path. Display conditions are edited in the Settings tab; this
* only summarizes them.
*/
import { useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { Dropdown, FormToggle } from '@wordpress/components';
import {
DndContext,
KeyboardSensor,
PointerSensor,
closestCenter,
useSensor,
useSensors,
type DragEndEvent,
type Modifier,
} from '@dnd-kit/core';
import {
SortableContext,
sortableKeyboardCoordinates,
useSortable,
verticalListSortingStrategy,
} from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
import {
Check,
ChevronDown,
Copy,
GripVertical,
Pencil,
Plus,
Trash2,
X,
} from 'lucide-react';
import ConfirmDialog from '../../confirm-dialog';
import {
newTemplate,
duplicateTemplate,
moveTemplate,
type HeaderTemplate,
} from './template-list';
import { describeConditions } from './display-conditions';
import type { SetPath } from '../../types';
// Templates only move up and down the list.
const restrictToVerticalAxis: Modifier = ( { transform } ) => ( {
...transform,
x: 0,
} );
interface TemplateSwitcherProps {
templates: HeaderTemplate[];
selected: number;
basePath: string;
maxTemplates: number;
/** Which zone's templates are managed — drives labels + template shape. */
zone?: 'header' | 'footer';
onSelect: ( index: number ) => void;
setPath: SetPath;
}
interface ItemProps {
tpl: HeaderTemplate;
index: number;
isSelected: boolean;
canDelete: boolean;
canDuplicate: boolean;
canReorder: boolean;
onSelect: () => void;
onRename: ( name: string ) => void;
onToggle: ( enabled: boolean ) => void;
onDuplicate: () => void;
onDelete: () => void;
}
function TemplateItem( {
tpl,
index,
isSelected,
canDelete,
canDuplicate,
canReorder,
onSelect,
onRename,
onToggle,
onDuplicate,
onDelete,
}: ItemProps ) {
const [ draft, setDraft ] = useState< string | null >( null );
const {
attributes,
listeners,
setNodeRef,
setActivatorNodeRef,
transform,
transition,
isDragging,
} = useSortable( { id: tpl.id, disabled: ! canReorder } );
const isEnabled = tpl.enabled !== false;
const editing = null !== draft;
const save = () => {
const name = ( draft ?? '' ).trim();
if ( name && name !== tpl.name ) {
onRename( name );
}
setDraft( null );
};
return (
{ editing ? (
) : (
<>
onToggle( event.target.checked )
}
/>
>
) }
);
}
export default function TemplateSwitcher( {
templates,
selected,
basePath,
maxTemplates,
zone = 'header',
onSelect,
setPath,
}: TemplateSwitcherProps ) {
const [ pendingDelete, setPendingDelete ] = useState< number | null >(
null
);
const sensors = useSensors(
useSensor( PointerSensor, { activationConstraint: { distance: 4 } } ),
useSensor( KeyboardSensor, {
coordinateGetter: sortableKeyboardCoordinates,
} )
);
const commitList = ( next: HeaderTemplate[] ) => setPath( basePath, next );
const atMax = templates.length >= maxTemplates;
const isFooter = 'footer' === zone;
if ( ! templates.length ) {
return null;
}
const active = templates[ selected ] ?? templates[ 0 ];
const addTemplate = () => {
if ( atMax ) {
return;
}
commitList( [ ...templates, newTemplate( templates.length, zone ) ] );
onSelect( templates.length );
};
const duplicate = ( index: number ) => {
if ( atMax ) {
return;
}
const next = [ ...templates ];
next.splice( index + 1, 0, duplicateTemplate( templates[ index ] ) );
commitList( next );
onSelect( index + 1 );
};
const remove = ( index: number ) => {
if ( templates.length <= 1 ) {
return;
}
commitList( templates.filter( ( _, i ) => i !== index ) );
onSelect( Math.max( 0, index <= selected ? selected - 1 : selected ) );
};
// Reorder, keeping the same template selected wherever it lands.
const onDragEnd = ( { active: dragged, over }: DragEndEvent ) => {
if ( ! over || dragged.id === over.id ) {
return;
}
const from = templates.findIndex( ( t ) => t.id === dragged.id );
const to = templates.findIndex( ( t ) => t.id === over.id );
if ( -1 === from || -1 === to ) {
return;
}
const next = moveTemplate( templates, from, to );
commitList( next );
onSelect( next.findIndex( ( t ) => t.id === active.id ) );
};
const editingLabel = isFooter
? sprintf(
/* translators: %s: active template name. */
__( 'Editing footer template: %s', 'aureon' ),
active.name
)
: sprintf(
/* translators: %s: active template name. */
__( 'Editing header template: %s', 'aureon' ),
active.name
);
return (
(
) }
renderContent={ ( { onClose } ) => (
{ isFooter
? __( 'Footer templates', 'aureon' )
: __( 'Header templates', 'aureon' ) }
{ __(
'When several templates match a page, the topmost enabled one is used. Drag to set priority.',
'aureon'
) }
t.id ) }
strategy={ verticalListSortingStrategy }
>
{ templates.map( ( tpl, index ) => (
1 }
canDuplicate={ ! atMax }
canReorder={ templates.length > 1 }
onSelect={ () => onSelect( index ) }
onRename={ ( name ) =>
setPath(
`${ basePath }.${ index }.name`,
name
)
}
onToggle={ ( enabled ) =>
setPath(
`${ basePath }.${ index }.enabled`,
enabled
)
}
onDuplicate={ () =>
duplicate( index )
}
onDelete={ () => {
setPendingDelete( index );
onClose();
} }
/>
) ) }
{ atMax && (
{ sprintf(
/* translators: %d: maximum number of templates. */
__( 'Maximum of %d reached.', 'aureon' ),
maxTemplates
) }
) }
) }
/>
{ null !== pendingDelete && templates[ pendingDelete ] && (
{
remove( pendingDelete );
setPendingDelete( null );
} }
onCancel={ () => setPendingDelete( null ) }
/>
) }
);
}