Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#30930 - Make it easier to select "Edit visually" when in "Edit as HTML #41516

Merged
merged 7 commits into from
Jun 21, 2022
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* WordPress dependencies
*/
import { rawHandler, getBlockContent } from '@wordpress/blocks';
import { ToolbarButton } 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 BlockVisuallyConvertButton( { clientIds, ...props } ) {
ZebulanStanphill marked this conversation as resolved.
Show resolved Hide resolved
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 (
<ToolbarButton
title={ __( 'Edit visually' ) }
onClick={ onClick }
{ ...props }
>
{ __( 'Edit visually' ) }
</ToolbarButton>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ToolbarGroup, ToolbarItem } from '@wordpress/components';
* Internal dependencies
*/
import BlockSettingsDropdown from './block-settings-dropdown';
import BlockVisuallyConvertButton from './block-visually-convert-button';

export function BlockSettingsMenu( { clientIds, ...props } ) {
return (
Expand All @@ -20,6 +21,7 @@ export function BlockSettingsMenu( { clientIds, ...props } ) {
/>
) }
</ToolbarItem>
<BlockVisuallyConvertButton clientIds={ clientIds } { ...props } />
</ToolbarGroup>
);
}
Expand Down