-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
utils.js
96 lines (92 loc) · 2.9 KB
/
utils.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
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { LAYOUT_DEFINITIONS } from './definitions';
/**
* Utility to generate the proper CSS selector for layout styles.
*
* @param {string} selectors CSS selector, also supports multiple comma-separated selectors.
* @param {string} append The string to append.
*
* @return {string} - CSS selector.
*/
export function appendSelectors( selectors, append = '' ) {
return selectors
.split( ',' )
.map(
( subselector ) =>
`${ subselector }${ append ? ` ${ append }` : '' }`
)
.join( ',' );
}
/**
* Get generated blockGap CSS rules based on layout definitions provided in theme.json
* Falsy values in the layout definition's spacingStyles rules will be swapped out
* with the provided `blockGapValue`.
*
* @param {string} selector The CSS selector to target for the generated rules.
* @param {Object} layoutDefinitions Layout definitions object.
* @param {string} layoutType The layout type (e.g. `default` or `flex`).
* @param {string} blockGapValue The current blockGap value to be applied.
* @return {string} The generated CSS rules.
*/
export function getBlockGapCSS(
selector,
layoutDefinitions = LAYOUT_DEFINITIONS,
layoutType,
blockGapValue
) {
let output = '';
if (
layoutDefinitions?.[ layoutType ]?.spacingStyles?.length &&
blockGapValue
) {
layoutDefinitions[ layoutType ].spacingStyles.forEach( ( gapStyle ) => {
output += `${ appendSelectors(
selector,
gapStyle.selector.trim()
) } { `;
output += Object.entries( gapStyle.rules )
.map(
( [ cssProperty, value ] ) =>
`${ cssProperty }: ${ value ? value : blockGapValue }`
)
.join( '; ' );
output += '; }';
} );
}
return output;
}
/**
* Helper method to assign contextual info to clarify
* alignment settings.
*
* Besides checking if `contentSize` and `wideSize` have a
* value, we now show this information only if their values
* are not a `css var`. This needs to change when parsing
* css variables land.
*
* @see https://github.com/WordPress/gutenberg/pull/34710#issuecomment-918000752
*
* @param {Object} layout The layout object.
* @return {Object} An object with contextual info per alignment.
*/
export function getAlignmentsInfo( layout ) {
const { contentSize, wideSize, type = 'default' } = layout;
const alignmentInfo = {};
const sizeRegex =
/^(?!0)\d+(px|em|rem|vw|vh|%|svw|lvw|dvw|svh|lvh|dvh|vi|svi|lvi|dvi|vb|svb|lvb|dvb|vmin|svmin|lvmin|dvmin|vmax|svmax|lvmax|dvmax)?$/i;
if ( sizeRegex.test( contentSize ) && type === 'constrained' ) {
// translators: %s: container size (i.e. 600px etc)
alignmentInfo.none = sprintf( __( 'Max %s wide' ), contentSize );
}
if ( sizeRegex.test( wideSize ) ) {
// translators: %s: container size (i.e. 600px etc)
alignmentInfo.wide = sprintf( __( 'Max %s wide' ), wideSize );
}
return alignmentInfo;
}