Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show most frequently used blocks next to inserter #2877

Merged
merged 9 commits into from
Oct 10, 2017
26 changes: 9 additions & 17 deletions editor/modes/visual-editor/test/inserter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
*/
import { shallow } from 'enzyme';

/**
* WordPress dependencies
*/
import { getBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
*/
Expand All @@ -26,17 +31,18 @@ describe( 'VisualEditorInserter', () => {
expect( wrapper.state( 'isShowingControls' ) ).toBe( false );
} );

/* it( 'should insert paragraph block', () => {
it( 'should insert frequently used blocks', () => {
const onInsertBlock = jest.fn();
const mostFrequentlyUsedBlocks = [ getBlockType( 'core/paragraph' ), getBlockType( 'core/image' ) ];
const wrapper = shallow(
<VisualEditorInserter onInsertBlock={ onInsertBlock } />
<VisualEditorInserter onInsertBlock={ onInsertBlock } mostFrequentlyUsedBlocks={ mostFrequentlyUsedBlocks } />
);
wrapper.state.preferences = {
blockUsage: {
'core/paragraph': 42,
'core/image': 34,
},
}
};

wrapper
.findWhere( ( node ) => node.prop( 'children' ) === 'Paragraph' )
Expand All @@ -45,18 +51,4 @@ describe( 'VisualEditorInserter', () => {
expect( onInsertBlock ).toHaveBeenCalled();
expect( onInsertBlock.mock.calls[ 0 ][ 0 ].name ).toBe( 'core/paragraph' );
} );

it( 'should insert image block', () => {
const onInsertBlock = jest.fn();
const wrapper = shallow(
<VisualEditorInserter onInsertBlock={ onInsertBlock } />
);

wrapper
.findWhere( ( node ) => node.prop( 'children' ) === 'Image' )
.simulate( 'click' );

expect( onInsertBlock ).toHaveBeenCalled();
expect( onInsertBlock.mock.calls[ 0 ][ 0 ].name ).toBe( 'core/image' );
} ); */
} );
7 changes: 5 additions & 2 deletions editor/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
some,
values,
keys,
memoize,
} from 'lodash';
import createSelector from 'rememo';

Expand Down Expand Up @@ -879,13 +880,15 @@ export function getRecentlyUsedBlocks( state ) {

/**
* Resolves the block usage stats into a list of the most frequently used blocks.
* Memoized so we're not generating block lists every time we render the list
* in the inserter.
*
* @param {Object} state Global application state
* @return {Array} List of block type settings
*/
export function getMostFrequentlyUsedBlocks( state ) {
export const getMostFrequentlyUsedBlocks = memoize( ( state ) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use createSelector helper instead to memoize the selector (we already use it elsewhere). https://github.com/aduth/rememo

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to suggest something like this, I didn't see we already had something :) Will fix!

const { blockUsage } = state.preferences;
return keys( blockUsage ).sort( ( a, b ) => blockUsage[ b ] - blockUsage[ a ] )
.slice( 0, 3 )
.map( blockType => getBlockType( blockType ) );
}
}, ( state ) => state.preferences.blockUsage );