-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Font Library: refactor client side install functions to work with revised API #57844
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
Changes from all commits
9facbad
3c65771
2ba4fc4
73bd4d9
c251a31
94b32c5
ac9f29d
6648afc
1c0f335
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import { privateApis as componentsPrivateApis } from '@wordpress/components'; | |
| */ | ||
| import { FONT_WEIGHTS, FONT_STYLES } from './constants'; | ||
| import { unlock } from '../../../../lock-unlock'; | ||
| import { fetchInstallFontFace } from '../resolvers'; | ||
|
|
||
| /** | ||
| * Browser dependencies | ||
|
|
@@ -135,39 +136,84 @@ export function getDisplaySrcFromFontFace( input, urlPrefix ) { | |
| return src; | ||
| } | ||
|
|
||
| export function makeFormDataFromFontFamily( fontFamily ) { | ||
| export function makeFontFamilyFormData( fontFamily ) { | ||
| const formData = new FormData(); | ||
| const { kebabCase } = unlock( componentsPrivateApis ); | ||
|
|
||
| const newFontFamily = { | ||
| ...fontFamily, | ||
| const { fontFace, category, ...familyWithValidParameters } = fontFamily; | ||
|
||
| const fontFamilySettings = { | ||
| ...familyWithValidParameters, | ||
| slug: kebabCase( fontFamily.slug ), | ||
| }; | ||
|
|
||
| if ( newFontFamily?.fontFace ) { | ||
| const newFontFaces = newFontFamily.fontFace.map( | ||
| ( face, faceIndex ) => { | ||
| 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 by the uploadedFile key | ||
| const { file, ...faceWithoutFileProperty } = face; | ||
| const newFace = { | ||
| ...faceWithoutFileProperty, | ||
| uploadedFile: fileId, | ||
| }; | ||
| return newFace; | ||
| } | ||
| return face; | ||
| formData.append( | ||
| 'font_family_settings', | ||
| JSON.stringify( fontFamilySettings ) | ||
| ); | ||
| return formData; | ||
| } | ||
|
|
||
| export function makeFontFacesFormData( font ) { | ||
| if ( font?.fontFace ) { | ||
| 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 ) | ||
| ); | ||
| } else { | ||
| formData.append( 'font_face_settings', JSON.stringify( face ) ); | ||
| } | ||
| ); | ||
| newFontFamily.fontFace = newFontFaces; | ||
| return formData; | ||
| } ); | ||
|
|
||
| return fontFacesFormData; | ||
| } | ||
| } | ||
|
|
||
| formData.append( 'font_family_settings', JSON.stringify( newFontFamily ) ); | ||
| return formData; | ||
| export async function batchInstallFontFaces( fontFamilyId, fontFacesData ) { | ||
| const promises = fontFacesData.map( ( faceData ) => | ||
| fetchInstallFontFace( fontFamilyId, faceData ) | ||
| ); | ||
| const responses = await Promise.allSettled( promises ); | ||
|
|
||
| const results = { | ||
| errors: [], | ||
| successes: [], | ||
| }; | ||
|
|
||
| responses.forEach( ( result, index ) => { | ||
| if ( result.status === 'fulfilled' ) { | ||
| const response = result.value; | ||
| if ( response.id ) { | ||
| results.successes.push( response ); | ||
| } else { | ||
| results.errors.push( { | ||
| data: fontFacesData[ index ], | ||
jffng marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| message: `Error: ${ response.message }`, | ||
| } ); | ||
| } | ||
| } else { | ||
| // Handle network errors or other fetch-related errors | ||
| results.errors.push( { | ||
| data: fontFacesData[ index ], | ||
| error: `Fetch error: ${ result.reason }`, | ||
| } ); | ||
| } | ||
| } ); | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| /* | ||
|
|
||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.