From 5fa160746c9aee6427f3e6fd0129ab1805813d06 Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Sun, 17 Mar 2024 16:35:26 -0500 Subject: [PATCH] Font Library: fix JS errors when activating or deactivating system fonts --- .../components/global-styles/font-library-modal/context.js | 6 +++--- .../global-styles/font-library-modal/utils/index.js | 6 +++--- .../font-library-modal/utils/preview-styles.js | 4 ++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/context.js b/packages/edit-site/src/components/global-styles/font-library-modal/context.js index 2c04a72bb6c6e..4a1f34a095699 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/context.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/context.js @@ -425,15 +425,15 @@ function FontLibraryProvider( { children } ) { const isFaceActivated = isFontActivated( font.slug, - face.fontStyle, - face.fontWeight, + face?.fontStyle, + face?.fontWeight, font.source ); if ( isFaceActivated ) { loadFontFaceInBrowser( face, - getDisplaySrcFromFontFace( face.src ), + getDisplaySrcFromFontFace( face?.src ), 'all' ); } else { 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 07153bcd1b9c3..b3deb8fcec499 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 @@ -134,9 +134,9 @@ export function unloadFontFaceInBrowser( fontFace, removeFrom = 'all' ) { const unloadFontFace = ( fonts ) => { fonts.forEach( ( f ) => { if ( - f.family === formatFontFaceName( fontFace.fontFamily ) && - f.weight === fontFace.fontWeight && - f.style === fontFace.fontStyle + f.family === formatFontFaceName( fontFace?.fontFamily ) && + f.weight === fontFace?.fontWeight && + f.style === fontFace?.fontStyle ) { fonts.delete( f ); } diff --git a/packages/edit-site/src/components/global-styles/font-library-modal/utils/preview-styles.js b/packages/edit-site/src/components/global-styles/font-library-modal/utils/preview-styles.js index ef2b04feceb5e..d0b5d1389d801 100644 --- a/packages/edit-site/src/components/global-styles/font-library-modal/utils/preview-styles.js +++ b/packages/edit-site/src/components/global-styles/font-library-modal/utils/preview-styles.js @@ -87,6 +87,10 @@ export function formatFontFamily( input ) { * formatFontFaceName(", 'Open Sans', 'Helvetica Neue', sans-serif") => "Open Sans" */ export function formatFontFaceName( input ) { + if ( ! input ) { + return ''; + } + let output = input.trim(); if ( output.includes( ',' ) ) { output = output