Skip to content

Commit

Permalink
Block Editor: Simplify BlockHTMLConvertButton (#54972)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed Oct 2, 2023
1 parent 4968465 commit f91f0b3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { MenuItem } from '@wordpress/components';
import { rawHandler, getBlockContent } from '@wordpress/blocks';
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import BlockConvertButton from './block-convert-button';
import { store as blockEditorStore } from '../../store';

export default compose(
withSelect( ( select, { clientId } ) => {
const block = select( blockEditorStore ).getBlock( clientId );
function BlockHTMLConvertButton( { clientId } ) {
const block = useSelect(
( select ) => select( blockEditorStore ).getBlock( clientId ),
[ clientId ]
);
const { replaceBlocks } = useDispatch( blockEditorStore );

return {
block,
shouldRender: block && block.name === 'core/html',
};
} ),
withDispatch( ( dispatch, { block } ) => ( {
onClick: () =>
dispatch( blockEditorStore ).replaceBlocks(
block.clientId,
rawHandler( { HTML: getBlockContent( block ) } )
),
} ) )
)( BlockConvertButton );
if ( ! block || block.name !== 'core/html' ) {
return null;
}

return (
<MenuItem
onClick={ () =>
replaceBlocks(
clientId,
rawHandler( { HTML: getBlockContent( block ) } )
)
}
>
{ __( 'Convert to Blocks' ) }
</MenuItem>
);
}

export default BlockHTMLConvertButton;

0 comments on commit f91f0b3

Please sign in to comment.