-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
block-list.js
305 lines (274 loc) · 7.58 KB
/
block-list.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* External dependencies
*/
import {
map,
includes,
findIndex,
flow,
sortBy,
groupBy,
isEmpty,
} from 'lodash';
/**
* WordPress dependencies
*/
import { __, _x, _n, sprintf } from '@wordpress/i18n';
import { withSpokenMessages } from '@wordpress/components';
import { addQueryArgs } from '@wordpress/url';
import { controlsRepeat } from '@wordpress/icons';
import { speak } from '@wordpress/a11y';
import { createBlock } from '@wordpress/blocks';
import { useMemo, useEffect } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
/**
* Internal dependencies
*/
import BlockTypesList from '../block-types-list';
import ChildBlocks from './child-blocks';
import __experimentalInserterMenuExtension from '../inserter-menu-extension';
import { searchBlockItems } from './search-items';
import InserterPanel from './panel';
import InserterNoResults from './no-results';
// Copied over from the Columns block. It seems like it should become part of public API.
const createBlocksFromInnerBlocksTemplate = ( innerBlocksTemplate ) => {
return map(
innerBlocksTemplate,
( [ name, attributes, innerBlocks = [] ] ) =>
createBlock(
name,
attributes,
createBlocksFromInnerBlocksTemplate( innerBlocks )
)
);
};
const getBlockNamespace = ( item ) => item.name.split( '/' )[ 0 ];
const MAX_SUGGESTED_ITEMS = 6;
export function InserterBlockList( {
rootClientId,
onInsert,
onHover,
__experimentalSelectBlockOnInsert: selectBlockOnInsert,
filterValue,
debouncedSpeak,
} ) {
const {
categories,
collections,
items,
rootChildBlocks,
fetchReusableBlocks,
} = useSelect(
( select ) => {
const { getInserterItems, getBlockName, getSettings } = select(
'core/block-editor'
);
const {
getCategories,
getCollections,
getChildBlockNames,
} = select( 'core/blocks' );
const rootBlockName = getBlockName( rootClientId );
const { __experimentalFetchReusableBlocks } = getSettings();
return {
categories: getCategories(),
collections: getCollections(),
rootChildBlocks: getChildBlockNames( rootBlockName ),
items: getInserterItems( rootClientId ),
fetchReusableBlocks: __experimentalFetchReusableBlocks,
};
},
[ rootClientId ]
);
// Fetch resuable blocks on mount
useEffect( () => {
if ( fetchReusableBlocks ) {
fetchReusableBlocks();
}
}, [] );
const onSelectItem = ( item ) => {
const { name, title, initialAttributes, innerBlocks } = item;
const insertedBlock = createBlock(
name,
initialAttributes,
createBlocksFromInnerBlocksTemplate( innerBlocks )
);
onInsert( insertedBlock );
if ( ! selectBlockOnInsert ) {
// translators: %s: the name of the block that has been added
const message = sprintf( __( '%s block added' ), title );
speak( message );
}
};
const filteredItems = useMemo( () => {
return searchBlockItems( items, categories, collections, filterValue );
}, [ filterValue, items, categories, collections ] );
const childItems = useMemo( () => {
return filteredItems.filter( ( { name } ) =>
includes( rootChildBlocks, name )
);
}, [ filteredItems, rootChildBlocks ] );
const suggestedItems = useMemo( () => {
return items.slice( 0, MAX_SUGGESTED_ITEMS );
}, [ items ] );
const reusableItems = useMemo( () => {
return filteredItems.filter(
( { category } ) => category === 'reusable'
);
}, [ filteredItems ] );
const uncategorizedItems = useMemo( () => {
return filteredItems.filter( ( item ) => ! item.category );
}, [ filteredItems ] );
const itemsPerCategory = useMemo( () => {
const getCategoryIndex = ( item ) => {
return findIndex(
categories,
( category ) => category.slug === item.category
);
};
return flow(
( itemList ) =>
itemList.filter(
( item ) => item.category && item.category !== 'reusable'
),
( itemList ) => sortBy( itemList, getCategoryIndex ),
( itemList ) => groupBy( itemList, 'category' )
)( filteredItems );
}, [ filteredItems, categories ] );
const itemsPerCollection = useMemo( () => {
// Create a new Object to avoid mutating collection
const result = { ...collections };
Object.keys( collections ).forEach( ( namespace ) => {
result[ namespace ] = filteredItems.filter(
( item ) => getBlockNamespace( item ) === namespace
);
if ( result[ namespace ].length === 0 ) {
delete result[ namespace ];
}
} );
return result;
}, [ filteredItems, collections ] );
// Announce search results on change
useEffect( () => {
const resultsFoundMessage = sprintf(
/* translators: %d: number of results. */
_n( '%d result found.', '%d results found.', filteredItems.length ),
filteredItems.length
);
debouncedSpeak( resultsFoundMessage );
}, [ filterValue, debouncedSpeak ] );
const hasItems = ! isEmpty( filteredItems );
const hasChildItems = childItems.length > 0;
return (
<div>
<ChildBlocks
rootClientId={ rootClientId }
items={ childItems }
onSelect={ onSelectItem }
onHover={ onHover }
/>
{ ! hasChildItems && !! suggestedItems.length && ! filterValue && (
<InserterPanel title={ _x( 'Most used', 'blocks' ) }>
<BlockTypesList
items={ suggestedItems }
onSelect={ onSelectItem }
onHover={ onHover }
/>
</InserterPanel>
) }
{ ! hasChildItems &&
map( categories, ( category ) => {
const categoryItems = itemsPerCategory[ category.slug ];
if ( ! categoryItems || ! categoryItems.length ) {
return null;
}
return (
<InserterPanel
key={ category.slug }
title={ category.title }
icon={ category.icon }
>
<BlockTypesList
items={ categoryItems }
onSelect={ onSelectItem }
onHover={ onHover }
/>
</InserterPanel>
);
} ) }
{ ! hasChildItems && !! uncategorizedItems.length && (
<InserterPanel
className="block-editor-inserter__uncategorized-blocks-panel"
title={ __( 'Uncategorized' ) }
>
<BlockTypesList
items={ uncategorizedItems }
onSelect={ onSelectItem }
onHover={ onHover }
/>
</InserterPanel>
) }
{ ! hasChildItems &&
map( collections, ( collection, namespace ) => {
const collectionItems = itemsPerCollection[ namespace ];
if ( ! collectionItems || ! collectionItems.length ) {
return null;
}
return (
<InserterPanel
key={ namespace }
title={ collection.title }
icon={ collection.icon }
>
<BlockTypesList
items={ collectionItems }
onSelect={ onSelectItem }
onHover={ onHover }
/>
</InserterPanel>
);
} ) }
{ ! hasChildItems && !! reusableItems.length && (
<InserterPanel
className="block-editor-inserter__reusable-blocks-panel"
title={ __( 'Reusable' ) }
icon={ controlsRepeat }
>
<BlockTypesList
items={ reusableItems }
onSelect={ onSelectItem }
onHover={ onHover }
/>
<a
className="block-editor-inserter__manage-reusable-blocks"
href={ addQueryArgs( 'edit.php', {
post_type: 'wp_block',
} ) }
>
{ __( 'Manage all reusable blocks' ) }
</a>
</InserterPanel>
) }
<__experimentalInserterMenuExtension.Slot
fillProps={ {
onSelect: onSelectItem,
onHover,
filterValue,
hasItems,
} }
>
{ ( fills ) => {
if ( fills.length ) {
return fills;
}
if ( ! hasItems ) {
return <InserterNoResults />;
}
return null;
} }
</__experimentalInserterMenuExtension.Slot>
</div>
);
}
export default compose( withSpokenMessages )( InserterBlockList );