-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
manager.js
108 lines (102 loc) · 2.61 KB
/
manager.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
/**
* External dependencies
*/
import { filter, isArray } from 'lodash';
/**
* WordPress dependencies
*/
import { withSelect } from '@wordpress/data';
import { compose, withState } from '@wordpress/compose';
import { TextControl } from '@wordpress/components';
import { __, _n, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import BlockManagerCategory from './category';
function BlockManager( {
search,
setState,
blockTypes,
categories,
hasBlockSupport,
isMatchingSearchTerm,
numberOfHiddenBlocks,
} ) {
// Filtering occurs here (as opposed to `withSelect`) to avoid wasted
// wasted renders by consequence of `Array#filter` producing a new
// value reference on each call.
blockTypes = blockTypes.filter( ( blockType ) => (
hasBlockSupport( blockType, 'inserter', true ) &&
( ! search || isMatchingSearchTerm( blockType, search ) ) &&
! blockType.parent
) );
return (
<div className="edit-post-manage-blocks-modal__content">
<TextControl
type="search"
label={ __( 'Search for a block' ) }
value={ search }
onChange={ ( nextSearch ) => setState( {
search: nextSearch,
} ) }
className="edit-post-manage-blocks-modal__search"
/>
{ !! numberOfHiddenBlocks && (
<div className="edit-post-manage-blocks-modal__disabled-blocks-count">
{
sprintf(
_n(
'%1$d block is disabled.',
'%1$d blocks are disabled.',
numberOfHiddenBlocks
),
numberOfHiddenBlocks
)
}
</div>
) }
<div
tabIndex="0"
role="region"
aria-label={ __( 'Available block types' ) }
className="edit-post-manage-blocks-modal__results"
>
{ blockTypes.length === 0 && (
<p className="edit-post-manage-blocks-modal__no-results">
{ __( 'No blocks found.' ) }
</p>
) }
{ categories.map( ( category ) => (
<BlockManagerCategory
key={ category.slug }
category={ category }
blockTypes={ filter( blockTypes, {
category: category.slug,
} ) }
/>
) ) }
</div>
</div>
);
}
export default compose( [
withState( { search: '' } ),
withSelect( ( select ) => {
const {
getBlockTypes,
getCategories,
hasBlockSupport,
isMatchingSearchTerm,
} = select( 'core/blocks' );
const { getPreference } = select( 'core/edit-post' );
const hiddenBlockTypes = getPreference( 'hiddenBlockTypes' );
const numberOfHiddenBlocks = isArray( hiddenBlockTypes ) && hiddenBlockTypes.length;
return {
blockTypes: getBlockTypes(),
categories: getCategories(),
hasBlockSupport,
isMatchingSearchTerm,
numberOfHiddenBlocks,
};
} ),
] )( BlockManager );