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: Font Collections frontend #54566

Merged
merged 44 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
321e3fe
remove wrong comment around code merge
matiasbenedetto Sep 18, 2023
ac34108
registering google fonts collection
matiasbenedetto Sep 18, 2023
bd6fdc9
registering google fonts collection
matiasbenedetto Sep 18, 2023
95fe38c
TEMPORAL: Adding Google Fonts list data as JSON file
matiasbenedetto Sep 18, 2023
5467492
removing unused constant
matiasbenedetto Sep 18, 2023
48df0cd
add data resolvers to fetch font collections
matiasbenedetto Sep 18, 2023
6c22d1d
fetching collections data from context
matiasbenedetto Sep 18, 2023
1c5d611
adding collections tabs in font library modal
matiasbenedetto Sep 18, 2023
5710b38
refactor google-fonts categories
matiasbenedetto Sep 18, 2023
097250d
add filter fonts functions
matiasbenedetto Sep 18, 2023
c459fa9
add filterFonts functions tests
matiasbenedetto Sep 18, 2023
818d5a5
move actionHandler into FontCard component
matiasbenedetto Sep 18, 2023
5be0281
adding ui filters for font collections
matiasbenedetto Sep 18, 2023
4964fc4
add font collections selected variants indicator
matiasbenedetto Sep 19, 2023
414a09c
add tests for fonts-outline functions
matiasbenedetto Sep 19, 2023
e864d96
install fonts
matiasbenedetto Sep 19, 2023
1d8b29c
fixing the css font family name in the previews to handle font names …
matiasbenedetto Sep 19, 2023
300aba2
add tests for previews styles functions
matiasbenedetto Sep 19, 2023
07a5fbf
change value used for refresh font library posts
matiasbenedetto Sep 19, 2023
19ee4d6
removing not needed prefix on slug
matiasbenedetto Sep 19, 2023
bab14db
updating google fonts data file with previews
matiasbenedetto Sep 19, 2023
0cdabf8
loading svg previews
matiasbenedetto Sep 19, 2023
a05da99
fixing categories in google fonts json file
matiasbenedetto Sep 19, 2023
032c1eb
format php
matiasbenedetto Sep 19, 2023
5417b77
adding google fonts confirmation
matiasbenedetto Sep 19, 2023
26f09a7
Removing ordering because it is not needed here. Use the default orde…
matiasbenedetto Sep 19, 2023
caf9557
updating google fonts preview urls to load from the new repo
matiasbenedetto Sep 19, 2023
0919ba3
update wording
matiasbenedetto Sep 20, 2023
e37a6bb
update wording
matiasbenedetto Sep 20, 2023
7d5e01a
update wording
matiasbenedetto Sep 20, 2023
2b2f0a3
changing src by downloadFromUrl
matiasbenedetto Sep 20, 2023
27829d9
use window.localStorage instead of localStorage
matiasbenedetto Sep 20, 2023
307b70a
fixing font collection getData test
matiasbenedetto Sep 20, 2023
241f9b7
refactor getFontCollections php unit tests to the the default font co…
matiasbenedetto Sep 20, 2023
742de9f
renaming Google Fonts to Default Font Collection
matiasbenedetto Sep 20, 2023
77b7005
adding font name to tab title when a font is selected
matiasbenedetto Sep 20, 2023
a973add
replace button deprecated prop
matiasbenedetto Sep 20, 2023
39d86ad
implement a fontface sort function
matiasbenedetto Sep 20, 2023
507a8a9
adding unit tests for fontface sorting function
matiasbenedetto Sep 20, 2023
375b1b9
use fontface sorting'
matiasbenedetto Sep 20, 2023
754e0b4
use Google fonts as tab title
matiasbenedetto Sep 20, 2023
dbfd080
format php
matiasbenedetto Sep 20, 2023
02bd1b9
revert unwanted changes
matiasbenedetto Sep 20, 2023
a43d445
add translation domain
matiasbenedetto Sep 20, 2023
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 @@ -56,10 +56,10 @@
foreach ( $settings['typography']['fontFamilies'] as $font_families ) {
foreach ( $font_families as $definition ) {

// Skip if font-family "name" is not defined.
if ( empty( $definition['name'] ) ) {
continue;
}
// // Skip if font-family "name" is not defined.
// if ( empty( $definition['name'] ) ) {
// continue;

Check failure on line 61 in lib/compat/wordpress-6.4/fonts/font-face/class-wp-font-face-resolver.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Spaces must be used for mid-line alignment; tabs are not allowed

Check failure on line 61 in lib/compat/wordpress-6.4/fonts/font-face/class-wp-font-face-resolver.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space before comment text but found 2; use block comment if you need indentation
// }

// Skip if "fontFace" is not defined, meaning there are no variations.
if ( empty( $definition['fontFace'] ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function get_data() {
return new WP_Error( 'font_collection_file_error', __( 'Font Collection data JSON file does not exist.', 'gutenberg' ) );
}

$data = file_get_contents( $this->config['data_json_file'] );
$data = wp_json_file_decode( $this->config['data_json_file'] );
matiasbenedetto marked this conversation as resolved.
Show resolved Hide resolved
if ( empty( $data ) ) {
return new WP_Error( 'font_collection_read_error', __( 'Error reading the Font Collection data JSON file contents.', 'gutenberg' ) );
}
Expand Down
10 changes: 9 additions & 1 deletion lib/experimental/fonts/font-library/font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,18 @@
}
}

// @core-merge: This code needs to be removed.
add_action(
'enqueue_block_editor_assets',
function () {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalFontLibrary = true', 'before' );
}
);

$default_font_collection = array(
matiasbenedetto marked this conversation as resolved.
Show resolved Hide resolved
'id' => 'google-fonts',
'name' => 'Google Fonts',
'description' => __( 'Add from Google Fonts. Fonts are copied to and serverd, from your server.' ),
matiasbenedetto marked this conversation as resolved.
Show resolved Hide resolved
'data_json_file' => path_join( __DIR__, 'google-fonts.json' ),

Check warning on line 70 in lib/experimental/fonts/font-library/font-library.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Missing $domain parameter in function call to __(). If this text string is supposed to use a WP Core translation, use the "default" text domain.
matiasbenedetto marked this conversation as resolved.
Show resolved Hide resolved
);

wp_register_font_collection( $default_font_collection );
55,714 changes: 55,714 additions & 0 deletions lib/experimental/fonts/font-library/google-fonts.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useContext } from '@wordpress/element';
* Internal dependencies
*/
import { FontLibraryContext } from './font-library-modal/context';
import { getPreviewStyle } from './font-library-modal/utils';
import { getFamilyPreviewStyle } from './font-library-modal/utils/preview-styles';

function FontFamilyItem( { font } ) {
const { handleSetLibraryFontSelected, toggleModal } =
Expand All @@ -26,7 +26,7 @@ function FontFamilyItem( { font } ) {
toggleModal( 'installed-fonts' );
};

const previewStyle = getPreviewStyle( font );
const previewStyle = getFamilyPreviewStyle( font );

return (
<Item onClick={ handleClick }>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* WordPress dependencies
*/
import {
__experimentalVStack as VStack,
__experimentalSpacer as Spacer,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import CollectionFontVariant from './collection-font-variant';
import { isFontFontFaceInOutline } from './utils/fonts-outline';

function CollectionFontDetails( {
font,
handleToggleVariant,
fontToInstallOutline,
} ) {
const fontFaces =
font.fontFace && font.fontFace.length
? font.fontFace
: [
{
fontFamily: font.fontFamily,
fontStyle: 'normal',
fontWeight: '400',
},
];

return (
<>
<Spacer margin={ 4 } />
<VStack spacing={ 0 }>
<Spacer margin={ 8 } />
{ fontFaces.map( ( face, i ) => (
<CollectionFontVariant
font={ font }
face={ face }
key={ `face${ i }` }
handleToggleVariant={ handleToggleVariant }
selected={ isFontFontFaceInOutline(
font.slug,
face,
fontToInstallOutline
) }
/>
) ) }
</VStack>
<Spacer margin={ 8 } />
</>
);
}

export default CollectionFontDetails;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* WordPress dependencies
*/
import { CheckboxControl, Flex } from '@wordpress/components';
/**
* Internal dependencies
*/
import { getFontFaceVariantName } from './utils';

/**
* Internal dependencies
*/
import FontFaceDemo from './font-demo';

function CollectionFontVariant( {
face,
font,
handleToggleVariant,
selected,
} ) {
const handleToggleActivation = () => {
if ( font?.fontFace ) {
handleToggleVariant( font, face );
return;
}
handleToggleVariant( font );
};

const displayName = font.name + ' ' + getFontFaceVariantName( face );

return (
<div className="font-library-modal__library-font-variant">
<Flex justify="space-between" align="center" gap="1rem">
<FontFaceDemo fontFace={ face } text={ displayName } />
<CheckboxControl
checked={ selected }
onChange={ handleToggleActivation }
__nextHasNoMarginBottom={ true }
/>
</Flex>
</div>
);
}

export default CollectionFontVariant;
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { fetchInstallFonts, fetchUninstallFonts } from './resolvers';
import {
fetchInstallFonts,
fetchUninstallFonts,
fetchFontCollections,
fetchFontCollection,
} from './resolvers';
import { unlock } from '../../../lock-unlock';
const { useGlobalSetting } = unlock( blockEditorPrivateApis );
import {
Expand Down Expand Up @@ -52,7 +57,7 @@ function FontLibraryProvider( { children } ) {
const [ refreshKey, setRefreshKey ] = useState( 0 );

const refreshLibrary = () => {
setRefreshKey( ( prevKey ) => prevKey + 1 );
setRefreshKey( Date.now() );
};

const {
Expand Down Expand Up @@ -313,6 +318,30 @@ function FontLibraryProvider( { children } ) {
loadedFontUrls.add( src );
};

// Font Collections
const [ collections, setFontCollections ] = useState( [] );
const getFontCollections = async () => {
const response = await fetchFontCollections();
setFontCollections( response );
};
const getFontCollection = async ( id ) => {
const hasData = !! collections.find(
( collection ) => collection.id === id
)?.data;
if ( hasData ) return;
const response = await fetchFontCollection( id );
const updatedCollections = collections.map( ( collection ) =>
collection.id === id
? { ...collection, data: { ...response?.data } }
: collection
);
setFontCollections( updatedCollections );
};

useEffect( () => {
getFontCollections();
}, [] );

return (
<FontLibraryContext.Provider
value={ {
Expand All @@ -337,6 +366,8 @@ function FontLibraryProvider( { children } ) {
isResolvingLibrary,
hasResolvedLibrary,
isInstalling,
collections,
getFontCollection,
} }
>
{ children }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import {
Button,
Flex,
FlexItem,
Icon,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import FontDemo from './font-demo';
import { getPreviewStyle } from './utils';
import { getFamilyPreviewStyle } from './utils/preview-styles';
import { chevronRight } from '@wordpress/icons';

function FontCard( { font, onClick, actionHandler, variantsText } ) {
function FontCard( { font, onClick, variantsText } ) {
const fakeFontFace = {
fontStyle: 'normal',
fontWeight: '400',
Expand All @@ -31,7 +33,7 @@ function FontCard( { font, onClick, actionHandler, variantsText } ) {
) || font.fontFace[ 0 ]
: fakeFontFace;

const demoStyle = getPreviewStyle( font );
const demoStyle = getFamilyPreviewStyle( font );

const variantsCount = font.fontFace?.length || 1;

Expand All @@ -47,6 +49,7 @@ function FontCard( { font, onClick, actionHandler, variantsText } ) {
>
<Flex justify="space-between" wrap={ false }>
<FontDemo
customPreviewUrl={ font.preview }
fontFace={ displayFontFace }
text={ font.name }
style={ demoStyle }
Expand All @@ -60,7 +63,9 @@ function FontCard( { font, onClick, actionHandler, variantsText } ) {
_n( 'variant', 'variants', variantsCount ) }
</Text>
</FlexItem>
<FlexItem>{ !! actionHandler && actionHandler }</FlexItem>
<FlexItem>
<Icon icon={ chevronRight } />
</FlexItem>
</Flex>
</Flex>
</Button>
Expand Down
Loading
Loading