-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathhooks.js
68 lines (63 loc) · 1.38 KB
/
hooks.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
/**
* WordPress dependencies
*/
import {
category,
page,
postContent,
tag,
customPostType,
} from '@wordpress/icons';
/**
* Internal dependencies
*/
import fallbackVariations from './fallback-variations';
function getIcon( variationName ) {
switch ( variationName ) {
case 'post':
return postContent;
case 'page':
return page;
case 'tag':
return tag;
case 'category':
return category;
default:
return customPostType;
}
}
export function enhanceNavigationLinkVariations( settings, name ) {
if ( name !== 'core/navigation-link' ) {
return settings;
}
// Fallback handling may be deleted after supported WP ranges understand the `variations`
// property when passed to register_block_type in index.php.
if ( ! settings.variations ) {
return {
...settings,
variations: fallbackVariations,
};
}
// Otherwise decorate server passed variations with an icon and isActive function.
if ( settings.variations ) {
const isActive = ( blockAttributes, variationAttributes ) => {
return blockAttributes.type === variationAttributes.type;
};
const variations = settings.variations.map( ( variation ) => {
return {
...variation,
...( ! variation.icon && {
icon: getIcon( variation.name ),
} ),
...( ! variation.isActive && {
isActive,
} ),
};
} );
return {
...settings,
variations,
};
}
return settings;
}