-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experiment with rudity mentory mechanic to allow Block Grouping
Tried a few approaches via `insertBlock/removeBlocks` and even `replaceBlocks` but nothing preserved the undo history apart from this rather brute force method.
- Loading branch information
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
packages/editor/src/components/convert-to-group-buttons/convert-button.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,83 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { noop } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Fragment } from '@wordpress/element'; | ||
import { MenuItem } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
import { compose } from '@wordpress/compose'; | ||
|
||
export function ConvertToGroupButton( { | ||
onConvertToGroup, | ||
} ) { | ||
return ( | ||
<Fragment> | ||
<MenuItem | ||
className="editor-block-settings-menu__control block-editor-block-settings-menu__control" | ||
icon="controls-repeat" | ||
onClick={ onConvertToGroup } | ||
> | ||
{ __( 'Convert to Group' ) } | ||
</MenuItem> | ||
</Fragment> | ||
); | ||
} | ||
|
||
export default compose( [ | ||
withSelect( ( select, { clientIds } ) => { | ||
const { | ||
getBlocksByClientId, | ||
getBlockRootClientId, | ||
} = select( 'core/block-editor' ); | ||
|
||
const blocksToGroup = getBlocksByClientId( clientIds ); | ||
|
||
const isGroupable = ( | ||
blocksToGroup.length === 1 && | ||
blocksToGroup[ 0 ] | ||
); | ||
|
||
// Define any edge cases here | ||
const isVisible = true; | ||
|
||
return { | ||
isGroupable, | ||
isVisible, | ||
blocksToGroup, | ||
getBlockRootClientId, | ||
}; | ||
} ), | ||
withDispatch( ( dispatch, { clientIds, onToggle = noop, blocksToGroup = [], getBlockRootClientId } ) => { | ||
const { | ||
insertBlock, | ||
moveBlockToPosition, | ||
} = dispatch( 'core/block-editor' ); | ||
|
||
return { | ||
onConvertToGroup() { | ||
if ( ! blocksToGroup.length ) { | ||
return; | ||
} | ||
|
||
const wrapperBlock = createBlock( 'core/section', { | ||
backgroundColor: 'lighter-blue', | ||
} ); | ||
|
||
const firstBlockIndex = blocksToGroup[ 0 ].clientId; | ||
|
||
insertBlock( wrapperBlock, firstBlockIndex ); | ||
|
||
clientIds.forEach( ( blockClientId ) => { | ||
moveBlockToPosition( blockClientId, getBlockRootClientId( blockClientId ), wrapperBlock.clientId ); | ||
} ); | ||
onToggle(); | ||
}, | ||
}; | ||
} ), | ||
] )( ConvertToGroupButton ); |
33 changes: 33 additions & 0 deletions
33
packages/editor/src/components/convert-to-group-buttons/index.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,33 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Fragment } from '@wordpress/element'; | ||
import { __experimentalBlockSettingsMenuPluginsExtension } from '@wordpress/block-editor'; | ||
import { withSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ConvertToGroupButton from './convert-button'; | ||
|
||
function ReusableBlocksButtons( { clientIds } ) { | ||
return ( | ||
<__experimentalBlockSettingsMenuPluginsExtension> | ||
{ ( { onClose } ) => ( | ||
<Fragment> | ||
<ConvertToGroupButton | ||
clientIds={ clientIds } | ||
onToggle={ onClose } | ||
/> | ||
</Fragment> | ||
) } | ||
</__experimentalBlockSettingsMenuPluginsExtension> | ||
); | ||
} | ||
|
||
export default withSelect( ( select ) => { | ||
const { getSelectedBlockClientIds } = select( 'core/block-editor' ); | ||
return { | ||
clientIds: getSelectedBlockClientIds(), | ||
}; | ||
} )( ReusableBlocksButtons ); |
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