-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathprivate-selectors.js
211 lines (189 loc) · 4.91 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
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
/**
* WordPress dependencies
*/
import { createSelector } from '@wordpress/data';
/**
* Internal dependencies
*/
import { getBlockType } from './selectors';
import { getValueFromObjectPath } from './utils';
import { __EXPERIMENTAL_STYLE_PROPERTY as STYLE_PROPERTY } from '../api/constants';
const ROOT_BLOCK_SUPPORTS = [
'background',
'backgroundColor',
'color',
'linkColor',
'captionColor',
'buttonColor',
'headingColor',
'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, button, caption and text
if (
support === 'textTransform' &&
! name &&
! (
[ 'heading', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ].includes(
element
) ||
element === 'button' ||
element === 'caption' ||
element === 'text'
)
) {
return false;
}
// This is only available for heading, button, caption and text
if (
support === 'letterSpacing' &&
! name &&
! (
[ 'heading', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ].includes(
element
) ||
element === 'button' ||
element === 'caption' ||
element === 'text'
)
) {
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.
if ( blockType?.supports?.spacing?.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 &&
getValueFromObjectPath(
blockType.supports,
STYLE_PROPERTY[ styleName ].support
) !== false
) {
supportKeys.push( styleName );
return;
}
}
if (
getValueFromObjectPath(
blockType.supports,
STYLE_PROPERTY[ styleName ].support,
false
)
) {
supportKeys.push( styleName );
}
} );
return filterElementBlockSupports( supportKeys, name, element );
},
( state, name ) => [ state.blockTypes[ name ] ]
);
/**
* Returns the bootstrapped block type metadata for a give block name.
*
* @param {Object} state Data state.
* @param {string} name Block name.
*
* @return {Object} Bootstrapped block type metadata for a block.
*/
export function getBootstrappedBlockType( state, name ) {
return state.bootstrappedBlockTypes[ name ];
}
/**
* Returns all the unprocessed (before applying the `registerBlockType` filter)
* block type settings as passed during block registration.
*
* @param {Object} state Data state.
*
* @return {Array} Unprocessed block type settings for all blocks.
*/
export function getUnprocessedBlockTypes( state ) {
return state.unprocessedBlockTypes;
}
/**
* Returns all the block bindings sources registered.
*
* @param {Object} state Data state.
*
* @return {Object} All the registered sources and their properties.
*/
export function getAllBlockBindingsSources( state ) {
return state.blockBindingsSources;
}
/**
* Returns a specific block bindings source.
*
* @param {Object} state Data state.
* @param {string} sourceName Name of the source to get.
*
* @return {Object} The specific block binding source and its properties.
*/
export function getBlockBindingsSource( state, sourceName ) {
return state.blockBindingsSources[ sourceName ];
}