Skip to content

Commit

Permalink
List all active fonts in the typography section (WordPress#65806)
Browse files Browse the repository at this point in the history
* list all active fonts

* sort active fonts alphabetically

also skip unnecessary sorts

* abstract font source mapping into function

---------

Co-authored-by: Vicente Canales <[email protected]>
  • Loading branch information
2 people authored and karthick-murugan committed Nov 13, 2024
1 parent 774abb9 commit 59a54af
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions packages/edit-site/src/components/global-styles/font-families.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ import { unlock } from '../../lock-unlock';

const { useGlobalSetting } = unlock( blockEditorPrivateApis );

/**
* Maps the fonts with the source, if available.
*
* @param {Array} fonts The fonts to map.
* @param {string} source The source of the fonts.
* @return {Array} The mapped fonts.
*/
function mapFontsWithSource( fonts, source ) {
return fonts
? fonts.map( ( f ) => setUIValuesNeeded( f, { source } ) )
: [];
}

function FontFamilies() {
const { baseCustomFonts, modalTabOpen, setModalTabOpen } =
useContext( FontLibraryContext );
Expand All @@ -34,18 +47,12 @@ function FontFamilies() {
undefined,
'base'
);
const themeFonts = fontFamilies?.theme
? fontFamilies.theme
.map( ( f ) => setUIValuesNeeded( f, { source: 'theme' } ) )
.sort( ( a, b ) => a.name.localeCompare( b.name ) )
: [];
const customFonts = fontFamilies?.custom
? fontFamilies.custom
.map( ( f ) => setUIValuesNeeded( f, { source: 'custom' } ) )
.sort( ( a, b ) => a.name.localeCompare( b.name ) )
: [];
const hasFonts = 0 < customFonts.length || 0 < themeFonts.length;

const themeFonts = mapFontsWithSource( fontFamilies?.theme, 'theme' );
const customFonts = mapFontsWithSource( fontFamilies?.custom, 'custom' );
const activeFonts = [ ...themeFonts, ...customFonts ].sort( ( a, b ) =>
a.name.localeCompare( b.name )
);
const hasFonts = 0 < activeFonts.length;
const hasInstalledFonts =
hasFonts ||
baseFontFamilies?.theme?.length > 0 ||
Expand All @@ -61,11 +68,11 @@ function FontFamilies() {
) }

<VStack spacing={ 4 }>
{ [ ...themeFonts, ...customFonts ].length > 0 && (
{ activeFonts.length > 0 && (
<>
<Subtitle level={ 3 }>{ __( 'Fonts' ) }</Subtitle>
<ItemGroup size="large" isBordered isSeparated>
{ themeFonts.map( ( font ) => (
{ activeFonts.map( ( font ) => (
<FontFamilyItem
key={ font.slug }
font={ font }
Expand Down

0 comments on commit 59a54af

Please sign in to comment.