Skip to content

Commit

Permalink
Experiment with rudity mentory mechanic to allow Block Grouping
Browse files Browse the repository at this point in the history
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
getdave committed Apr 10, 2019
1 parent 0bc9067 commit c17e571
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
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 packages/editor/src/components/convert-to-group-buttons/index.js
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 );
2 changes: 2 additions & 0 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { decodeEntities } from '@wordpress/html-entities';
import transformStyles from '../../editor-styles';
import { mediaUpload } from '../../utils';
import ReusableBlocksButtons from '../reusable-blocks-buttons';
import ConvertToGroupButtons from '../convert-to-group-buttons';

const fetchLinkSuggestions = async ( search ) => {
const posts = await apiFetch( {
Expand Down Expand Up @@ -158,6 +159,7 @@ class EditorProvider extends Component {
>
{ children }
<ReusableBlocksButtons />
<ConvertToGroupButtons />
</BlockEditorProvider>
);
}
Expand Down

0 comments on commit c17e571

Please sign in to comment.