Skip to content

Commit e34e523

Browse files
committed
Update getInserterItems to respect new reusable block data layout
Makes `getInserterItems` and `getFrecentInserterItems` respect that reusable blocks now point to a block that is elsewhere in the editor state. This makes reusable blocks again appear in the inserter.
1 parent 4e1026f commit e34e523

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

editor/store/selectors.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -1122,12 +1122,13 @@ function buildInserterItemFromBlockType( state, enabledBlockTypes, blockType ) {
11221122
/**
11231123
* Given a reusable block, constructs an item that appears in the inserter.
11241124
*
1125+
* @param {Object} state Global application state.
11251126
* @param {string[]|boolean} enabledBlockTypes Enabled block types, or true/false to enable/disable all types.
11261127
* @param {Object} reusableBlock Reusable block, likely from getReusableBlock().
11271128
*
11281129
* @return {Editor.InserterItem} Item that appears in inserter.
11291130
*/
1130-
function buildInserterItemFromReusableBlock( enabledBlockTypes, reusableBlock ) {
1131+
function buildInserterItemFromReusableBlock( state, enabledBlockTypes, reusableBlock ) {
11311132
if ( ! enabledBlockTypes || ! reusableBlock ) {
11321133
return null;
11331134
}
@@ -1137,7 +1138,12 @@ function buildInserterItemFromReusableBlock( enabledBlockTypes, reusableBlock )
11371138
return null;
11381139
}
11391140

1140-
const referencedBlockType = getBlockType( reusableBlock.type );
1141+
const referencedBlock = getBlock( state, reusableBlock.uid );
1142+
if ( ! referencedBlock ) {
1143+
return null;
1144+
}
1145+
1146+
const referencedBlockType = getBlockType( referencedBlock.name );
11411147
if ( ! referencedBlockType ) {
11421148
return null;
11431149
}
@@ -1173,7 +1179,7 @@ export function getInserterItems( state, enabledBlockTypes = true ) {
11731179
);
11741180

11751181
const dynamicItems = getReusableBlocks( state ).map( reusableBlock =>
1176-
buildInserterItemFromReusableBlock( enabledBlockTypes, reusableBlock )
1182+
buildInserterItemFromReusableBlock( state, enabledBlockTypes, reusableBlock )
11771183
);
11781184

11791185
const items = [ ...staticItems, ...dynamicItems ];
@@ -1201,7 +1207,7 @@ function getItemsFromInserts( state, inserts, enabledBlockTypes = true, maximum
12011207
const items = fillWithCommonBlocks( inserts ).map( insert => {
12021208
if ( insert.ref ) {
12031209
const reusableBlock = getReusableBlock( state, insert.ref );
1204-
return buildInserterItemFromReusableBlock( enabledBlockTypes, reusableBlock );
1210+
return buildInserterItemFromReusableBlock( state, enabledBlockTypes, reusableBlock );
12051211
}
12061212

12071213
const blockType = getBlockType( insert.name );
@@ -1248,8 +1254,8 @@ export function getFrecentInserterItems( state, enabledBlockTypes = true, maximu
12481254
/**
12491255
* Returns the reusable block with the given ID.
12501256
*
1251-
* @param {Object} state Global application state.
1252-
* @param {string} ref The reusable block's ID.
1257+
* @param {Object} state Global application state.
1258+
* @param {number|string} ref The reusable block's ID.
12531259
*
12541260
* @return {Object} The reusable block, or null if none exists.
12551261
*/
@@ -1260,10 +1266,12 @@ export const getReusableBlock = createSelector(
12601266
return null;
12611267
}
12621268

1269+
const isTemporary = isNaN( parseInt( ref ) );
1270+
12631271
return {
12641272
...block,
1265-
id: ref,
1266-
isTemporary: ! Number.isInteger( ref ),
1273+
id: isTemporary ? ref : +ref,
1274+
isTemporary,
12671275
};
12681276
},
12691277
( state, ref ) => [
@@ -1304,7 +1312,7 @@ export function isFetchingReusableBlock( state, ref ) {
13041312
* @return {Array} An array of all reusable blocks.
13051313
*/
13061314
export function getReusableBlocks( state ) {
1307-
return Object.values( state.reusableBlocks.data );
1315+
return Object.keys( state.reusableBlocks.data ).map( ( ref ) => getReusableBlock( state, ref ) );
13081316
}
13091317

13101318
/**

editor/store/test/selectors.js

+16-26
Original file line numberDiff line numberDiff line change
@@ -2344,19 +2344,17 @@ describe( 'selectors', () => {
23442344
const state = {
23452345
editor: {
23462346
present: {
2347-
blocksByUid: {},
2347+
blocksByUid: {
2348+
carrot: { name: 'core/test-block' },
2349+
},
23482350
blockOrder: {},
23492351
edits: {},
23502352
},
23512353
},
23522354
currentPost: {},
23532355
reusableBlocks: {
23542356
data: {
2355-
123: {
2356-
id: 123,
2357-
title: 'My reusable block',
2358-
type: 'core/test-block',
2359-
},
2357+
123: { uid: 'carrot', title: 'My reusable block' },
23602358
},
23612359
},
23622360
};
@@ -2398,14 +2396,19 @@ describe( 'selectors', () => {
23982396
},
23992397
editor: {
24002398
present: {
2399+
blocksByUid: {
2400+
carrot: { name: 'core/test-block' },
2401+
},
24012402
blockOrder: [],
2403+
edits: {},
24022404
},
24032405
},
24042406
reusableBlocks: {
24052407
data: {
2406-
123: { id: 123, type: 'core/test-block' },
2408+
123: { uid: 'carrot' },
24072409
},
24082410
},
2411+
currentPost: {},
24092412
};
24102413

24112414
expect( getFrecentInserterItems( state, true, 3 ) ).toMatchObject( [
@@ -2545,33 +2548,20 @@ describe( 'selectors', () => {
25452548

25462549
describe( 'getReusableBlocks', () => {
25472550
it( 'should return an array of reusable blocks', () => {
2548-
const reusableBlock1 = {
2549-
id: '358b59ee-bab3-4d6f-8445-e8c6971a5605',
2550-
name: 'My cool block',
2551-
type: 'core/paragraph',
2552-
attributes: {
2553-
content: 'Hello!',
2554-
},
2555-
};
2556-
const reusableBlock2 = {
2557-
id: '687e1a87-cca1-41f2-a782-197ddaea9abf',
2558-
name: 'My neat block',
2559-
type: 'core/paragraph',
2560-
attributes: {
2561-
content: 'Goodbye!',
2562-
},
2563-
};
25642551
const state = {
25652552
reusableBlocks: {
25662553
data: {
2567-
[ reusableBlock1.id ]: reusableBlock1,
2568-
[ reusableBlock2.id ]: reusableBlock2,
2554+
123: { uid: 'carrot' },
2555+
reusable1: { uid: 'broccoli' },
25692556
},
25702557
},
25712558
};
25722559

25732560
const reusableBlocks = getReusableBlocks( state );
2574-
expect( reusableBlocks ).toEqual( [ reusableBlock1, reusableBlock2 ] );
2561+
expect( reusableBlocks ).toEqual( [
2562+
{ id: 123, isTemporary: false, uid: 'carrot' },
2563+
{ id: 'reusable1', isTemporary: true, uid: 'broccoli' },
2564+
] );
25752565
} );
25762566

25772567
it( 'should return an empty array when no reusable blocks exist', () => {

0 commit comments

Comments
 (0)