/** * Font-family resolution shared by the Typography hub (index.tsx) and each of * its per-tab native Customizer sections (tab-section.tsx) — extracted so the * two don't drift: heading/body slugs and CSS come from the active Font * Pairing, link/button/misc from `design.font_family`, and every one of them * resolves against the same Font Pairing + Font Library catalog. */ import { useEffect } from '@wordpress/element'; import { loadGoogleFont } from '../font-pairings/load-google-font'; import type { TypographySchema, TypographyFieldValues, FontPairingsSchema, FontChoice, WorkingSettings, } from '../../types'; interface UseTypographyHelpersArgs { schema?: TypographySchema; fontPairingsSchema?: FontPairingsSchema; defaults: Partial< WorkingSettings >; working: WorkingSettings; } export function useTypographyHelpers( { schema, fontPairingsSchema, defaults, working, }: UseTypographyHelpersArgs ) { const families = working?.design?.font_family ?? {}; const familyDefaults = defaults?.design?.font_family ?? {}; const typography = working?.design?.typography ?? {}; const typographyDefaults = defaults?.design?.typography ?? {}; const pairings = working?.font_pairings; const pairingFonts = fontPairingsSchema?.fonts ?? []; const activePairing = pairings?.items.find( ( item ) => item.id === pairings.active ); const activePairingFontLabel = ( role: 'heading' | 'body' ): string => { const slug = activePairing?.[ role ] ?? 'system'; return ( pairingFonts.find( ( font ) => font.value === slug )?.label ?? slug ); }; const familySlugFor = ( role: string ): string => { if ( 'heading' === role || 'body' === role ) { return activePairing?.[ role ] ?? families[ role ] ?? 'system'; } return families[ role ] ?? 'system'; }; const familyLabel = ( role: string ): string => { const slug = familySlugFor( role ); return ( pairingFonts.find( ( font ) => font.value === slug )?.label ?? slug ); }; // Same shape as PairingCard's cssFamilyFor() (see font-pairings/pairing-card.tsx): // a Google Font resolves to its literal family name (only loaded on-demand // in wp-admin, see loadGoogleFont below); anything else falls back to its // generic category, since per-slug --aureon-font-family-* custom properties // aren't guaranteed to be loaded on this admin screen. const familyCssFor = ( role: string ): string => { const slug = familySlugFor( role ); const font = pairingFonts.find( ( item ) => item.value === slug ) as | FontChoice | undefined; if ( ! font ) { return 'inherit'; } return 'google' === font.source ? `"${ font.label }", ${ font.category }` : font.category; }; const headingFont = pairingFonts.find( ( font ) => font.value === familySlugFor( 'heading' ) ); useEffect( () => { if ( 'google' === headingFont?.source ) { loadGoogleFont( headingFont.label ); } }, [ headingFont?.source, headingFont?.label ] ); const familyOptions = ( schema?.families ?? [] ).map( ( item ) => ( { label: item.label, value: item.value, } ) ); const pairingFamilyOptions = pairingFonts.map( ( font ) => ( { label: font.label, value: font.value, } ) ); const weightOptions = ( schema?.weights ?? [] ).map( ( item ) => ( { label: `${ item.value } — ${ item.label }`, value: item.value, } ) ); const transformOptions = ( schema?.transforms ?? [] ).map( ( item ) => ( { label: item.label, value: item.value, } ) ); const decorationOptions = ( schema?.decorations ?? [] ).map( ( item ) => ( { label: item.label, value: item.value, } ) ); const scaleOptions = ( schema?.scales ?? [] ).map( ( item ) => ( { label: item.label, value: item.value, } ) ); return { families, familyDefaults, typography, typographyDefaults, pairings, pairingFonts, activePairing, activePairingFontLabel, familySlugFor, familyLabel, familyCssFor, familyOptions, pairingFamilyOptions, weightOptions, transformOptions, decorationOptions, scaleOptions, }; } export function isSectionDefault( values: TypographyFieldValues, defaultValues: TypographyFieldValues, familyRole: string, families: Record< string, string >, familyDefaults: Record< string, string > ): boolean { const familyDefault = familyDefaults[ familyRole ]; if ( undefined !== familyDefault && families[ familyRole ] !== familyDefault ) { return false; } return ( Object.keys( defaultValues ) as ( keyof TypographyFieldValues )[] ).every( ( key ) => { const defaultValue = defaultValues[ key ]; // `overrides` is the one object-valued field (per-heading-level custom // sizes) — compare it by value, not by reference, or a freshly loaded // `values` object (new reference, same empty contents) would always // read as "changed". if ( defaultValue && 'object' === typeof defaultValue ) { const value = ( values[ key ] ?? {} ) as Record< string, string >; return Object.entries( defaultValue as Record< string, string > ).every( ( [ subKey, subValue ] ) => ( value[ subKey ] ?? '' ) === subValue ); } return values[ key ] === defaultValue; } ); }