Skip to content

Commit

Permalink
Fix list of base theme fonts when a theme variation is applied. (Word…
Browse files Browse the repository at this point in the history
…Press#59959)

* use globalStyles + theme.json base files to render a list of avialble fonts in the font library modal. Avoid fonts with duplicated slugs.

* add comments

* improve comment

* fix typo in comment

Co-authored-by: Grant Kinney <creativecoder@users.noreply.github.com>

---------

Co-authored-by: matiasbenedetto <mmaattiiaass@git.wordpress.org>
Co-authored-by: getdave <get_dave@git.wordpress.org>
Co-authored-by: scruffian <scruffian@git.wordpress.org>
Co-authored-by: mikachan <mikachan@git.wordpress.org>
Co-authored-by: creativecoder <grantmkin@git.wordpress.org>
  • Loading branch information
6 people authored and carstingaxion committed Mar 27, 2024
1 parent 1bf2154 commit 5ac7c2b
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,31 @@ function FontLibraryProvider( { children } ) {
const [ modalTabOpen, setModalTabOpen ] = useState( false );
const [ libraryFontSelected, setLibraryFontSelected ] = useState( null );

const baseThemeFonts = baseFontFamilies?.theme
? baseFontFamilies.theme
.map( ( f ) => setUIValuesNeeded( f, { source: 'theme' } ) )
.sort( ( a, b ) => a.name.localeCompare( b.name ) )
: [];

// Themes Fonts are the fonts defined in the global styles (database persisted theme.json data).
const themeFonts = fontFamilies?.theme
? fontFamilies.theme
.map( ( f ) => setUIValuesNeeded( f, { source: 'theme' } ) )
.sort( ( a, b ) => a.name.localeCompare( b.name ) )
: [];

const themeFontsSlugs = new Set( themeFonts.map( ( f ) => f.slug ) );

/*
* Base Theme Fonts are the fonts defined in the theme.json *file*.
*
* Uses the fonts from global styles + the ones from the theme.json file that hasn't repeated slugs.
* Avoids incosistencies with the fonts listed in the font library modal as base (unactivated).
* These inconsistencies can happen when the active theme fonts in global styles aren't defined in theme.json file as when a theme style variation is applied.
*/
const baseThemeFonts = baseFontFamilies?.theme
? themeFonts.concat(
baseFontFamilies.theme
.filter( ( f ) => ! themeFontsSlugs.has( f.slug ) )
.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' } ) )
Expand Down Expand Up @@ -144,8 +157,7 @@ function FontLibraryProvider( { children } ) {
return;
}

const fonts =
font.source === 'theme' ? baseThemeFonts : baseCustomFonts;
const fonts = font.source === 'theme' ? themeFonts : baseCustomFonts;

// Tries to find the font in the installed fonts
const fontSelected = fonts.find( ( f ) => f.slug === font.slug );
Expand Down

0 comments on commit 5ac7c2b

Please sign in to comment.