forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Edit Site: Add convert to template part flow. (WordPress#20445)
* Edit Site: Add convert to template part flow. * auto create in placeholder if innerBlocks are present * add comment for auto-creation in placeholder * remove outdated JSDOC param * jsdoc format spacing * remove unnecessary popover * remove transform, define creation in converter * ensure blocks[0] exists (causing error in e2e) * wrap spinner in a placeholder for more consistent style * comment update Co-authored-by: Addison-Stavlo <stavz01@gmail.com>
- Loading branch information
1 parent
c33f007
commit 07b3ab2
Showing
4 changed files
with
84 additions
and
4 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
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
55 changes: 55 additions & 0 deletions
55
packages/edit-site/src/components/template-part-converter/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,55 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { BlockSettingsMenuControls } from '@wordpress/block-editor'; | ||
import { MenuItem } from '@wordpress/components'; | ||
import { createBlock } 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' ); | ||
|
||
// Avoid transforming a single `core/template-part` block. | ||
if ( blocks.length === 1 && blocks[ 0 ]?.name === 'core/template-part' ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<BlockSettingsMenuControls> | ||
{ ( { onClose } ) => ( | ||
<MenuItem | ||
onClick={ () => { | ||
replaceBlocks( | ||
clientIds, | ||
createBlock( | ||
'core/template-part', | ||
{}, | ||
blocks.map( ( block ) => | ||
createBlock( | ||
block.name, | ||
block.attributes, | ||
block.innerBlocks | ||
) | ||
) | ||
) | ||
); | ||
onClose(); | ||
} } | ||
> | ||
{ __( 'Make template part' ) } | ||
</MenuItem> | ||
) } | ||
</BlockSettingsMenuControls> | ||
); | ||
} |