-
Notifications
You must be signed in to change notification settings - Fork 4k
/
private-selectors.js
162 lines (144 loc) · 3.66 KB
/
private-selectors.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
/**
* External dependencies
*/
import createSelector from 'rememo';
import { get } from 'lodash';
/**
* Internal dependencies
*/
import { getBlockType } from './selectors';
import { __EXPERIMENTAL_STYLE_PROPERTY as STYLE_PROPERTY } from '../api/constants';
const ROOT_BLOCK_SUPPORTS = [
'background',
'backgroundColor',
'color',
'filter',
'linkColor',
'captionColor',
'buttonColor',
'fontFamily',
'fontSize',
'fontStyle',
'fontWeight',
'lineHeight',
'padding',
'contentSize',
'wideSize',
'blockGap',
'textDecoration',
'textTransform',
'letterSpacing',
];
/**
* Filters the list of supported styles for a given element.
*
* @param {string[]} blockSupports list of supported styles.
* @param {string|undefined} name block name.
* @param {string|undefined} element element name.
*
* @return {string[]} filtered list of supported styles.
*/
function filterElementBlockSupports( blockSupports, name, element ) {
return blockSupports.filter( ( support ) => {
if ( support === 'fontSize' && element === 'heading' ) {
return false;
}
// This is only available for links
if ( support === 'textDecoration' && ! name && element !== 'link' ) {
return false;
}
// This is only available for heading
if (
support === 'textTransform' &&
! name &&
! [ 'heading', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ].includes(
element
)
) {
return false;
}
// This is only available for headings
if (
support === 'letterSpacing' &&
! name &&
! [ 'heading', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ].includes(
element
)
) {
return false;
}
// Text columns is only available for blocks.
if ( support === 'textColumns' && ! name ) {
return false;
}
return true;
} );
}
/**
* Returns the list of supported styles for a given block name and element.
*/
export const getSupportedStyles = createSelector(
( state, name, element ) => {
if ( ! name ) {
return filterElementBlockSupports(
ROOT_BLOCK_SUPPORTS,
name,
element
);
}
const blockType = getBlockType( state, name );
if ( ! blockType ) {
return [];
}
const supportKeys = [];
// Check for blockGap support.
// Block spacing support doesn't map directly to a single style property, so needs to be handled separately.
// Also, only allow `blockGap` support if serialization has not been skipped, to be sure global spacing can be applied.
if (
blockType?.supports?.spacing?.blockGap &&
blockType?.supports?.spacing?.__experimentalSkipSerialization !==
true &&
! blockType?.supports?.spacing?.__experimentalSkipSerialization?.some?.(
( spacingType ) => spacingType === 'blockGap'
)
) {
supportKeys.push( 'blockGap' );
}
// check for shadow support
if ( blockType?.supports?.shadow ) {
supportKeys.push( 'shadow' );
}
Object.keys( STYLE_PROPERTY ).forEach( ( styleName ) => {
if ( ! STYLE_PROPERTY[ styleName ].support ) {
return;
}
// Opting out means that, for certain support keys like background color,
// blocks have to explicitly set the support value false. If the key is
// unset, we still enable it.
if ( STYLE_PROPERTY[ styleName ].requiresOptOut ) {
if (
STYLE_PROPERTY[ styleName ].support[ 0 ] in
blockType.supports &&
get(
blockType.supports,
STYLE_PROPERTY[ styleName ].support
) !== false
) {
supportKeys.push( styleName );
return;
}
}
if (
get(
blockType.supports,
STYLE_PROPERTY[ styleName ].support,
false
)
) {
supportKeys.push( styleName );
}
} );
return filterElementBlockSupports( supportKeys, name, element );
},
( state, name ) => [ state.blockTypes[ name ] ]
);