| 1 |
import {BlockInstance, getBlockType, getUnregisteredTypeHandlerName} from '@wordpress/blocks'; |
| 2 |
|
| 3 |
/** |
| 4 |
* Replaces any missing blocks with the original block type. |
| 5 |
* |
| 6 |
* This is used to preserve the original block type definition when persisting the blocks to the database. |
| 7 |
* |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
export default function revertMissingBlocks(blocks: BlockInstance[]) { |
| 11 |
blocks.forEach((sectionBlock, sectionBlockIndex) => { |
| 12 |
sectionBlock.innerBlocks?.forEach((innerBlock, innerBlockIndex) => { |
| 13 |
const blockType = getBlockType(innerBlock.name); |
| 14 |
|
| 15 |
if (blockType.name === getUnregisteredTypeHandlerName() && innerBlock.attributes?.originalContent) { |
| 16 |
blocks[sectionBlockIndex].innerBlocks[innerBlockIndex] = JSON.parse( |
| 17 |
innerBlock.attributes.originalContent |
| 18 |
); |
| 19 |
} |
| 20 |
}); |
| 21 |
}); |
| 22 |
} |
| 23 |
|