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: Include a "Select All" options to activate/deactivate all fonts #63531

Merged
merged 16 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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 @@ -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'
);
}
} );
}
};

return (
<div className="font-library-modal__tabpanel-layout">
{ isResolvingLibrary && (
Expand Down Expand Up @@ -305,6 +362,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,13 @@ button.font-library-modal__upload-area {
justify-content: center;
}
}

.font-library-modal__select-all {
width: 100%;
height: auto;
padding: $grid-unit-20;

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

@t-hamano t-hamano Jul 24, 2024

Choose a reason for hiding this comment

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

Suggested change
.font-library-modal__select-all {
width: 100%;
height: auto;
padding: $grid-unit-20;
.components-checkbox-control__label {
padding-left: $grid-unit-20;
}
}
.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;
}
}

checkbox

To properly align the vertical position of the checkboxes, let's add a 1px border width to the left side.

I've also removed styles that we don't need.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch!

Loading