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

Activate the fonts coming from the backend and not the data from the frontend #60093

Merged
merged 2 commits into from
Mar 26, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,13 @@ function FontLibraryProvider( { children } ) {
sucessfullyInstalledFontFaces?.length > 0 ||
alreadyInstalledFontFaces?.length > 0
) {
fontFamilyToInstall.fontFace = [
// Use font data from REST API not from client to ensure
// correct font information is used.
installedFontFamily.fontFace = [
matiasbenedetto marked this conversation as resolved.
Show resolved Hide resolved
...sucessfullyInstalledFontFaces,
...alreadyInstalledFontFaces,
];
fontFamiliesToActivate.push( fontFamilyToInstall );

fontFamiliesToActivate.push( installedFontFamily );
}

// If it's a system font but was installed successfully, activate it.
Expand Down Expand Up @@ -402,14 +404,29 @@ function FontLibraryProvider( { children } ) {
};

const activateCustomFontFamilies = ( fontsToAdd ) => {
// Merge the existing custom fonts with the new fonts.
// Removes the id from the families and faces to avoid saving that to global styles post content.
Copy link
Contributor

@getdave getdave Mar 26, 2024

Choose a reason for hiding this comment

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

I think we could explain here why we want to avoid saving to global styles post content.

Presumably the reason this code has been added now is because we're now using the font data from the API we're getting an id field which wasn't present previously.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, should explain.

Copy link
Contributor

Choose a reason for hiding this comment

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

Although I think we can do that in a follow up.

const fontsToActivate = fontsToAdd.map(
( { id: _familyDbId, fontFace, ...font } ) => ( {
...font,
...( fontFace && fontFace.length > 0
? {
fontFace: fontFace.map(
( { id: _faceDbId, ...face } ) => face
),
}
: {} ),
} )
);

// Activate the fonts by set the new custom fonts array.
setFontFamilies( {
...fontFamilies,
custom: mergeFontFamilies( fontFamilies?.custom, fontsToAdd ),
// Merge the existing custom fonts with the new fonts.
custom: mergeFontFamilies( fontFamilies?.custom, fontsToActivate ),
} );

// Add custom fonts to the browser.
fontsToAdd.forEach( ( font ) => {
fontsToActivate.forEach( ( font ) => {
if ( font.fontFace ) {
font.fontFace.forEach( ( face ) => {
// Load font faces just in the iframe because they already are in the document.
Expand Down
Loading