-
Notifications
You must be signed in to change notification settings - Fork 4k
/
selectors.js
67 lines (62 loc) · 1.84 KB
/
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
/**
* External dependencies
*/
import createSelector from 'rememo';
/**
* Returns all the available format types.
*
* @param {Object} state Data state.
*
* @return {Array} Format types.
*/
export const getFormatTypes = createSelector(
( state ) => Object.values( state.formatTypes ),
( state ) => [ state.formatTypes ]
);
/**
* Returns a format type by name.
*
* @param {Object} state Data state.
* @param {string} name Format type name.
*
* @return {Object?} Format type.
*/
export function getFormatType( state, name ) {
return state.formatTypes[ name ];
}
/**
* Gets the format type, if any, that can handle a bare element (without a
* data-format-type attribute), given the tag name of this element.
*
* @param {Object} state Data state.
* @param {string} bareElementTagName The tag name of the element to find a
* format type for.
* @return {?Object} Format type.
*/
export function getFormatTypeForBareElement( state, bareElementTagName ) {
const formatTypes = getFormatTypes( state );
return (
formatTypes.find( ( { className, tagName } ) => {
return className === null && bareElementTagName === tagName;
} ) ||
formatTypes.find( ( { className, tagName } ) => {
return className === null && '*' === tagName;
} )
);
}
/**
* Gets the format type, if any, that can handle an element, given its classes.
*
* @param {Object} state Data state.
* @param {string} elementClassName The classes of the element to find a format
* type for.
* @return {?Object} Format type.
*/
export function getFormatTypeForClassName( state, elementClassName ) {
return getFormatTypes( state ).find( ( { className } ) => {
if ( className === null ) {
return false;
}
return ` ${ elementClassName } `.indexOf( ` ${ className } ` ) >= 0;
} );
}