Repository navigation
BlockInvalidWarning: Prefer canInsertBlockType and refactor to hooks. - #49052
Conversation
…sic block not insertable
9e1a3f7 to
56b9e12
Compare
|
Size Change: -12 B (0%) Total Size: 1.34 MB
ℹ️ View Unchanged
|
| const convertToClassic = useCallback( () => { | ||
| replaceBlock( block.clientId, blockToClassic( block ) ); | ||
| }, [ block, replaceBlock ] ); | ||
|
|
||
| const convertToHTML = useCallback( | ||
| () => replaceBlock( block.clientId, blockToHTML( block ) ), | ||
| [ block, replaceBlock ] | ||
| ); |
There was a problem hiding this comment.
Suggestion (non-blocking): We can inline these inside useMemo and use a single method for memoization; also eliminate the blockToClassic and blockToHTML helpers.
There was a problem hiding this comment.
Yeah, I thought about that, tested it out in 9099bc1
| const attemptBlockRecovery = useCallback( () => { | ||
| replaceBlock( block.clientId, recoverBlock( block ) ); | ||
| }, [ block, replaceBlock ] ); |
There was a problem hiding this comment.
Nit: There's no need for the useCallback for this function; we can inline it. The button component isn't memoized and will re-render alongside the parent.
There was a problem hiding this comment.
Yeah, I realise that. I did it for consistency, as otherwise the code looked ugly 😄
|
|
||
| const convertToBlocks = useCallback( | ||
| () => replaceBlock( block.clientId, blockToBlocks( block ) ), | ||
| const convert = useMemo( |
There was a problem hiding this comment.
I could destructure it, but I decided to keep it this way as it allows me to pass a single convert value into the following useMemo dependency array..
|
Can this take in account the issue #49005? |
Thanks for mentioning that one, I don't think I'll tackle that in this PR, as this is now finalized, but I'll have a think about how that one can be solved. |
What?
Follow up to #49051.
This component should really use
canInsertBlockTypefor checking whether a block can be converted to HTML or Classic blocks.At the moment it checks whether the block type is registered, but this doesn't handle situations like
allowedBlocksfor inner blocks.I've also refactored this component to use hooks.
Why?
It should prevent any issues, like a user accidentally inserting an HTML or Classic blocks into a Buttons block.
How?
Use the
canInsertBlockTypeselector instead of thegetBlockTypeTesting Instructions
Test 1
Test 2
trunkafter the refactorScreenshots or screencast