-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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 all commits
a6d2631
48462f4
98fbaa5
a180ff1
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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import uuid from 'uuid/v4'; | ||
|
|
||
| /** | ||
| * Returns a block object given its type and attributes | ||
| * | ||
| * @param {Object} blockType BlockType | ||
| * @param {Object} attributes Block attributes | ||
| * @return {Object} Block object | ||
| */ | ||
| export function createBlock( blockType, attributes = {} ) { | ||
| return { | ||
| uid: uuid(), | ||
| blockType, | ||
| attributes | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { expect } from 'chai'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { createBlock } from '../factory'; | ||
|
|
||
| describe( 'block factory', () => { | ||
| describe( 'createBlock()', () => { | ||
| it( 'should create a block given its blockType and attributes', () => { | ||
| const block = createBlock( 'core/test-block', { | ||
| align: 'left' | ||
| } ); | ||
|
|
||
| expect( block.blockType ).to.eql( 'core/test-block' ); | ||
| expect( block.attributes ).to.eql( { | ||
| align: 'left' | ||
| } ); | ||
| expect( block.uid ).to.be.a( 'string' ); | ||
| } ); | ||
| } ); | ||
| } ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,96 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { connect } from 'react-redux'; | ||
|
|
||
| /** | ||
| * 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 ); | ||
| } | ||
|
|
||
| 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> | ||
| ) ) } | ||
| filter( event ) { | ||
| this.setState( { | ||
| filterValue: event.target.value | ||
| } ); | ||
| } | ||
|
|
||
| selectBlock( slug ) { | ||
| return () => { | ||
| this.props.onInsertBlock( slug ); | ||
| this.props.onSelect(); | ||
| this.setState( { filterValue: '' } ); | ||
| }; | ||
| } | ||
|
|
||
| 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: wp.blocks.createBlock( slug ) | ||
| } ); | ||
| } | ||
| } ) | ||
| )( InserterMenu ); | ||
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.
Cool re-use 👍