-
Notifications
You must be signed in to change notification settings - Fork 4k
/
font-size.js
401 lines (355 loc) · 10 KB
/
font-size.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { hasBlockSupport } from '@wordpress/blocks';
import TokenList from '@wordpress/token-list';
import { createHigherOrderComponent } from '@wordpress/compose';
import { select } from '@wordpress/data';
/**
* Internal dependencies
*/
import {
getFontSize,
getFontSizeClass,
getFontSizeObjectByValue,
FontSizePicker,
getComputedFluidTypographyValue,
} from '../components/font-sizes';
import { TYPOGRAPHY_SUPPORT_KEY } from './typography';
import {
cleanEmptyObject,
transformStyles,
shouldSkipSerialization,
} from './utils';
import useSetting from '../components/use-setting';
import { store as blockEditorStore } from '../store';
export const FONT_SIZE_SUPPORT_KEY = 'typography.fontSize';
/**
* Filters registered block settings, extending attributes to include
* `fontSize` and `fontWeight` attributes.
*
* @param {Object} settings Original block settings.
*
* @return {Object} Filtered block settings.
*/
function addAttributes( settings ) {
if ( ! hasBlockSupport( settings, FONT_SIZE_SUPPORT_KEY ) ) {
return settings;
}
// Allow blocks to specify a default value if needed.
if ( ! settings.attributes.fontSize ) {
Object.assign( settings.attributes, {
fontSize: {
type: 'string',
},
} );
}
return settings;
}
/**
* Override props assigned to save component to inject font size.
*
* @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_SIZE_SUPPORT_KEY ) ) {
return props;
}
if (
shouldSkipSerialization( blockType, TYPOGRAPHY_SUPPORT_KEY, 'fontSize' )
) {
return props;
}
// Use TokenList to dedupe classes.
const classes = new TokenList( props.className );
classes.add( getFontSizeClass( attributes.fontSize ) );
const newClassName = classes.value;
props.className = newClassName ? newClassName : undefined;
return props;
}
/**
* Filters registered block settings to expand the block edit wrapper
* by applying the desired styles and classnames.
*
* @param {Object} settings Original block settings.
*
* @return {Object} Filtered block settings.
*/
function addEditProps( settings ) {
if ( ! hasBlockSupport( settings, FONT_SIZE_SUPPORT_KEY ) ) {
return settings;
}
const existingGetEditWrapperProps = settings.getEditWrapperProps;
settings.getEditWrapperProps = ( attributes ) => {
let props = {};
if ( existingGetEditWrapperProps ) {
props = existingGetEditWrapperProps( attributes );
}
return addSaveProps( props, settings, attributes );
};
return settings;
}
/**
* Inspector control panel containing the font size related configuration
*
* @param {Object} props
*
* @return {WPElement} Font size edit element.
*/
export function FontSizeEdit( props ) {
const {
attributes: { fontSize, style },
setAttributes,
} = props;
const fontSizes = useSetting( 'typography.fontSizes' );
const onChange = ( value ) => {
const fontSizeSlug = getFontSizeObjectByValue( fontSizes, value ).slug;
setAttributes( {
style: cleanEmptyObject( {
...style,
typography: {
...style?.typography,
fontSize: fontSizeSlug ? undefined : value,
},
} ),
fontSize: fontSizeSlug,
} );
};
const fontSizeObject = getFontSize(
fontSizes,
fontSize,
style?.typography?.fontSize
);
const fontSizeValue =
fontSizeObject?.size || style?.typography?.fontSize || fontSize;
return (
<FontSizePicker
onChange={ onChange }
value={ fontSizeValue }
withReset={ false }
withSlider
size="__unstable-large"
__nextHasNoMarginBottom
/>
);
}
/**
* Checks if there is a current value set for the font size block support.
*
* @param {Object} props Block props.
* @return {boolean} Whether or not the block has a font size value set.
*/
export function hasFontSizeValue( props ) {
const { fontSize, style } = props.attributes;
return !! fontSize || !! style?.typography?.fontSize;
}
/**
* Resets the font size block support attribute. This can be used when
* disabling the font size support controls for a block via a progressive
* discovery panel.
*
* @param {Object} props Block props.
* @param {Object} props.attributes Block's attributes.
* @param {Object} props.setAttributes Function to set block's attributes.
*/
export function resetFontSize( { attributes = {}, setAttributes } ) {
const { style } = attributes;
setAttributes( {
fontSize: undefined,
style: cleanEmptyObject( {
...style,
typography: {
...style?.typography,
fontSize: undefined,
},
} ),
} );
}
/**
* Custom hook that checks if font-size settings have been disabled.
*
* @param {string} name The name of the block.
* @return {boolean} Whether setting is disabled.
*/
export function useIsFontSizeDisabled( { name: blockName } = {} ) {
const fontSizes = useSetting( 'typography.fontSizes' );
const hasFontSizes = !! fontSizes?.length;
return (
! hasBlockSupport( blockName, FONT_SIZE_SUPPORT_KEY ) || ! hasFontSizes
);
}
/**
* Add inline styles for font sizes.
* Ideally, this is not needed and themes load the font-size classes on the
* editor.
*
* @param {Function} BlockListBlock Original component.
*
* @return {Function} Wrapped component.
*/
const withFontSizeInlineStyles = createHigherOrderComponent(
( BlockListBlock ) => ( props ) => {
const fontSizes = useSetting( 'typography.fontSizes' );
const {
name: blockName,
attributes: { fontSize, style },
wrapperProps,
} = props;
// Only add inline styles if the block supports font sizes,
// doesn't skip serialization of font sizes,
// doesn't already have an inline font size,
// and does have a class to extract the font size from.
if (
! hasBlockSupport( blockName, FONT_SIZE_SUPPORT_KEY ) ||
shouldSkipSerialization(
blockName,
TYPOGRAPHY_SUPPORT_KEY,
'fontSize'
) ||
! fontSize ||
style?.typography?.fontSize
) {
return <BlockListBlock { ...props } />;
}
const fontSizeValue = getFontSize(
fontSizes,
fontSize,
style?.typography?.fontSize
).size;
const newProps = {
...props,
wrapperProps: {
...wrapperProps,
style: {
fontSize: fontSizeValue,
...wrapperProps?.style,
},
},
};
return <BlockListBlock { ...newProps } />;
},
'withFontSizeInlineStyles'
);
const MIGRATION_PATHS = {
fontSize: [ [ 'fontSize' ], [ 'style', 'typography', 'fontSize' ] ],
};
export function addTransforms( result, source, index, results ) {
const destinationBlockType = result.name;
const activeSupports = {
fontSize: hasBlockSupport(
destinationBlockType,
FONT_SIZE_SUPPORT_KEY
),
};
return transformStyles(
activeSupports,
MIGRATION_PATHS,
result,
source,
index,
results
);
}
/**
* Allow custom font sizes to appear fluid when fluid typography is enabled at
* the theme level.
*
* Adds a custom getEditWrapperProps() callback to all block types that support
* font sizes. Then, if fluid typography is enabled, this callback will swap any
* custom font size in style.fontSize with a fluid font size (i.e. one that uses
* clamp()).
*
* It's important that this hook runs after 'core/style/addEditProps' sets
* style.fontSize as otherwise fontSize will be overwritten.
*
* @param {Object} blockType Block settings object.
*/
function addEditPropsForFluidCustomFontSizes( blockType ) {
if (
! hasBlockSupport( blockType, FONT_SIZE_SUPPORT_KEY ) ||
shouldSkipSerialization( blockType, TYPOGRAPHY_SUPPORT_KEY, 'fontSize' )
) {
return blockType;
}
const existingGetEditWrapperProps = blockType.getEditWrapperProps;
blockType.getEditWrapperProps = ( attributes ) => {
const wrapperProps = existingGetEditWrapperProps
? existingGetEditWrapperProps( attributes )
: {};
const fontSize = wrapperProps?.style?.fontSize;
// TODO: This sucks! We should be using useSetting( 'typography.fluid' )
// or even useSelect( blockEditorStore ). We can't do either here
// because getEditWrapperProps is a plain JavaScript function called by
// BlockListBlock and not a React component rendered within
// BlockListContext.Provider. If we set fontSize using editor.
// BlockListBlock instead of using getEditWrapperProps then the value is
// clobbered when the core/style/addEditProps filter runs.
const fluidTypographyConfig =
select( blockEditorStore ).getSettings().__experimentalFeatures
?.typography?.fluid;
const fluidTypographySettings =
typeof fluidTypographyConfig?.fluid === 'object'
? fluidTypographyConfig?.fluid
: {};
const newFontSize =
fontSize && !! fluidTypographyConfig
? getComputedFluidTypographyValue( {
fontSize,
minimumViewPortWidth:
fluidTypographySettings?.minViewPortWidth,
maximumViewPortWidth:
fluidTypographySettings?.maxViewPortWidth,
scaleFactor: fluidTypographySettings?.scaleFactor,
minimumFontSizeFactor:
fluidTypographySettings?.minViewPortWidth,
maximumFontSizeFactor:
fluidTypographySettings?.maxFontSizeFactor,
} )
: null;
if ( newFontSize === null ) {
return wrapperProps;
}
return {
...wrapperProps,
style: {
...wrapperProps?.style,
fontSize: newFontSize,
},
};
};
return blockType;
}
addFilter(
'blocks.registerBlockType',
'core/font/addAttribute',
addAttributes
);
addFilter(
'blocks.getSaveContent.extraProps',
'core/font/addSaveProps',
addSaveProps
);
addFilter( 'blocks.registerBlockType', 'core/font/addEditProps', addEditProps );
addFilter(
'editor.BlockListBlock',
'core/font-size/with-font-size-inline-styles',
withFontSizeInlineStyles
);
addFilter(
'blocks.switchToBlockType.transformedBlock',
'core/font-size/addTransforms',
addTransforms
);
addFilter(
'blocks.registerBlockType',
'core/font-size/addEditPropsForFluidCustomFontSizes',
addEditPropsForFluidCustomFontSizes,
// Run after 'core/style/addEditProps' so that the style object has already
// been translated into inline CSS.
11
);