-
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.
Implemented button in the block toolbar that allows to move selection…
… to the parent.
- Loading branch information
1 parent
187a643
commit cb5a05c
Showing
3 changed files
with
74 additions
and
40 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,60 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { omit } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { compose } from '@wordpress/element'; | ||
import { ifCondition, IconButton } from '@wordpress/components'; | ||
import { withDispatch, withSelect } from '@wordpress/data'; | ||
import { getBlockFocusableWrapper } from '../../utils/dom'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
export function BlockSelectParent( { selectRootBlock, onFocus, ...props } ) { | ||
const selectParentLabel = __( 'Select parent block' ); | ||
const onFocusHandler = ( event ) => { | ||
if ( onFocus ) { | ||
onFocus( event ); | ||
} | ||
// 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. | ||
event.stopPropagation(); | ||
}; | ||
|
||
return ( | ||
<IconButton | ||
onClick={ selectRootBlock } | ||
onFocus={ onFocusHandler } | ||
label={ selectParentLabel } | ||
icon="arrow-left-alt" | ||
{ ...omit( props, [ 'rootUID' ] ) } | ||
/> | ||
); | ||
} | ||
|
||
export default compose( [ | ||
withSelect( ( select, ownProps ) => { | ||
const { getBlockRootUID } = select( 'core/editor' ); | ||
const { uid } = ownProps; | ||
|
||
return { | ||
rootUID: getBlockRootUID( uid ), | ||
}; | ||
} ), | ||
ifCondition( ( { rootUID } ) => rootUID ), | ||
withDispatch( ( dispatch, ownProps ) => { | ||
const { rootUID } = ownProps; | ||
const { selectBlock } = dispatch( 'core/editor' ); | ||
|
||
return { | ||
selectRootBlock: () => { | ||
selectBlock( rootUID ); | ||
const selectedBlockElement = getBlockFocusableWrapper( rootUID ); | ||
selectedBlockElement.focus(); | ||
}, | ||
}; | ||
} ), | ||
] )( BlockSelectParent ); |
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