Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font Library: handle missing fonts and variants #56991

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading