diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/font-collection.js b/packages/edit-site/src/components/global-styles/font-library-modal/font-collection.js index 46363365363bb..acbd3665b4c3e 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/font-collection.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/font-collection.js @@ -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', @@ -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 ); } diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/utils/index.js b/packages/edit-site/src/components/global-styles/font-library-modal/utils/index.js index 677b04f7e83d4..154ed8ac2cf8f 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/utils/index.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/utils/index.js @@ -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 ) ); } @@ -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( + 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(); + } ) + .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; - } ); + ); + + // 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; } /*