-
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.
Block List: Show block breadcrumb prior to block
- Loading branch information
Showing
8 changed files
with
276 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { compose } from '@wordpress/element'; | ||
import { Dashicon, Tooltip, Toolbar, Button } from '@wordpress/components'; | ||
import { withDispatch, withSelect } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import NavigableToolbar from '../navigable-toolbar'; | ||
import BlockTitle from '../block-title'; | ||
|
||
/** | ||
* Stops propagation of the given event argument. Assumes that the event has | ||
* been completely handled and no other listeners should be informed. | ||
* | ||
* For the breadcrumb component, this is used for improved interoperability | ||
* with the block's `onFocus` handler which selects the block, thus conflicting | ||
* with the intention to select the root block. | ||
* | ||
* @param {Event} event Event for which propagation should be stopped. | ||
*/ | ||
function stopPropagation( event ) { | ||
event.stopPropagation(); | ||
} | ||
|
||
/** | ||
* Block breadcrumb component, displaying the label of the block. If the block | ||
* descends from a root block, a button is displayed enabling the user to select | ||
* the root block. | ||
* | ||
* @param {string} props.uid UID of block. | ||
* @param {string} props.rootUID UID of block's root. | ||
* @param {Function} props.selectRootBlock Callback to select root block. | ||
* | ||
* @return {WPElement} Breadcrumb element. | ||
*/ | ||
function BlockBreadcrumb( { uid, rootUID, selectRootBlock } ) { | ||
return ( | ||
<NavigableToolbar className="editor-block-breadcrumb"> | ||
<Toolbar> | ||
{ rootUID && ( | ||
<Tooltip text={ __( 'Select parent block' ) }> | ||
<Button | ||
onClick={ selectRootBlock } | ||
onFocus={ stopPropagation } | ||
> | ||
<Dashicon icon="arrow-left" uid={ uid } /> | ||
</Button> | ||
</Tooltip> | ||
) } | ||
<BlockTitle uid={ uid } /> | ||
</Toolbar> | ||
</NavigableToolbar> | ||
); | ||
} | ||
|
||
export default compose( [ | ||
withSelect( ( select, ownProps ) => { | ||
const { getBlockRootUID } = select( 'core/editor' ); | ||
const { uid } = ownProps; | ||
|
||
return { | ||
rootUID: getBlockRootUID( uid ), | ||
}; | ||
} ), | ||
withDispatch( ( dispatch, ownProps ) => { | ||
const { rootUID } = ownProps; | ||
const { selectBlock } = dispatch( 'core/editor' ); | ||
|
||
return { | ||
selectRootBlock: () => selectBlock( rootUID ), | ||
}; | ||
} ), | ||
] )( BlockBreadcrumb ); |
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,10 @@ | ||
Block Title | ||
=========== | ||
|
||
Renders the block's configured title as a string, or empty if the title cannot be determined. | ||
|
||
## Usage | ||
|
||
```jsx | ||
<BlockTitle uid="afd1cb17-2c08-4e7a-91be-007ba7ddc3a1" /> | ||
``` |
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,41 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { withSelect } from '@wordpress/data'; | ||
import { getBlockType } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Renders the block's configured title as a string, or empty if the title | ||
* cannot be determined. | ||
* | ||
* @example | ||
* | ||
* ```jsx | ||
* <BlockTitle uid="afd1cb17-2c08-4e7a-91be-007ba7ddc3a1" /> | ||
* ``` | ||
* | ||
* @param {?string} props.name Block name. | ||
* | ||
* @return {?string} Block title. | ||
*/ | ||
export function BlockTitle( { name } ) { | ||
if ( ! name ) { | ||
return null; | ||
} | ||
|
||
const blockType = getBlockType( name ); | ||
if ( ! blockType ) { | ||
return null; | ||
} | ||
|
||
return blockType.title; | ||
} | ||
|
||
export default withSelect( ( select, ownProps ) => { | ||
const { getBlockName } = select( 'core/editor' ); | ||
const { uid } = ownProps; | ||
|
||
return { | ||
name: getBlockName( uid ), | ||
}; | ||
} )( BlockTitle ); |
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,43 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { BlockTitle } from '../'; | ||
|
||
jest.mock( '@wordpress/blocks', () => { | ||
return { | ||
getBlockType( name ) { | ||
switch ( name ) { | ||
case 'name-not-exists': | ||
return null; | ||
|
||
case 'name-exists': | ||
return { title: 'Block Title' }; | ||
} | ||
}, | ||
}; | ||
} ); | ||
|
||
describe( 'BlockTitle', () => { | ||
it( 'renders nothing if name is falsey', () => { | ||
const wrapper = shallow( <BlockTitle /> ); | ||
|
||
expect( wrapper.type() ).toBe( null ); | ||
} ); | ||
|
||
it( 'renders nothing if block type does not exist', () => { | ||
const wrapper = shallow( <BlockTitle name="name-not-exists" /> ); | ||
|
||
expect( wrapper.type() ).toBe( null ); | ||
} ); | ||
|
||
it( 'renders title if block type exists', () => { | ||
const wrapper = shallow( <BlockTitle name="name-exists" /> ); | ||
|
||
expect( wrapper.text() ).toBe( 'Block Title' ); | ||
} ); | ||
} ); |
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