-
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
Add support for Child Blocks #6753
Changes from all commits
7b6778b
d25bce7
cbc622f
8401418
24f99df
92f4576
f1a899e
7b09b14
4f6e662
7e215a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ import { | |
PanelBody, | ||
} from '@wordpress/components'; | ||
import { getCategories, isSharedBlock } from '@wordpress/blocks'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
import { withDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -33,6 +33,8 @@ import './style.scss'; | |
import BlockPreview from '../block-preview'; | ||
import ItemList from './item-list'; | ||
|
||
const MAX_SUGGESTED_ITEMS = 9; | ||
|
||
/** | ||
* Filters an item list given a search term. | ||
* | ||
|
@@ -56,9 +58,10 @@ export class InserterMenu extends Component { | |
this.state = { | ||
filterValue: '', | ||
hoveredItem: null, | ||
suggestedItems: [], | ||
sharedItems: [], | ||
itemsPerCategory: {}, | ||
openPanels: [ 'frecent' ], | ||
openPanels: [ 'suggested' ], | ||
}; | ||
this.onChangeSearchInput = this.onChangeSearchInput.bind( this ); | ||
this.onHover = this.onHover.bind( this ); | ||
|
@@ -107,7 +110,15 @@ export class InserterMenu extends Component { | |
filter( filterValue = '' ) { | ||
const { items } = this.props; | ||
const filteredItems = searchItems( items, filterValue ); | ||
|
||
let suggestedItems = []; | ||
if ( ! filterValue ) { | ||
const maxSuggestedItems = this.props.maxSuggestedItems || MAX_SUGGESTED_ITEMS; | ||
suggestedItems = filter( items, ( item ) => item.utility > 0 ).slice( 0, maxSuggestedItems ); | ||
} | ||
|
||
const sharedItems = filter( filteredItems, { category: 'shared' } ); | ||
|
||
const getCategoryIndex = ( item ) => { | ||
return findIndex( getCategories(), ( category ) => category.slug === item.category ); | ||
}; | ||
|
@@ -116,10 +127,11 @@ export class InserterMenu extends Component { | |
( itemList ) => sortBy( itemList, getCategoryIndex ), | ||
( itemList ) => groupBy( itemList, 'category' ) | ||
)( filteredItems ); | ||
|
||
let openPanels = this.state.openPanels; | ||
if ( filterValue !== this.state.filterValue ) { | ||
if ( ! filterValue ) { | ||
openPanels = [ 'frecent' ]; | ||
openPanels = [ 'suggested' ]; | ||
} else if ( sharedItems.length ) { | ||
openPanels = [ 'shared' ]; | ||
} else if ( filteredItems.length ) { | ||
|
@@ -131,16 +143,16 @@ export class InserterMenu extends Component { | |
this.setState( { | ||
hoveredItem: null, | ||
filterValue, | ||
suggestedItems, | ||
sharedItems, | ||
itemsPerCategory, | ||
openPanels, | ||
} ); | ||
} | ||
|
||
render() { | ||
const { instanceId, frecentItems, onSelect } = this.props; | ||
const { hoveredItem, filterValue, sharedItems, itemsPerCategory, openPanels } = this.state; | ||
const isSearching = !! filterValue; | ||
const { instanceId, onSelect } = this.props; | ||
const { hoveredItem, suggestedItems, sharedItems, itemsPerCategory, openPanels } = this.state; | ||
const isPanelOpen = ( panel ) => openPanels.indexOf( panel ) !== -1; | ||
|
||
// Disable reason: The inserter menu is a modal display, not one which | ||
|
@@ -163,13 +175,13 @@ export class InserterMenu extends Component { | |
/> | ||
|
||
<div className="editor-inserter__results"> | ||
{ ! isSearching && | ||
{ !! suggestedItems.length && | ||
<PanelBody | ||
title={ __( 'Most Used' ) } | ||
opened={ isPanelOpen( 'frecent' ) } | ||
onToggle={ this.onTogglePanel( 'frecent' ) } | ||
opened={ isPanelOpen( 'suggested' ) } | ||
onToggle={ this.onTogglePanel( 'suggested' ) } | ||
> | ||
<ItemList items={ frecentItems } onSelect={ onSelect } onHover={ this.onHover } /> | ||
<ItemList items={ suggestedItems } onSelect={ onSelect } onHover={ this.onHover } /> | ||
</PanelBody> | ||
} | ||
|
||
|
@@ -211,22 +223,6 @@ export class InserterMenu extends Component { | |
} | ||
|
||
export default compose( | ||
withSelect( ( select ) => { | ||
const { | ||
getBlockInsertionPoint, | ||
getInserterItems, | ||
getFrecentInserterItems, | ||
getSupportedBlocks, | ||
getEditorSettings, | ||
} = select( 'core/editor' ); | ||
const { allowedBlockTypes } = getEditorSettings(); | ||
const { rootUID } = getBlockInsertionPoint(); | ||
const supportedBlocks = getSupportedBlocks( rootUID, allowedBlockTypes ); | ||
return { | ||
items: getInserterItems( supportedBlocks ), | ||
frecentItems: getFrecentInserterItems( supportedBlocks ), | ||
}; | ||
} ), | ||
withDispatch( ( dispatch ) => ( { | ||
fetchSharedBlocks: dispatch( 'core/editor' ).fetchSharedBlocks, | ||
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. Minor: It is a little strange that we are dispatching a fetch action here but we are not selecting anything in this component. To make this relationship more clear maybe the 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. I'll leave this for now because we're looking into replacing this action with a resolver which would address this. 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.
Where is this tracked? 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. |
||
} ) ), | ||
|
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.
By the current wording, there's an implication that I could insert a block into e.g. an image block, which is not true. This is to say: Not all blocks support nested content.
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.
👌 changed in 920d7cc