-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
feat: Inserter displays block collections #42405
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1216df2
feat: Inserter display block collections
dcalhoun 41343b0
test: Add test for createInserterSection utility
dcalhoun c4af0f7
fix: Update reusable blocks list data structure
dcalhoun 35aa90b
test: Fix failure by expanding selector mock
dcalhoun 7e678f1
docs: Add changelog entry
dcalhoun a2214a9
refactor: Simplify and mirror web implementation
dcalhoun e9944a9
refactor: Remove unnecessary object spread
dcalhoun 259c9a1
fix: Remove broken keyExtractor
dcalhoun 4e03fee
fix: Revert utilization of onSelect from useBlockTypesState
dcalhoun cc7fd83
chore: Add background to section headers
dcalhoun 576c6de
chore: Display collection title to improve dark mode support
dcalhoun f044071
fix: Set collection title text color
dcalhoun 4f28816
fix: Adjust horizontal spacing for collection header
dcalhoun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,29 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BlockTypesList from '../block-types-list'; | ||
import useClipboardBlock from './hooks/use-clipboard-block'; | ||
import { store as blockEditorStore } from '../../store'; | ||
import useBlockTypeImpressions from './hooks/use-block-type-impressions'; | ||
import { filterInserterItems } from './utils'; | ||
import { createInserterSection, filterInserterItems } from './utils'; | ||
import useBlockTypesState from './hooks/use-block-types-state'; | ||
|
||
function BlockTypesTab( { onSelect, rootClientId, listProps } ) { | ||
const clipboardBlock = useClipboardBlock( rootClientId ); | ||
|
||
const { blockTypes } = useSelect( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
( select ) => { | ||
const { getInserterItems } = select( blockEditorStore ); | ||
const blockItems = filterInserterItems( | ||
getInserterItems( rootClientId ) | ||
); | ||
const getBlockNamespace = ( item ) => item.name.split( '/' )[ 0 ]; | ||
|
||
return { | ||
blockTypes: clipboardBlock | ||
? [ clipboardBlock, ...blockItems ] | ||
: blockItems, | ||
}; | ||
}, | ||
[ rootClientId ] | ||
function BlockTypesTab( { onSelect, rootClientId, listProps } ) { | ||
const [ rawBlockTypes, , collections ] = useBlockTypesState( | ||
rootClientId, | ||
onSelect | ||
); | ||
|
||
const clipboardBlock = useClipboardBlock( rootClientId ); | ||
const filteredBlockTypes = filterInserterItems( rawBlockTypes ); | ||
const blockTypes = clipboardBlock | ||
? [ clipboardBlock, ...filteredBlockTypes ] | ||
: filteredBlockTypes; | ||
const { items, trackBlockTypeSelected } = | ||
useBlockTypeImpressions( blockTypes ); | ||
|
||
|
@@ -39,10 +32,38 @@ function BlockTypesTab( { onSelect, rootClientId, listProps } ) { | |
onSelect( ...args ); | ||
}; | ||
|
||
const collectionSections = useMemo( () => { | ||
const result = []; | ||
Object.keys( collections ).forEach( ( namespace ) => { | ||
const data = items.filter( | ||
( item ) => getBlockNamespace( item ) === namespace | ||
); | ||
if ( data.length > 0 ) { | ||
result.push( | ||
createInserterSection( { | ||
key: `collection-${ namespace }`, | ||
metadata: { | ||
icon: collections[ namespace ].icon, | ||
title: collections[ namespace ].title, | ||
}, | ||
items: data, | ||
} ) | ||
); | ||
} | ||
} ); | ||
|
||
return result; | ||
}, [ items, collections ] ); | ||
|
||
const sections = [ | ||
createInserterSection( { key: 'default', items } ), | ||
...collectionSections, | ||
]; | ||
|
||
return ( | ||
<BlockTypesList | ||
name="Blocks" | ||
items={ items } | ||
sections={ sections } | ||
onSelect={ handleSelect } | ||
listProps={ listProps } | ||
/> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/block-editor/src/components/inserter/test/utils.native.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { createInserterSection } from '../utils'; | ||
|
||
describe( 'createInserterSection', () => { | ||
it( 'returns the expected object shape', () => { | ||
const key = 'mock-1'; | ||
const items = [ 1, 2, 3 ]; | ||
const metadata = { icon: 'icon-mock', title: 'Title Mock' }; | ||
|
||
expect( createInserterSection( { key, metadata, items } ) ).toEqual( | ||
expect.objectContaining( { | ||
metadata, | ||
data: [ { key, list: items } ], | ||
} ) | ||
); | ||
} ); | ||
|
||
it( 'return always includes metadata', () => { | ||
const key = 'mock-1'; | ||
const items = [ 1, 2, 3 ]; | ||
|
||
expect( createInserterSection( { key, items } ) ).toEqual( | ||
expect.objectContaining( { | ||
metadata: {}, | ||
data: [ { key, list: items } ], | ||
} ) | ||
); | ||
} ); | ||
|
||
it( 'requires a unique key', () => { | ||
expect( () => { | ||
createInserterSection( { items: [ 1, 2, 3 ] } ); | ||
} ).toThrow( 'A unique key for the section must be provided.' ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While testing these changes, I did not observe performance issues on my devices. However, I explored memoizing
renderSection
and its sibling render functions nonetheless. I did not observe much gain in doing so.It appeared the majority of re-renders are triggered from
listProps
changing (listProps
is a large set of attributes fromBottomSheet
), which refactoringlistProps
is likely outside of the scope of this PR.That said, I welcome push back on this topic if we feel there are meaningful performance optimization we should put in place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that this seems outside the scope of this PR. I spent some time measuring performance when adding a block for a separate project this week and believe I also saw
listProps
come up a lot as part of that, so seems like something that could be investigated separately 👍