-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathfont-family.js
108 lines (94 loc) · 2.74 KB
/
font-family.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { hasBlockSupport } from '@wordpress/blocks';
import TokenList from '@wordpress/token-list';
import { privateApis as componentsPrivateApis } from '@wordpress/components';
/**
* Internal dependencies
*/
import { shouldSkipSerialization } from './utils';
import { TYPOGRAPHY_SUPPORT_KEY } from './typography';
import { unlock } from '../lock-unlock';
export const FONT_FAMILY_SUPPORT_KEY = 'typography.__experimentalFontFamily';
const { kebabCase } = unlock( componentsPrivateApis );
/**
* Filters registered block settings, extending attributes to include
* the `fontFamily` attribute.
*
* @param {Object} settings Original block settings
* @return {Object} Filtered block settings
*/
function addAttributes( settings ) {
if ( ! hasBlockSupport( settings, FONT_FAMILY_SUPPORT_KEY ) ) {
return settings;
}
// Allow blocks to specify a default value if needed.
if ( ! settings.attributes.fontFamily ) {
Object.assign( settings.attributes, {
fontFamily: {
type: 'string',
},
} );
}
return settings;
}
/**
* Override props assigned to save component to inject font family.
*
* @param {Object} props Additional props applied to save element
* @param {Object} blockType Block type
* @param {Object} attributes Block attributes
* @return {Object} Filtered props applied to save element
*/
function addSaveProps( props, blockType, attributes ) {
if ( ! hasBlockSupport( blockType, FONT_FAMILY_SUPPORT_KEY ) ) {
return props;
}
if (
shouldSkipSerialization(
blockType,
TYPOGRAPHY_SUPPORT_KEY,
'fontFamily'
)
) {
return props;
}
if ( ! attributes?.fontFamily ) {
return props;
}
// Use TokenList to dedupe classes.
const classes = new TokenList( props.className );
classes.add( `has-${ kebabCase( attributes?.fontFamily ) }-font-family` );
const newClassName = classes.value;
props.className = newClassName ? newClassName : undefined;
return props;
}
function useBlockProps( { name, fontFamily } ) {
return addSaveProps( {}, name, { fontFamily } );
}
export default {
useBlockProps,
addSaveProps,
attributeKeys: [ 'fontFamily' ],
hasSupport( name ) {
return hasBlockSupport( name, FONT_FAMILY_SUPPORT_KEY );
},
};
/**
* Resets the font family block support attribute. This can be used when
* disabling the font family support controls for a block via a progressive
* discovery panel.
*
* @param {Object} props Block props.
* @param {Object} props.setAttributes Function to set block's attributes.
*/
export function resetFontFamily( { setAttributes } ) {
setAttributes( { fontFamily: undefined } );
}
addFilter(
'blocks.registerBlockType',
'core/fontFamily/addAttribute',
addAttributes
);