-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b2f095
commit 081494a
Showing
13 changed files
with
210 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { isEmpty } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { SelectControl } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useEditorFeature from '../use-editor-feature'; | ||
|
||
export default function FontFamilyControl( { | ||
value = '', | ||
onChange, | ||
fontFamilies, | ||
...props | ||
} ) { | ||
const blockLevelFontFamilies = useEditorFeature( | ||
'typography.fontFamilies' | ||
); | ||
if ( ! fontFamilies ) { | ||
fontFamilies = blockLevelFontFamilies; | ||
} | ||
|
||
if ( isEmpty( fontFamilies ) ) { | ||
return null; | ||
} | ||
|
||
const options = [ | ||
{ value: '', label: __( 'Default' ) }, | ||
...fontFamilies.map( ( { fontFamily, name } ) => { | ||
return { | ||
value: fontFamily, | ||
label: name || fontFamily, | ||
}; | ||
} ), | ||
]; | ||
return ( | ||
<SelectControl | ||
label={ __( 'Font family' ) } | ||
options={ options } | ||
value={ value } | ||
onChange={ onChange } | ||
labelPosition="top" | ||
{ ...props } | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { find } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { hasBlockSupport } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { cleanEmptyObject } from './utils'; | ||
import useEditorFeature from '../components/use-editor-feature'; | ||
import FontFamilyControl from '../components/font-family'; | ||
|
||
export const FONT_FAMILY_SUPPORT_KEY = '__experimentalFontFamily'; | ||
|
||
const getFontFamilyFromAttributeValue = ( fontFamilies, value ) => { | ||
const attributeParsed = /var:preset\|font-family\|(.+)/.exec( value ); | ||
if ( attributeParsed && attributeParsed[ 1 ] ) { | ||
return find( fontFamilies, ( { slug } ) => { | ||
return slug === attributeParsed[ 1 ]; | ||
} ).fontFamily; | ||
} | ||
return value; | ||
}; | ||
|
||
export function FontFamilyEdit( { | ||
name, | ||
setAttributes, | ||
attributes: { style = {} }, | ||
} ) { | ||
const fontFamilies = useEditorFeature( 'typography.fontFamilies' ); | ||
const isDisable = useIsFontFamilyDisabled( { name } ); | ||
|
||
if ( isDisable ) { | ||
return null; | ||
} | ||
|
||
const value = getFontFamilyFromAttributeValue( | ||
fontFamilies, | ||
style.typography?.fontFamily | ||
); | ||
|
||
function onChange( newValue ) { | ||
const predefinedFontFamily = find( | ||
fontFamilies, | ||
( { fontFamily } ) => fontFamily === newValue | ||
); | ||
setAttributes( { | ||
style: cleanEmptyObject( { | ||
...style, | ||
typography: { | ||
...( style.typography || {} ), | ||
fontFamily: predefinedFontFamily | ||
? `var:preset|font-family|${ predefinedFontFamily.slug }` | ||
: newValue || undefined, | ||
}, | ||
} ), | ||
} ); | ||
} | ||
|
||
return ( | ||
<FontFamilyControl | ||
className="block-editor-hooks-font-family-control" | ||
fontFamilies={ fontFamilies } | ||
value={ value } | ||
onChange={ onChange } | ||
/> | ||
); | ||
} | ||
|
||
/** | ||
* Custom hook that checks if font-family functionality is disabled. | ||
* | ||
* @param {string} name The name of the block. | ||
* @return {boolean} Whether setting is disabled. | ||
*/ | ||
export function useIsFontFamilyDisabled( { name } ) { | ||
const fontFamilies = useEditorFeature( 'typography.fontFamilies' ); | ||
return ( | ||
! fontFamilies || | ||
fontFamilies.length === 0 || | ||
! hasBlockSupport( name, FONT_FAMILY_SUPPORT_KEY ) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ | |
"color": { | ||
"textColor": true, | ||
"backgroundColor": true | ||
} | ||
}, | ||
"__experimentalFontFamily": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters