diff --git a/packages/core-data/src/utils/crdt.ts b/packages/core-data/src/utils/crdt.ts index fc7b35b2082251..2d1ff58c158e44 100644 --- a/packages/core-data/src/utils/crdt.ts +++ b/packages/core-data/src/utils/crdt.ts @@ -65,6 +65,18 @@ export function applyPostChangesToCRDTDoc( switch ( key ) { case 'blocks': { + // Account for the case where the content has some invalid blocks, and as a result + // the blocks are undefined but the content is not. + if ( changes.content && ! changes.blocks ) { + try { + // Pull in the parsed blocks from the content, and use that as the new value. + newValue = parse( getRawValue( changes.content ) ); + } catch ( e ) { + // No-op as one of the blocks is expected to be invalid. + // This ensures that neither the blocks, nor the content are overwritten. + } + } + let currentBlocks = ymap.get( 'blocks' ) as Y.Array< YBlock >; // Initialize.