Skip to content

Commit

Permalink
Exclude reusable blocks from the global block count
Browse files Browse the repository at this point in the history
Modifies getGlobalBlockCount() to exclude reusable blocks from its
count. This fixes the block count including reusable blocks that have
been fetched and parsed but not inserted into the post or page.
  • Loading branch information
noisysocks committed Nov 13, 2018
1 parent 3ff9890 commit 488bb0d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ export const getClientIdsWithDescendants = createSelector(
export const getGlobalBlockCount = createSelector(
( state, blockName ) => {
if ( ! blockName ) {
return size( state.editor.present.blocks.byClientId );
return size( state.editor.present.blocks.byClientId ) - size( state.reusableBlocks.data );
}
return reduce(
state.editor.present.blocks.byClientId,
Expand Down
33 changes: 32 additions & 1 deletion packages/editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2523,12 +2523,37 @@ describe( 'selectors', () => {
},
},
},
reusableBlocks: {
data: {},
},
};

expect( getGlobalBlockCount( state ) ).toBe( 2 );
} );

it( 'should return the global umber of blocks of a given type', () => {
it( 'should exclude blocks referenced by reusable blocks from the count', () => {
const state = {
editor: {
present: {
blocks: {
byClientId: {
123: { clientId: 123, name: 'core/paragraph', attributes: {} },
456: { clientId: 456, name: 'core/paragraph', attributes: {} },
},
},
},
},
reusableBlocks: {
data: {
1: { clientId: 456 },
},
},
};

expect( getGlobalBlockCount( state ) ).toBe( 1 );
} );

it( 'should return the global number of blocks of a given type', () => {
const state = {
editor: {
present: {
Expand All @@ -2542,6 +2567,9 @@ describe( 'selectors', () => {
},
},
},
reusableBlocks: {
data: {},
},
};

expect( getGlobalBlockCount( state, 'core/heading' ) ).toBe( 1 );
Expand All @@ -2556,6 +2584,9 @@ describe( 'selectors', () => {
},
},
},
reusableBlocks: {
data: {},
},
};
expect( getGlobalBlockCount( state ) ).toBe( 0 );
expect( getGlobalBlockCount( state, 'core/heading' ) ).toBe( 0 );
Expand Down

0 comments on commit 488bb0d

Please sign in to comment.