Skip to content

Commit

Permalink
Identify missing font faces.
Browse files Browse the repository at this point in the history
  • Loading branch information
jffng committed Dec 12, 2023
1 parent 93349ff commit bfb552c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,47 @@ function FontLibraryProvider( { children } ) {
);
};

function getActivatedNotInstalledFonts() {
return customFonts
.map( ( customFont ) => {
// Ensure fontFaces is an array in customFont
const customFontFaces = Array.isArray( customFont.fontFace )
? customFont.fontFace
: [];

// Find the corresponding font in baseCustomFonts
const baseFont = baseCustomFonts.find(
( base ) => base.slug === customFont.slug
);

// Ensure fontFaces is an array in baseFont, if baseFont exists
const baseFontFaces =
baseFont && Array.isArray( baseFont.fontFace )
? baseFont.fontFace
: [];

// Filter out the font faces that are not installed
const fontFacesNotInstalled = customFontFaces.filter(
( customFace ) => {
// If the font isn't found in baseCustomFonts, all its faces are considered not installed
if ( ! baseFont ) return true;

// Check if the font face is not present in the installed font's faces
return ! baseFontFaces.some(
( baseFace ) => baseFace === customFace
);
}
);

// Return the font object with only the non-installed font faces, while copying over all other properties
return {
...customFont,
fontFaces: fontFacesNotInstalled,
};
} )
.filter( ( font ) => font.fontFaces && font.fontFaces.length > 0 ); // Filter out fonts that have no non-installed faces
}

const getFontFacesActivated = ( slug, source ) => {
return getActivatedFontsOutline( source )[ slug ] || [];
};
Expand Down Expand Up @@ -372,6 +413,7 @@ function FontLibraryProvider( { children } ) {
isInstalling,
collections,
getFontCollection,
getActivatedNotInstalledFonts,
} }
>
{ children }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function InstalledFonts() {
refreshLibrary,
uninstallFont,
isResolvingLibrary,
getActivatedNotInstalledFonts,
} = useContext( FontLibraryContext );
const [ isConfirmDeleteOpen, setIsConfirmDeleteOpen ] = useState( false );

Expand Down Expand Up @@ -76,6 +77,8 @@ function InstalledFonts() {
const shouldDisplayDeleteButton =
!! libraryFontSelected && libraryFontSelected?.source !== 'theme';

const activatedNotInstalledFonts = getActivatedNotInstalledFonts();

useEffect( () => {
refreshLibrary();
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -129,10 +132,27 @@ function InstalledFonts() {
{ ! libraryFontSelected && (
<>
{ isResolvingLibrary && <Spinner /> }
{ activatedNotInstalledFonts.length > 0 && (
<>
<Spacer margin={ 2 } />
<FontsGrid title="Fonts missing variants">
{ activatedNotInstalledFonts.map( ( font ) => (
<LibraryFontCard
font={ font }
key={ font.slug }
onClick={ () => {
handleSelectFont( font );
} }
/>
) ) }
</FontsGrid>
<Spacer margin={ 8 } />
</>
) }
{ baseCustomFonts.length > 0 && (
<>
<Spacer margin={ 2 } />
<FontsGrid>
<FontsGrid title="Custom Fonts">
{ baseCustomFonts.map( ( font ) => (
<LibraryFontCard
font={ font }
Expand Down

0 comments on commit bfb552c

Please sign in to comment.