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
21 changes: 3 additions & 18 deletions packages/block-library/src/freeform/convert-to-blocks-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,12 @@
*/
import { __ } from '@wordpress/i18n';
import { ToolbarButton } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { rawHandler, serialize } from '@wordpress/blocks';
import { store as blockEditorStore } from '@wordpress/block-editor';

const ConvertToBlocksButton = ( { clientId } ) => {
const { replaceBlocks } = useDispatch( blockEditorStore );
const block = useSelect(
( select ) => {
return select( blockEditorStore ).getBlock( clientId );
},
[ clientId ]
);
import { rawHandler } from '@wordpress/blocks';

const ConvertToBlocksButton = ( { content, onReplace } ) => {
return (
<ToolbarButton
onClick={ () =>
replaceBlocks(
block.clientId,
rawHandler( { HTML: serialize( block ) } )
)
}
onClick={ () => onReplace( rawHandler( { HTML: content } ) ) }
>
{ __( 'Convert to blocks' ) }
</ToolbarButton>
Expand Down
8 changes: 6 additions & 2 deletions packages/block-library/src/freeform/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default function FreeformEdit( {
attributes,
setAttributes,
clientId,
onReplace,
} ) {
const { content } = attributes;
const [ isOpen, setOpen ] = useState( false );
Expand All @@ -49,7 +50,10 @@ export default function FreeformEdit( {
{ canRemove && ! isDeprecationMode && (
<BlockControls>
<ToolbarGroup>
<ConvertToBlocksButton clientId={ clientId } />
<ConvertToBlocksButton
content={ content }
onReplace={ onReplace }
/>
</ToolbarGroup>
</BlockControls>
) }
Expand All @@ -66,8 +70,8 @@ export default function FreeformEdit( {
<div { ...useBlockProps() }>
{ isDeprecationMode && canRemove && content && (
<MigrationNotice
clientId={ clientId }
content={ content }
onReplace={ onReplace }
/>
) }
{ content ? (
Expand Down
34 changes: 10 additions & 24 deletions packages/block-library/src/freeform/migration-notice.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* WordPress dependencies
*/
import { store as blockEditorStore, Warning } from '@wordpress/block-editor';
import { Warning } from '@wordpress/block-editor';
import { Button } from '@wordpress/components';
import { useDispatch } from '@wordpress/data';
import { createBlock, rawHandler, serialize } from '@wordpress/blocks';
import { createBlock, rawHandler } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';

/**
Expand All @@ -16,40 +15,27 @@ import { __ } from '@wordpress/i18n';
* and offers two migration actions - a primary "Convert to blocks", and a
* secondary "Convert to Custom HTML".
*
* @param {Object} props
* @param {string} props.clientId Client ID of the Classic block.
* @param {string} props.content Raw HTML content of the Classic block.
* @param {Object} props
* @param {string} props.content Raw HTML content of the Classic block.
* @param {Function} props.onReplace Replace the current block with the given blocks.
*/
export default function MigrationNotice( { clientId, content } ) {
const { replaceBlocks } = useDispatch( blockEditorStore );

const convertToBlocks = () => {
replaceBlocks(
clientId,
rawHandler( {
HTML: serialize( createBlock( 'core/freeform', { content } ) ),
} )
);
};

const convertToHtmlBlock = () => {
replaceBlocks( clientId, createBlock( 'core/html', { content } ) );
};

export default function MigrationNotice( { content, onReplace } ) {
const actions = [
<Button
__next40pxDefaultSize
key="convert-to-blocks"
variant="primary"
onClick={ convertToBlocks }
onClick={ () => onReplace( rawHandler( { HTML: content } ) ) }
>
{ __( 'Convert to blocks' ) }
</Button>,
<Button
__next40pxDefaultSize
key="convert-to-html"
variant="secondary"
onClick={ convertToHtmlBlock }
onClick={ () =>
onReplace( createBlock( 'core/html', { content } ) )
}
>
{ __( 'Convert to HTML' ) }
</Button>,
Expand Down
Loading