Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Handle deleted blocks

Hakan Shehu11c6d6ac4a92
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<any>;
+ const blockIds = new Set<string>();
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<any>;
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<any>, block: NodeBlock) => {