Skip to content

Commit

Permalink
Download then upload font face assets when installing from a collecti…
Browse files Browse the repository at this point in the history
…on (#57694)

* Download then upload font face assets when installing from a collection

* Add error handling to font download-then-upload

* Check for downloadFromUrl property before downloading to upload

* Simplify downloadFontFaceAsset and move to utils.

* Add error handling to downloadFontFaceAsset

* Use Promise.all to handle multiple asynchronous font face downloads.
>
>
Co-authored-by: Sarah Norris <[email protected]>

---------

Co-authored-by: Jeff Ong <[email protected]>
Co-authored-by: Sarah Norris <[email protected]>
  • Loading branch information
3 people authored Jan 11, 2024
1 parent 4f97a3f commit 50ac86a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { toggleFont } from './utils/toggleFont';
import { getFontsOutline } from './utils/fonts-outline';
import GoogleFontsConfirmDialog from './google-fonts-confirm-dialog';
import { getNoticeFromInstallResponse } from './utils/get-notice-from-response';
import { downloadFontFaceAsset } from './utils';

const DEFAULT_CATEGORY = {
id: 'all',
Expand Down Expand Up @@ -154,7 +155,34 @@ function FontCollection( { id } ) {
};

const handleInstall = async () => {
const response = await installFont( fontsToInstall[ 0 ] );
const fontFamily = fontsToInstall[ 0 ];

try {
if ( fontFamily?.fontFace ) {
await Promise.all(
fontFamily.fontFace.map( async ( fontFace ) => {
if ( fontFace.downloadFromUrl ) {
fontFace.file = await downloadFontFaceAsset(
fontFace.downloadFromUrl
);
delete fontFace.downloadFromUrl;
}
} )
);
}
} catch ( error ) {
// If any of the fonts fail to download,
// show an error notice and stop the request from being sent.
setNotice( {
type: 'error',
message: __(
'Error installing the fonts, could not be downloaded.'
),
} );
return;
}

const response = await installFont( fontFamily );
const installNotice = getNoticeFromInstallResponse( response );
setNotice( installNotice );
resetFontsToInstall();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { privateApis as componentsPrivateApis } from '@wordpress/components';
import { FONT_WEIGHTS, FONT_STYLES } from './constants';
import { unlock } from '../../../../lock-unlock';

/**
* Browser dependencies
*/
const { File } = window;

export function setUIValuesNeeded( font, extraValues = {} ) {
if ( ! font.name && ( font.fontFamily || font.slug ) ) {
font.name = font.fontFamily || font.slug;
Expand Down Expand Up @@ -164,3 +169,33 @@ export function makeFormDataFromFontFamily( fontFamily ) {
formData.append( 'font_family_settings', JSON.stringify( newFontFamily ) );
return formData;
}

/*
* Downloads a font face asset from a URL to the client and returns a File object.
*/
export async function downloadFontFaceAsset( url ) {
return fetch( new Request( url ) )
.then( ( response ) => {
if ( ! response.ok ) {
throw new Error(
`Error downloading font face asset from ${ url }. Server responded with status: ${ response.status }`
);
}
return response.blob();
} )
.then( ( blob ) => {
const filename = url.split( '/' ).pop();
const file = new File( [ blob ], filename, {
type: blob.type,
} );
return file;
} )
.catch( ( error ) => {
// eslint-disable-next-line no-console
console.error(
`Error downloading font face asset from ${ url }:`,
error
);
throw error;
} );
}

0 comments on commit 50ac86a

Please sign in to comment.