Skip to content

Commit

Permalink
Font Library: Include a "Select All" options to activate/deactivate a…
Browse files Browse the repository at this point in the history
…ll fonts (#63531)

* Add function to activate all fonts on select all click

* implement toggle to select all fonts

* add style for select all option

* Add style for select all lable

* Removed unneccessory use of flex component

* Remove new function added to context

* Modified flow of select all toggle function

* Removed extra spaces

* Remove use of context

* Update logic to choose all fonts on click in the indeterminate condition

* Fix the issue of select all toggle for fonts without font faces

* Remove ad hoc font style for select all label

* Updated styles for select all toggle's checkbox to align with other checkboxes

Co-authored-by: akasunil <sunil25393@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: jameskoster <jameskoster@git.wordpress.org>
Co-authored-by: jasmussen <joen@git.wordpress.org>
Co-authored-by: annezazu <annezazu@git.wordpress.org>
Co-authored-by: creativecoder <grantmkin@git.wordpress.org>
Co-authored-by: beafialho <beafialho@git.wordpress.org>
  • Loading branch information
8 people authored Jul 24, 2024
1 parent e05fbc2 commit a15e048
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Flex,
Notice,
ProgressBar,
CheckboxControl,
} from '@wordpress/components';
import { useEntityRecord, store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
Expand All @@ -31,7 +32,12 @@ import { FontLibraryContext } from './context';
import FontCard from './font-card';
import LibraryFontVariant from './library-font-variant';
import { sortFontFaces } from './utils/sort-font-faces';
import { setUIValuesNeeded } from './utils';
import {
setUIValuesNeeded,
loadFontFaceInBrowser,
unloadFontFaceInBrowser,
getDisplaySrcFromFontFace,
} from './utils';
import { unlock } from '../../../lock-unlock';

const { useGlobalSetting } = unlock( blockEditorPrivateApis );
Expand All @@ -49,8 +55,11 @@ function InstalledFonts() {
getFontFacesActivated,
notice,
setNotice,
fontFamilies,
} = useContext( FontLibraryContext );

const [ fontFamilies, setFontFamilies ] = useGlobalSetting(
'typography.fontFamilies'
);
const [ isConfirmDeleteOpen, setIsConfirmDeleteOpen ] = useState( false );
const [ baseFontFamilies ] = useGlobalSetting(
'typography.fontFamilies',
Expand All @@ -61,7 +70,6 @@ function InstalledFonts() {
const { __experimentalGetCurrentGlobalStylesId } = select( coreStore );
return __experimentalGetCurrentGlobalStylesId();
} );

const globalStyles = useEntityRecord(
'root',
'globalStyles',
Expand Down Expand Up @@ -148,6 +156,55 @@ function InstalledFonts() {
refreshLibrary();
}, [] );

// Get activated fonts count.
const activeFontsCount = libraryFontSelected
? getFontFacesActivated(
libraryFontSelected.slug,
libraryFontSelected.source
).length
: 0;

const selectedFontsCount =
libraryFontSelected?.fontFace?.length ??
( libraryFontSelected?.fontFamily ? 1 : 0 );

// Check if any fonts are selected.
const isIndeterminate =
activeFontsCount > 0 && activeFontsCount !== selectedFontsCount;

// Check if all fonts are selected.
const isSelectAllChecked = activeFontsCount === selectedFontsCount;

// Toggle select all fonts.
const toggleSelectAll = () => {
const initialFonts =
fontFamilies?.[ libraryFontSelected.source ]?.filter(
( f ) => f.slug !== libraryFontSelected.slug
) ?? [];
const newFonts = isSelectAllChecked
? initialFonts
: [ ...initialFonts, libraryFontSelected ];

setFontFamilies( {
...fontFamilies,
[ libraryFontSelected.source ]: newFonts,
} );

if ( libraryFontSelected.fontFace ) {
libraryFontSelected.fontFace.forEach( ( face ) => {
if ( isSelectAllChecked ) {
unloadFontFaceInBrowser( face, 'all' );
} else {
loadFontFaceInBrowser(
face,
getDisplaySrcFromFontFace( face?.src ),
'all'
);
}
} );
}
};

const hasFonts = baseThemeFonts.length > 0 || baseCustomFonts.length > 0;
return (
<div className="font-library-modal__tabpanel-layout">
Expand Down Expand Up @@ -311,6 +368,14 @@ function InstalledFonts() {
</Text>
<Spacer margin={ 4 } />
<VStack spacing={ 0 }>
<CheckboxControl
className="font-library-modal__select-all"
label={ __( 'Select all' ) }
checked={ isSelectAllChecked }
onChange={ toggleSelectAll }
indeterminate={ isIndeterminate }
__nextHasNoMarginBottom
/>
<Spacer margin={ 8 } />
{ getFontFacesToDisplay(
libraryFontSelected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,12 @@ button.font-library-modal__upload-area {
justify-content: center;
}
}

.font-library-modal__select-all {
padding: $grid-unit-20 $grid-unit-20 $grid-unit-20 $grid-unit-20 + $border-width;

.components-checkbox-control__label {
padding-left: $grid-unit-20;
}
}

1 comment on commit a15e048

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in a15e048.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/10073780607
📝 Reported issues:

Please sign in to comment.