/** * Shared constants and small pure helpers for the Typography section. */ export const ID_PREFIX = 'aureon-typography'; export const LENGTH_UNITS = [ { value: 'px', label: 'px' }, { value: 'rem', label: 'rem' }, { value: 'em', label: 'em' }, { value: '%', label: '%' }, ]; export const LINE_HEIGHT_UNITS = [ { value: 'em', label: 'em' }, { value: 'px', label: 'px' }, { value: 'rem', label: 'rem' }, ]; interface HeadingLevel { key: string; label: string; lineHeightDelta: number; } export const HEADING_LEVELS: HeadingLevel[] = [ { key: 'h1', label: 'H1', lineHeightDelta: 0 }, { key: 'h2', label: 'H2', lineHeightDelta: 0.05 }, { key: 'h3', label: 'H3', lineHeightDelta: 0.1 }, { key: 'h4', label: 'H4', lineHeightDelta: 0.2 }, { key: 'h5', label: 'H5', lineHeightDelta: 0.3 }, { key: 'h6', label: 'H6', lineHeightDelta: 0.4 }, ]; /** * Per-level size ratios against the H1 base size, keyed by * `design.typography.headings.scale`. Kept in sync with the identical ratio * tables in includes/Core/Dynamic_CSS.php (HEADING_SCALE_RATIOS) so this * preview matches the generated frontend CSS exactly. * * 'linear' keeps the original arithmetic-ish steps this feature shipped * with; 'modular' is a true geometric scale (ratio 1.25, a "major third"). */ export const HEADING_SCALE_RATIOS: Record< string, Record< string, number > > = { modular: { h1: 1, h2: 0.8, h3: 0.64, h4: 0.512, h5: 0.41, h6: 0.328, }, linear: { h1: 1, h2: 0.833, h3: 0.667, h4: 0.5, h5: 0.417, h6: 0.333, }, }; /** * Rough px equivalent for a CSS length, for the heading scale preview only. * @param length */ export function toPx( length: string ): number { const match = /^(-?\d*\.?\d+)(px|rem|em)?$/.exec( length.trim() ); if ( ! match ) { return 0; } const value = parseFloat( match[ 1 ] ); const unit = match[ 2 ] ?? 'px'; return 'px' === unit ? value : value * 16; } /** * Multiply a CSS length's numeric part by a ratio, preserving its unit (e.g. * `3rem` * 0.8 => `2.4rem`) — mirrors Dynamic_CSS::scale_length() on the PHP * side so the preview matches the generated CSS. * @param length * @param ratio */ export function scaleLength( length: string, ratio: number ): string { const match = /^(-?\d*\.?\d+)([a-z%]*)$/i.exec( length.trim() ); if ( ! match ) { return length; } const [ , quantity, unit ] = match; const scaled = parseFloat( ( parseFloat( quantity ) * ratio ).toFixed( 4 ) ); return `${ scaled }${ unit }`; }