-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve child blocks API's and UI. (#7003)
This PR extends the blocks API with two new selectors getChildBlockNames and hasChildBlocks. A class is added to inserter items with children, and the UI is changed using the new class. Now we have a special design in blocks with children. A new area that appears when we are inserting blocks inside blocks with children was created.
- Loading branch information
1 parent
02c9edb
commit c93b778
Showing
13 changed files
with
333 additions
and
27 deletions.
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
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,137 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getChildBlockNames } from '../selectors'; | ||
|
||
describe( 'selectors', () => { | ||
describe( 'getChildBlockNames', () => { | ||
it( 'should return an empty array if state is empty', () => { | ||
const state = {}; | ||
|
||
expect( getChildBlockNames( state, 'parent1' ) ).toHaveLength( 0 ); | ||
} ); | ||
|
||
it( 'should return an empty array if no children exist', () => { | ||
const state = { | ||
blockTypes: [ | ||
{ | ||
name: 'child1', | ||
parent: [ 'parent1' ], | ||
}, | ||
{ | ||
name: 'child2', | ||
parent: [ 'parent2' ], | ||
}, | ||
{ | ||
name: 'parent3', | ||
}, | ||
], | ||
}; | ||
|
||
expect( getChildBlockNames( state, 'parent3' ) ).toHaveLength( 0 ); | ||
} ); | ||
|
||
it( 'should return an empty array if the parent block is not found', () => { | ||
const state = { | ||
blockTypes: [ | ||
{ | ||
name: 'child1', | ||
parent: [ 'parent1' ], | ||
}, | ||
{ | ||
name: 'parent1', | ||
}, | ||
], | ||
}; | ||
|
||
expect( getChildBlockNames( state, 'parent3' ) ).toHaveLength( 0 ); | ||
} ); | ||
|
||
it( 'should return an array with the child block names', () => { | ||
const state = { | ||
blockTypes: [ | ||
{ | ||
name: 'child1', | ||
parent: [ 'parent1' ], | ||
}, | ||
{ | ||
name: 'child2', | ||
parent: [ 'parent2' ], | ||
}, | ||
{ | ||
name: 'child3', | ||
parent: [ 'parent1' ], | ||
}, | ||
{ | ||
name: 'child4', | ||
}, | ||
{ | ||
name: 'parent1', | ||
}, | ||
{ | ||
name: 'parent2', | ||
}, | ||
], | ||
}; | ||
|
||
expect( getChildBlockNames( state, 'parent1' ) ).toEqual( [ 'child1', 'child3' ] ); | ||
} ); | ||
|
||
it( 'should return an array with the child block names even if only one child exists', () => { | ||
const state = { | ||
blockTypes: [ | ||
{ | ||
name: 'child1', | ||
parent: [ 'parent1' ], | ||
}, | ||
{ | ||
name: 'child2', | ||
parent: [ 'parent2' ], | ||
}, | ||
{ | ||
name: 'child4', | ||
}, | ||
{ | ||
name: 'parent1', | ||
}, | ||
{ | ||
name: 'parent2', | ||
}, | ||
], | ||
}; | ||
|
||
expect( getChildBlockNames( state, 'parent1' ) ).toEqual( [ 'child1' ] ); | ||
} ); | ||
|
||
it( 'should return an array with the child block names even if children have multiple parents', () => { | ||
const state = { | ||
blockTypes: [ | ||
{ | ||
name: 'child1', | ||
parent: [ 'parent1' ], | ||
}, | ||
{ | ||
name: 'child2', | ||
parent: [ 'parent1', 'parent2' ], | ||
}, | ||
{ | ||
name: 'child3', | ||
parent: [ 'parent1' ], | ||
}, | ||
{ | ||
name: 'child4', | ||
}, | ||
{ | ||
name: 'parent1', | ||
}, | ||
{ | ||
name: 'parent2', | ||
}, | ||
], | ||
}; | ||
|
||
expect( getChildBlockNames( state, 'parent1' ) ).toEqual( [ 'child1', 'child2', 'child3' ] ); | ||
expect( getChildBlockNames( state, 'parent2' ) ).toEqual( [ 'child2' ] ); | ||
} ); | ||
} ); | ||
} ); |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { compose } from '@wordpress/element'; | ||
import { withSelect } from '@wordpress/data'; | ||
import { ifCondition } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
import ItemList from './item-list'; | ||
import BlockIcon from '../block-icon'; | ||
|
||
function ChildBlocks( { rootBlockIcon, rootBlockTitle, items, ...props } ) { | ||
return ( | ||
<div className="editor-inserter__child-blocks"> | ||
{ ( rootBlockIcon || rootBlockTitle ) && ( | ||
<div className="editor-inserter__parent-block-header"> | ||
{ rootBlockIcon && ( | ||
<div className="editor-inserter__parent-block-icon"> | ||
<BlockIcon icon={ rootBlockIcon } /> | ||
</div> | ||
) } | ||
{ rootBlockTitle && <h2>{ rootBlockTitle }</h2> } | ||
</div> | ||
) } | ||
<ItemList items={ items } { ...props } /> | ||
</div> | ||
); | ||
} | ||
|
||
export default compose( | ||
ifCondition( ( { items } ) => items && items.length > 0 ), | ||
withSelect( ( select, { rootUID } ) => { | ||
const { | ||
getBlockType, | ||
} = select( 'core/blocks' ); | ||
const { | ||
getBlockName, | ||
} = select( 'core/editor' ); | ||
const rootBlockName = getBlockName( rootUID ); | ||
const rootBlockType = getBlockType( rootBlockName ); | ||
return { | ||
rootBlockTitle: rootBlockType && rootBlockType.title, | ||
rootBlockIcon: rootBlockType && rootBlockType.icon, | ||
}; | ||
} ), | ||
)( ChildBlocks ); |
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
Oops, something went wrong.