-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Inserter: Enable inserting a block using the inserter #407
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
Changes from 1 commit
a6d2631
48462f4
98fbaa5
a180ff1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Adding the INSERT_BLOCK action - Simplifying the hovered and selected reducers
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,100 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { connect } from 'react-redux'; | ||
| import uuid from 'uuid/v4'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import './style.scss'; | ||
| import Dashicon from 'components/dashicon'; | ||
|
|
||
| function InserterMenu( { position = 'top' } ) { | ||
| const blocks = wp.blocks.getBlocks(); | ||
| const blocksByCategory = blocks.reduce( ( groups, block ) => { | ||
| if ( ! groups[ block.category ] ) { | ||
| groups[ block.category ] = []; | ||
| } | ||
| groups[ block.category ].push( block ); | ||
| return groups; | ||
| }, {} ); | ||
| const categories = wp.blocks.getCategories(); | ||
| class InserterMenu extends wp.element.Component { | ||
| constructor() { | ||
| super( ...arguments ); | ||
| this.state = { | ||
| filterValue: '' | ||
| }; | ||
| this.filter = this.filter.bind( this ); | ||
| } | ||
|
|
||
| filter( event ) { | ||
| this.setState( { | ||
| filterValue: event.target.value | ||
| } ); | ||
| } | ||
|
|
||
| return ( | ||
| <div className={ `editor-inserter__menu is-${ position }` }> | ||
| <div className="editor-inserter__arrow" /> | ||
| <div className="editor-inserter__content"> | ||
| { categories | ||
| .map( ( category ) => !! blocksByCategory[ category.slug ] && ( | ||
| <div key={ category.slug }> | ||
| <div className="editor-inserter__separator">{ category.title }</div> | ||
| <div className="editor-inserter__category-blocks"> | ||
| { blocksByCategory[ category.slug ].map( ( { slug, title, icon } ) => ( | ||
| <div key={ slug } className="editor-inserter__block"> | ||
| <Dashicon icon={ icon } /> | ||
| { title } | ||
| </div> | ||
| ) ) } | ||
| selectBlock( slug ) { | ||
| return () => { | ||
| this.props.onInsertBlock( slug ); | ||
| this.props.onSelect(); | ||
| }; | ||
| } | ||
|
|
||
| render() { | ||
| const { position = 'top' } = this.props; | ||
| const blocks = wp.blocks.getBlocks(); | ||
| const isShownBlock = block => block.title.toLowerCase().indexOf( this.state.filterValue.toLowerCase() ) !== -1; | ||
|
||
| const blocksByCategory = blocks.reduce( ( groups, block ) => { | ||
| if ( ! isShownBlock( block ) ) { | ||
| return groups; | ||
| } | ||
| if ( ! groups[ block.category ] ) { | ||
| groups[ block.category ] = []; | ||
| } | ||
| groups[ block.category ].push( block ); | ||
| return groups; | ||
| }, {} ); | ||
| const categories = wp.blocks.getCategories(); | ||
|
|
||
| return ( | ||
| <div className={ `editor-inserter__menu is-${ position }` }> | ||
| <div className="editor-inserter__arrow" /> | ||
| <div className="editor-inserter__content"> | ||
| { categories | ||
| .map( ( category ) => !! blocksByCategory[ category.slug ] && ( | ||
| <div key={ category.slug }> | ||
| <div className="editor-inserter__separator">{ category.title }</div> | ||
| <div className="editor-inserter__category-blocks"> | ||
| { blocksByCategory[ category.slug ].map( ( { slug, title, icon } ) => ( | ||
| <button | ||
| key={ slug } | ||
| className="editor-inserter__block" | ||
| onClick={ this.selectBlock( slug ) } | ||
| > | ||
| <Dashicon icon={ icon } /> | ||
| { title } | ||
| </button> | ||
| ) ) } | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ) ) | ||
| } | ||
| ) ) | ||
| } | ||
| </div> | ||
| <input | ||
| type="search" | ||
| placeholder={ wp.i18n.__( 'Search…' ) } | ||
| className="editor-inserter__search" | ||
| onChange={ this.filter } | ||
| /> | ||
| </div> | ||
| <input | ||
| type="search" | ||
| placeholder={ wp.i18n.__( 'Search…' ) } | ||
| className="editor-inserter__search" /> | ||
| </div> | ||
| ); | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default InserterMenu; | ||
| export default connect( | ||
| undefined, | ||
| ( dispatch ) => ( { | ||
| onInsertBlock( slug ) { | ||
| dispatch( { | ||
| type: 'INSERT_BLOCK', | ||
| block: { | ||
| uid: uuid(), | ||
| blockType: slug, | ||
| attributes: {} | ||
| } | ||
| } ); | ||
| } | ||
| } ) | ||
| )( InserterMenu ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,12 @@ export const blocks = combineReducers( { | |
| ...action.updates | ||
| } | ||
| }; | ||
|
|
||
| case 'INSERT_BLOCK': | ||
| return { | ||
| ...state, | ||
| [ action.block.uid ]: action.block | ||
| }; | ||
| } | ||
|
|
||
| return state; | ||
|
|
@@ -62,35 +68,50 @@ export const blocks = combineReducers( { | |
| action.uid, | ||
| ...state.slice( index + 2 ) | ||
| ]; | ||
|
|
||
| case 'INSERT_BLOCK': | ||
|
||
| return [ | ||
| ...state, | ||
| action.block.uid | ||
| ]; | ||
| } | ||
|
|
||
| return state; | ||
| }, | ||
| selected( state = {}, action ) { | ||
| selected( state = null, action ) { | ||
| switch ( action.type ) { | ||
| case 'TOGGLE_BLOCK_SELECTED': | ||
| return { | ||
| ...state, | ||
| [ action.uid ]: action.selected | ||
| }; | ||
| if ( action.selected ) { | ||
| return action.uid; | ||
| } | ||
|
|
||
| if ( ! action.selected && action.uid === state ) { | ||
| return null; | ||
| } | ||
|
|
||
| return state; | ||
| case 'INSERT_BLOCK': | ||
| return action.block.uid; | ||
| } | ||
|
|
||
| return state; | ||
| }, | ||
| hovered( state = {}, action ) { | ||
| hovered( state = null, action ) { | ||
| switch ( action.type ) { | ||
| case 'TOGGLE_BLOCK_HOVERED': | ||
| return { | ||
| ...state, | ||
| [ action.uid ]: action.hovered | ||
| }; | ||
| if ( action.hovered ) { | ||
| return action.uid; | ||
| } | ||
|
|
||
| if ( ! action.hovered && action.uid === state ) { | ||
| return null; | ||
| } | ||
|
|
||
| return state; | ||
|
|
||
| case 'TOGGLE_BLOCK_SELECTED': | ||
| if ( state[ action.uid ] ) { | ||
| return { | ||
| ...state, | ||
| [ action.uid ]: false | ||
| }; | ||
| if ( state === action.uid ) { | ||
| return null; | ||
| } | ||
| break; | ||
| } | ||
|
|
||

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.
Should we clear the current filter value?