Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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