2024-02-01 17:16:12 +00:00
|
|
|
/* eslint-disable @woocommerce/dependency-group */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
privateApis as blockEditorPrivateApis,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
} from '@wordpress/block-editor';
|
|
|
|
// @ts-expect-error No types for this exist yet.
|
|
|
|
import { unlock } from '@wordpress/edit-site/build-module/lock-unlock';
|
|
|
|
|
2023-09-29 05:20:29 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2024-08-09 15:44:10 +00:00
|
|
|
import { FONT_PAIRINGS } from './sidebar/global-styles/font-pairing-variations/constants';
|
2023-09-29 05:20:29 +00:00
|
|
|
import { FontFamiliesLoader } from './sidebar/global-styles/font-pairing-variations/font-families-loader';
|
2024-08-09 15:44:10 +00:00
|
|
|
import { useContext, useMemo } from '@wordpress/element';
|
|
|
|
import { FontFamily } from '../types/font';
|
2024-02-01 17:16:12 +00:00
|
|
|
import { FontFamiliesLoaderDotCom } from './sidebar/global-styles/font-pairing-variations/font-families-loader-dot-com';
|
|
|
|
import { CustomizeStoreContext } from '.';
|
|
|
|
import { isAIFlow, isNoAIFlow } from '../guards';
|
|
|
|
|
|
|
|
const { useGlobalSetting } = unlock( blockEditorPrivateApis );
|
|
|
|
|
2023-09-29 05:20:29 +00:00
|
|
|
export const PreloadFonts = () => {
|
2024-08-09 15:44:10 +00:00
|
|
|
const [ enabledFontFamilies ]: [
|
2024-02-01 17:16:12 +00:00
|
|
|
{
|
|
|
|
custom: Array< FontFamily >;
|
|
|
|
theme: Array< FontFamily >;
|
|
|
|
},
|
|
|
|
( font: {
|
|
|
|
custom: Array< FontFamily >;
|
|
|
|
theme: Array< FontFamily >;
|
|
|
|
} ) => void
|
|
|
|
] = useGlobalSetting( 'typography.fontFamilies' );
|
|
|
|
|
2024-02-28 14:47:31 +00:00
|
|
|
// theme.json file font families
|
|
|
|
const [ baseFontFamilies ] = useGlobalSetting(
|
|
|
|
'typography.fontFamilies',
|
|
|
|
undefined,
|
|
|
|
'base'
|
|
|
|
);
|
|
|
|
|
2024-02-01 17:16:12 +00:00
|
|
|
const { context } = useContext( CustomizeStoreContext );
|
|
|
|
|
2023-09-29 05:20:29 +00:00
|
|
|
const allFontChoices = FONT_PAIRINGS.map(
|
|
|
|
( fontPair ) => fontPair?.settings?.typography?.fontFamilies?.theme
|
|
|
|
);
|
2024-02-01 17:16:12 +00:00
|
|
|
|
2024-02-14 09:30:18 +00:00
|
|
|
const iframeInstance = useMemo( () => {
|
|
|
|
return document.querySelector(
|
|
|
|
'.block-editor-block-preview__content iframe'
|
|
|
|
) as HTMLObjectElement | null;
|
|
|
|
}, [] );
|
|
|
|
|
2023-09-29 05:20:29 +00:00
|
|
|
const fontFamilies = allFontChoices.map( ( fontPair ) => {
|
|
|
|
return [
|
|
|
|
...fontPair.map( ( font ) => {
|
|
|
|
return {
|
|
|
|
...font,
|
|
|
|
name: font.fontFamily,
|
|
|
|
};
|
|
|
|
} ),
|
|
|
|
];
|
|
|
|
} );
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2024-02-01 17:16:12 +00:00
|
|
|
{ isAIFlow( context.flowType ) &&
|
|
|
|
fontFamilies.map( ( fontPair ) => (
|
|
|
|
<FontFamiliesLoaderDotCom
|
|
|
|
fontFamilies={ fontPair }
|
|
|
|
key={ fontPair
|
|
|
|
.map( ( font ) => font.slug )
|
|
|
|
.join( '-' ) }
|
|
|
|
preload
|
|
|
|
/>
|
|
|
|
) ) }
|
|
|
|
{ isNoAIFlow( context.flowType ) && (
|
2023-09-29 05:20:29 +00:00
|
|
|
<FontFamiliesLoader
|
2024-02-28 14:47:31 +00:00
|
|
|
fontFamilies={ [
|
|
|
|
...( enabledFontFamilies.custom ?? [] ),
|
|
|
|
...baseFontFamilies.theme,
|
|
|
|
] }
|
2024-02-14 09:30:18 +00:00
|
|
|
iframeInstance={ iframeInstance }
|
2023-09-29 05:20:29 +00:00
|
|
|
/>
|
2024-02-01 17:16:12 +00:00
|
|
|
) }
|
2023-09-29 05:20:29 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|