diff --git a/desktop/src/lib/editor.ts b/desktop/src/lib/editor.ts index 032e6c10..ccc6ed1b 100644 --- a/desktop/src/lib/editor.ts +++ b/desktop/src/lib/editor.ts @@ -209,7 +209,10 @@ export const applyChangeToYDoc = (doc: Y.Doc, blocks: NodeBlock[]) => { } const contentMap = attributesMap.get('content') as Y.Map; + const blockIds = new Set(); for (const block of blocks) { + blockIds.add(block.id); + if (!contentMap.has(block.id)) { contentMap.set(block.id, new Y.Map()); } @@ -217,6 +220,14 @@ export const applyChangeToYDoc = (doc: Y.Doc, blocks: NodeBlock[]) => { const blockMap = contentMap.get(block.id) as Y.Map; applyBlockChangesToYDoc(blockMap, block); } + + const deletedBlockIds = Array.from(contentMap.keys()).filter( + (id) => !blockIds.has(id), + ); + + for (const id of deletedBlockIds) { + contentMap.delete(id); + } }; const applyBlockChangesToYDoc = (blockMap: Y.Map, block: NodeBlock) => {