-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Convert a Classic Block into Multiple Blocks #3179
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { IconButton } from '@wordpress/components'; | ||
import { getUnknownTypeHandlerName, rawHandler, serialize } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getBlock } from '../selectors'; | ||
import { replaceBlocks } from '../actions'; | ||
|
||
export function UnknownConverter( { block, convertToBlocks, small } ) { | ||
if ( ! block || getUnknownTypeHandlerName() !== block.name ) { | ||
return null; | ||
} | ||
|
||
const label = __( 'Convert to blocks' ); | ||
|
||
return ( | ||
<IconButton | ||
className="editor-block-settings-menu__control" | ||
onClick={ () => convertToBlocks( block ) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not entirely sure what the best way is here to avoid passing arguments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the desire to avoid passing the argument? If you mean so that the component doesn't need to be aware of a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make the component not know about the block? Don't you have to return |
||
icon="screenoptions" | ||
label={ small ? label : undefined } | ||
> | ||
{ ! small && label } | ||
</IconButton> | ||
); | ||
} | ||
|
||
export default connect( | ||
( state, { uid } ) => ( { | ||
block: getBlock( state, uid ), | ||
} ), | ||
( dispatch, { uid } ) => ( { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have ( dispatch, { block, uid } ) => ( {
convertToBlocks() {
dispatch( replaceBlocks( uid, rawHandler( {
HTML: serialize( block ),
inline: false,
} ) ) );
},
} ) Although @aduth might be correct that you would have to use 3rd param Yes, 2nd param is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another funky way to do the same is to wrap this with HOC const withBlock = component => connect( ( state, { uid } ) => ( {
block: getBlock( state, uid ),
} ) )( component );
export default connect(
null,
( dispatch, { block, uid } ) => ( {
convertToBlocks() {
dispatch( replaceBlocks( uid, rawHandler( {
HTML: serialize( block ),
inline: false,
} ) ) );
},
} )
)( withBlock( UnknownConverter ) ); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with keeping as is :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
convertToBlocks( block ) { | ||
dispatch( replaceBlocks( uid, rawHandler( { | ||
HTML: serialize( block ), | ||
inline: false, | ||
} ) ) ); | ||
}, | ||
} ) | ||
)( UnknownConverter ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to include this in BlockToolbar (for mobile controls)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not entirely sure what you mean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The block settings menu is hidden on mobile and these controls are shown in the block toolbar instead here lob/master/editor/block-toolbar/index.js#L143 Thinking we could do the same with this control.