-
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.
Multi block toolbar controls support
- Loading branch information
1 parent
3343e8c
commit f6de2b9
Showing
6 changed files
with
169 additions
and
13 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,9 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { withMultiBlockSupport } from '../block-controls/multi-block-controls'; | ||
import AlignmentToolbar from './'; | ||
|
||
const MultiBlockAlignmentToolbar = withMultiBlockSupport( AlignmentToolbar, 'align' ); | ||
|
||
export default MultiBlockAlignmentToolbar; |
101 changes: 101 additions & 0 deletions
101
editor/components/block-controls/multi-block-controls.js
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,101 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createSlotFill, Toolbar } from '@wordpress/components'; | ||
import { createHigherOrderComponent, compose } from '@wordpress/compose'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { isFirstOrOnlyBlockSelected } from '../block-edit/context'; | ||
|
||
const { Fill, Slot } = createSlotFill( 'MultiBlockControls' ); | ||
|
||
const MultiBlockControlsFill = ( { controls, children } ) => ( | ||
<Fill> | ||
<Toolbar controls={ controls } /> | ||
{ children } | ||
</Fill> | ||
); | ||
|
||
const MultiBlockControls = isFirstOrOnlyBlockSelected( MultiBlockControlsFill ); | ||
|
||
MultiBlockControls.Slot = Slot; | ||
|
||
export default MultiBlockControls; | ||
|
||
/** | ||
* Reduces blocks to a single attribute's value, taking the first in the list as | ||
* a default, returning `undefined` if all blocks are not the same value. | ||
* | ||
* @param {Array} multiSelectedBlocks Array of selected blocks. | ||
* @param {string} attributeName Attribute name. | ||
* | ||
* @return {*} Reduced value of attribute. | ||
*/ | ||
function reduceAttribute( multiSelectedBlocks, attributeName ) { | ||
let attribute; | ||
// Reduce the selected block's attributes, so if they all have the | ||
// same value for an attribute, we get it in the multi toolbar attributes. | ||
for ( let i = 0; i < multiSelectedBlocks.length; i++ ) { | ||
const block = multiSelectedBlocks[ i ]; | ||
if ( block.attributes[ attributeName ] === attribute || 0 === i ) { | ||
attribute = block.attributes[ attributeName ]; | ||
} else { | ||
attribute = undefined; | ||
} | ||
} | ||
return attribute; | ||
} | ||
|
||
/** | ||
* Adds multi block support to a block control. If the control is used when there is a | ||
* multi block selection, the `onChange` and `value` props are intercepted, and uses | ||
* `reduceAttribute` to get a single value for the control from all selected blocks, | ||
* and changes all selected blocks with the new value. | ||
* | ||
* This requires that multi block controls have `value` and `onChange` props, and | ||
* set attributes on blocks with no other side effects (other than those handled | ||
* when the edit component receives new props) | ||
* | ||
* @param {Component} component Component to make multi block selection aware. | ||
* @param {string} attributeName Attribute name the component controls. | ||
* | ||
* @return {Component} Component that can handle multple selected blocks. | ||
*/ | ||
export const withMultiBlockSupport = ( component, attributeName ) => createHigherOrderComponent( ( OriginalComponent ) => { | ||
const multSelectComponent = ( props ) => { | ||
const newProps = { ...props }; | ||
if ( props.multiSelectedBlocks.length > 1 ) { | ||
newProps.value = reduceAttribute( props.multiSelectedBlocks, attributeName ); | ||
newProps.onChange = ( newValue ) => { | ||
const newAttributes = { | ||
[ attributeName ]: newValue, | ||
}; | ||
for ( let i = 0; i < props.multiSelectedBlocks.length; i++ ) { | ||
newProps.onMultiBlockChange( props.multiSelectedBlocks[ i ].uid, newAttributes ); | ||
} | ||
}; | ||
} | ||
return ( | ||
<OriginalComponent { ...newProps } /> | ||
); | ||
}; | ||
return compose( [ | ||
withSelect( ( select ) => { | ||
const { getMultiSelectedBlocks } = select( 'core/editor' ); | ||
return { | ||
multiSelectedBlocks: getMultiSelectedBlocks(), | ||
}; | ||
} ), | ||
withDispatch( ( dispatch ) => { | ||
const { updateBlockAttributes } = dispatch( 'core/editor' ); | ||
return { | ||
onMultiBlockChange( uid, attributes ) { | ||
updateBlockAttributes( uid, attributes ); | ||
}, | ||
}; | ||
} ), | ||
] )( multSelectComponent ); | ||
}, 'withMultiBlockSupport' )( component ); |
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