diff --git a/packages/block-editor/src/components/block-settings-menu/block-edit-visually-button.js b/packages/block-editor/src/components/block-settings-menu/block-edit-visually-button.js new file mode 100644 index 00000000000000..87b28cb2ae26f6 --- /dev/null +++ b/packages/block-editor/src/components/block-settings-menu/block-edit-visually-button.js @@ -0,0 +1,52 @@ +/** + * WordPress dependencies + */ +import { rawHandler, getBlockContent } from '@wordpress/blocks'; +import { ToolbarButton, ToolbarGroup } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { useSelect, useDispatch } from '@wordpress/data'; +import { useCallback } from '@wordpress/element'; + +/** + * Internal dependencies + */ +import { store as blockEditorStore } from '../../store'; + +export default function BlockEditVisuallyButton( { clientIds, ...props } ) { + const { block, shouldRender } = useSelect( + ( select ) => { + const firstBlockClientId = clientIds[ 0 ]; + const { isBlockMultiSelected, getBlockMode, getBlock } = + select( blockEditorStore ); + const isSingleSelected = + ! isBlockMultiSelected( firstBlockClientId ); + const isHtmlMode = getBlockMode( firstBlockClientId ) === 'html'; + + return { + block: getBlock( firstBlockClientId ), + shouldRender: isSingleSelected && isHtmlMode, + }; + }, + [ clientIds[ 0 ] ] + ); + + const { replaceBlocks } = useDispatch( blockEditorStore ); + const onClick = useCallback( () => { + replaceBlocks( + block.clientId, + rawHandler( { HTML: getBlockContent( block ) } ) + ); + }, [ block ] ); + + if ( ! shouldRender ) { + return null; + } + + return ( + + + { __( 'Edit visually' ) } + + + ); +} diff --git a/packages/block-editor/src/components/block-settings-menu/index.js b/packages/block-editor/src/components/block-settings-menu/index.js index 0b0b1a775e736f..1b3f5fe0aa349e 100644 --- a/packages/block-editor/src/components/block-settings-menu/index.js +++ b/packages/block-editor/src/components/block-settings-menu/index.js @@ -7,20 +7,24 @@ import { ToolbarGroup, ToolbarItem } from '@wordpress/components'; * Internal dependencies */ import BlockSettingsDropdown from './block-settings-dropdown'; +import BlockEditVisuallyButton from './block-edit-visually-button'; export function BlockSettingsMenu( { clientIds, ...props } ) { return ( - - - { ( toggleProps ) => ( - - ) } - - + <> + + + + { ( toggleProps ) => ( + + ) } + + + ); }