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
@@ -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 (
<ToolbarGroup>
<ToolbarButton onClick={ onClick } { ...props }>
{ __( 'Edit visually' ) }
</ToolbarButton>
</ToolbarGroup>
);
}
26 changes: 15 additions & 11 deletions packages/block-editor/src/components/block-settings-menu/index.js
Expand Up @@ -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 (
<ToolbarGroup>
<ToolbarItem>
{ ( toggleProps ) => (
<BlockSettingsDropdown
clientIds={ clientIds }
toggleProps={ toggleProps }
{ ...props }
/>
) }
</ToolbarItem>
</ToolbarGroup>
<>
<BlockEditVisuallyButton clientIds={ clientIds } { ...props } />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally got around to actually trying out this PR. It works fine, but I now see that the button appears after the ellipsis. Shouldn't it be placed before it?

image

Besides that, this PR is good to go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZebulanStanphill I did it according to the design from that issue #30930

IMHO it's ok, but we can change the order

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also suggest putting the button before the ellipsis (with a divider between) to avoid breaking a widespread UI convention.

<ToolbarGroup>
<ToolbarItem>
{ ( toggleProps ) => (
<BlockSettingsDropdown
clientIds={ clientIds }
toggleProps={ toggleProps }
{ ...props }
/>
) }
</ToolbarItem>
</ToolbarGroup>
</>
);
}

Expand Down