Skip to content

Commit

Permalink
Edit Site: Add convert to template part flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras committed Feb 25, 2020
1 parent 7c21d2c commit 927b609
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export default function TemplatePartInnerBlocks() {
);
return (
<InnerBlocks
__experimentalBlocks={ blocks }
// Allow initial blocks from a block transform to load, if any.
__experimentalBlocks={ blocks.length === 0 ? undefined : blocks }
onInput={ onInput }
onChange={ onChange }
/>
Expand Down
28 changes: 28 additions & 0 deletions packages/block-library/src/template-part/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { createBlock } from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -18,4 +19,31 @@ export const settings = {
html: false,
},
edit,
transforms: {
from: [
{
type: 'block',
isMultiBlock: true,
blocks: [ '*' ],
__experimentalConvert( blocks ) {
// Avoid transforming a single `core/template-part` block.
if ( blocks.length === 1 && blocks[ 0 ].name === name ) {
return;
}

return createBlock(
name,
{},
blocks.map( ( block ) =>
createBlock(
block.name,
block.attributes,
block.innerBlocks
)
)
);
},
},
],
},
};
1 change: 1 addition & 0 deletions packages/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@ _Parameters_

- _blocks_ `(Array|Object)`: Blocks array or block object.
- _name_ `string`: Block name.
- _isGroupingBlock_ `boolean`: Set to true to bypass default grouping block selection.

_Returns_

Expand Down
10 changes: 6 additions & 4 deletions packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,13 @@ export function getBlockTransforms( direction, blockTypeOrName ) {
/**
* Switch one or more blocks into one or more blocks of the new block type.
*
* @param {Array|Object} blocks Blocks array or block object.
* @param {string} name Block name.
* @param {Array|Object} blocks Blocks array or block object.
* @param {string} name Block name.
* @param {boolean} isGroupingBlock Set to true to bypass default grouping block selection.
*
* @return {?Array} Array of blocks or null.
*/
export function switchToBlockType( blocks, name ) {
export function switchToBlockType( blocks, name, isGroupingBlock ) {
const blocksArray = castArray( blocks );
const isMultiBlock = blocksArray.length > 1;
const firstBlock = blocksArray[ 0 ];
Expand All @@ -413,7 +414,8 @@ export function switchToBlockType( blocks, name ) {
if (
! isContainerGroupBlock( name ) &&
isMultiBlock &&
! isBlockSelectionOfSameType( blocksArray )
! isBlockSelectionOfSameType( blocksArray ) &&
! isGroupingBlock
) {
return null;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/edit-site/src/components/block-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import {
BlockList,
ButtonBlockerAppender,
} from '@wordpress/block-editor';
import { Popover } from '@wordpress/components';

/**
* Internal dependencies
*/
import { useEditorContext } from '../editor';
import TemplatePartConverter from '../template-part-converter';
import NavigateToLink from '../navigate-to-link';
import Sidebar from '../sidebar';

Expand Down Expand Up @@ -68,7 +70,9 @@ export default function BlockEditor() {
onChange={ onChange }
useSubRegistry={ false }
>
<Popover.Slot name="block-toolbar" />
<BlockEditorKeyboardShortcuts />
<TemplatePartConverter />
<URLPopover.LinkViewer.Fill>
{ useCallback(
( fillProps ) => (
Expand Down
43 changes: 43 additions & 0 deletions packages/edit-site/src/components/template-part-converter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { BlockSettingsMenuControls } from '@wordpress/block-editor';
import { MenuItem } from '@wordpress/components';
import { switchToBlockType } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';

export default function TemplatePartConverter() {
const { clientIds, blocks } = useSelect( ( select ) => {
const { getSelectedBlockClientIds, getBlocksByClientId } = select(
'core/block-editor'
);
const selectedBlockClientIds = getSelectedBlockClientIds();
return {
clientIds: selectedBlockClientIds,
blocks: getBlocksByClientId( selectedBlockClientIds ),
};
} );
const { replaceBlocks } = useDispatch( 'core/block-editor' );
return (
<BlockSettingsMenuControls>
{ ( { onClose } ) => (
<MenuItem
onClick={ () => {
replaceBlocks(
clientIds,
switchToBlockType(
blocks,
'core/template-part',
true
)
);
onClose();
} }
>
{ __( 'Make template part' ) }
</MenuItem>
) }
</BlockSettingsMenuControls>
);
}

0 comments on commit 927b609

Please sign in to comment.