Skip to content
Merged
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
1 change: 1 addition & 0 deletions packages/global-styles-ui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Enhancements

- Remove the dot that marks blocks with user styles in the block list. The filter to show only those blocks stays ([#84657](https://github.com/WordPress/gutenberg/pull/84657)).
- Font Library: Mark installed font variants in font collections ([#84023](https://github.com/WordPress/gutenberg/pull/84023)).

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
CheckboxControl as WCCheckboxControl,
Flex,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { Text } from '@wordpress/ui';
import { getFontFaceVariantName } from './utils';
import FontDemo from './font-demo';
import type { CollectionFontVariantProps } from './types';
Expand All @@ -12,8 +14,12 @@ function CollectionFontVariant( {
font,
handleToggleVariant,
selected,
installed,
}: CollectionFontVariantProps ) {
const handleToggleActivation = () => {
if ( installed ) {
return;
}
if ( font?.fontFace ) {
handleToggleVariant( font, face );
return;
Expand All @@ -23,22 +29,35 @@ function CollectionFontVariant( {

const displayName = font.name + ' ' + getFontFaceVariantName( face );
const checkboxId = useId();
const installedId = `${ checkboxId }-installed`;

return (
<div className="font-library__font-card">
<Flex justify="flex-start" align="center" gap="1rem">
<WCCheckboxControl
checked={ selected }
onChange={ handleToggleActivation }
id={ checkboxId }
/>
<label htmlFor={ checkboxId }>
<FontDemo
font={ face }
text={ displayName }
onClick={ handleToggleActivation }
<Flex justify="space-between" align="center" gap="1rem">
<Flex justify="flex-start" align="center" gap="1rem">
<WCCheckboxControl
checked={ selected || installed }
disabled={ installed }
onChange={ handleToggleActivation }
id={ checkboxId }
aria-describedby={ installed ? installedId : undefined }
/>
</label>
<label htmlFor={ checkboxId }>
<FontDemo
font={ face }
text={ displayName }
onClick={ handleToggleActivation }
/>
</label>
</Flex>
{ installed && (
<Text
id={ installedId }
className="font-library__font-card__count"
>
{ __( 'Installed' ) }
</Text>
) }
</Flex>
</div>
);
Expand Down
46 changes: 38 additions & 8 deletions packages/global-styles-ui/src/font-library/font-collection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ function FontCollection( { slug }: { slug: string } ) {
const [ renderConfirmDialog, setRenderConfirmDialog ] = useState(
requiresPermission && ! getGoogleFontsPermissionFromStorage()
);
const { installFonts, isInstalling } = useContext( FontLibraryContext );
const { installFonts, isInstalling, baseCustomFonts } =
useContext( FontLibraryContext );
const { record: selectedCollection, isResolving: isLoading } =
useEntityRecord< FontCollectionType >( 'root', 'fontCollection', slug );

Expand Down Expand Up @@ -162,6 +163,29 @@ function FontCollection( { slug }: { slug: string } ) {
};

const fontToInstallOutline = getFontsOutline( fontsToInstall );
const installedFontsOutline = getFontsOutline( baseCustomFonts );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

baseCustomFonts only contains the default page of 10 installed families, so variants from omitted families still appear uninstalled and remain selectable. Could we load all installed families before using this list for the installed-state checks?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! I've opened a separate PR for this: #84684


const isFontFaceInstalled = ( face: FontFace ) =>
!! selectedFont &&
isFontFontFaceInOutline(
selectedFont.slug,
selectedFont.fontFace ? face : null, // If the font has no fontFace, we want to check if the font is installed
installedFontsOutline
);

const installableFontFaces = selectedFont?.fontFace?.filter(
( face ) => ! isFontFaceInstalled( face )
);

const isAllInstalled =
!! selectedFont &&
( selectedFont.fontFace?.length
? installableFontFaces?.length === 0
: isFontFontFaceInOutline(
selectedFont.slug,
null,
installedFontsOutline
) );

const resetFontsToInstall = () => {
setFontsToInstall( [] );
Expand All @@ -174,18 +198,20 @@ function FontCollection( { slug }: { slug: string } ) {

// Check if any fonts are selected.
const isIndeterminate =
selectFontCount > 0 &&
selectFontCount !== selectedFont?.fontFace?.length;
selectFontCount > 0 && selectFontCount !== installableFontFaces?.length;

// Check if all fonts are selected.
const isSelectAllChecked =
selectFontCount === selectedFont?.fontFace?.length;
// Check if all installable fonts are selected.
const isSelectAllChecked = selectFontCount === installableFontFaces?.length;

// Toggle select all fonts.
const toggleSelectAll = () => {
const newFonts: FontFamily[] = [];
if ( ! isSelectAllChecked && selectedFont ) {
newFonts.push( selectedFont );
newFonts.push(
selectedFont.fontFace
? { ...selectedFont, fontFace: installableFontFaces }
: selectedFont
);
}

setFontsToInstall( newFonts );
Expand Down Expand Up @@ -440,9 +466,10 @@ function FontCollection( { slug }: { slug: string } ) {
<WCCheckboxControl
className="font-library__select-all"
label={ __( 'Select all' ) }
checked={ isSelectAllChecked }
checked={ isSelectAllChecked || isAllInstalled }
onChange={ toggleSelectAll }
indeterminate={ isIndeterminate }
disabled={ isAllInstalled }
/>
<VStack spacing={ 0 }>
{ /*
Expand Down Expand Up @@ -474,6 +501,9 @@ function FontCollection( { slug }: { slug: string } ) {
: null, // If the font has no fontFace, we want to check if the font is in the outline
fontToInstallOutline
) }
installed={ isFontFaceInstalled(
face
) }
/>
</li>
)
Expand Down
1 change: 1 addition & 0 deletions packages/global-styles-ui/src/font-library/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export interface CollectionFontVariantProps {
font: FontFamily;
handleToggleVariant: ( font: FontFamily, face?: FontFace ) => void;
selected: boolean;
installed: boolean;
}

export interface FontUploadResult {
Expand Down
Loading