From a15e048dccf3b4b9325a5349a8ef3233e35bb537 Mon Sep 17 00:00:00 2001 From: Sunil Prajapati <61308756+akasunil@users.noreply.github.com> Date: Wed, 24 Jul 2024 14:36:53 +0530 Subject: [PATCH] Font Library: Include a "Select All" options to activate/deactivate all 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 Co-authored-by: t-hamano Co-authored-by: jameskoster Co-authored-by: jasmussen Co-authored-by: annezazu Co-authored-by: creativecoder Co-authored-by: beafialho --- .../font-library-modal/installed-fonts.js | 71 ++++++++++++++++++- .../font-library-modal/style.scss | 9 +++ 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js b/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js index 76618a54aeb92..6602a778dc66c 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/installed-fonts.js @@ -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'; @@ -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 ); @@ -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', @@ -61,7 +70,6 @@ function InstalledFonts() { const { __experimentalGetCurrentGlobalStylesId } = select( coreStore ); return __experimentalGetCurrentGlobalStylesId(); } ); - const globalStyles = useEntityRecord( 'root', 'globalStyles', @@ -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 (
@@ -311,6 +368,14 @@ function InstalledFonts() { + { getFontFacesToDisplay( libraryFontSelected diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/style.scss b/packages/edit-site/src/components/global-styles/font-library-modal/style.scss index 5b9b595a1e647..5cbb53a6296cc 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/style.scss +++ b/packages/edit-site/src/components/global-styles/font-library-modal/style.scss @@ -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; + } +} +