Skip to content
Merged
Show file tree
Hide file tree
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 @@ -30,7 +30,7 @@ import CollectionFontDetails from './collection-font-details';
import { toggleFont } from './utils/toggleFont';
import { getFontsOutline } from './utils/fonts-outline';
import GoogleFontsConfirmDialog from './google-fonts-confirm-dialog';
import { downloadFontFaceAsset } from './utils';
import { downloadFontFaceAssets } from './utils';

const DEFAULT_CATEGORY = {
slug: 'all',
Expand Down Expand Up @@ -161,7 +161,7 @@ function FontCollection( { slug } ) {
await Promise.all(
fontFamily.fontFace.map( async ( fontFace ) => {
if ( fontFace.src ) {
fontFace.file = await downloadFontFaceAsset(
fontFace.file = await downloadFontFaceAssets(
fontFace.src
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,24 @@ export function makeFontFacesFormData( font ) {
const fontFacesFormData = font.fontFace.map( ( face, faceIndex ) => {
const formData = new FormData();
if ( face.file ) {
// Slugified file name because the it might contain spaces or characters treated differently on the server.
const fileId = `file-${ faceIndex }`;
// Add the files to the formData
formData.append( fileId, face.file, face.file.name );
// remove the file object from the face object the file is referenced in src
const { file, ...faceWithoutFileProperty } = face;
const fontFaceSettings = {
...faceWithoutFileProperty,
src: fileId,
};
formData.append(
'font_face_settings',
JSON.stringify( fontFaceSettings )
);
// Normalize to an array, since face.file may be a single file or an array of files.
const files = Array.isArray( face.file )
? face.file
: [ face.file ];
const src = [];

files.forEach( ( file, key ) => {
// Slugified file name because the it might contain spaces or characters treated differently on the server.
const fileId = `file-${ faceIndex }-${ key }`;
// Add the files to the formData
formData.append( fileId, file, file.name );
src.push( fileId );
} );

face.src = src.length === 1 ? src[ 0 ] : src;
delete face.file;

formData.append( 'font_face_settings', JSON.stringify( face ) );
} else {
formData.append( 'font_face_settings', JSON.stringify( face ) );
}
Expand Down Expand Up @@ -225,31 +229,33 @@ export async function batchInstallFontFaces( fontFamilyId, fontFacesData ) {
/*
* 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;
export async function downloadFontFaceAssets( src ) {
// Normalize to an array, since `src` could be a string or array.
src = Array.isArray( src ) ? src : [ src ];

const files = await Promise.all(
Comment thread
creativecoder marked this conversation as resolved.
Resolved
src.map( async ( 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();
} )
Comment thread
creativecoder marked this conversation as resolved.
Resolved
.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;
} );
Comment thread
pbking marked this conversation as resolved.
Resolved
);

// If we only have one file return it (not the array). Otherwise return all of them in the array.
return files.length === 1 ? files[ 0 ] : files;
}

/*
Expand Down