diff --git a/main/blockhandling.js b/main/blockhandling.js index 49a176e64..d75a40871 100644 --- a/main/blockhandling.js +++ b/main/blockhandling.js @@ -527,12 +527,18 @@ export function initializeBlockHandling() { } // Purge deleted blocks from the registry, then dispatch to handlers. + // Guard: only remove if no live block with that ID exists. When + // workspaces.load() queues DELETE events during its internal clear() + // and flushes them after creating new blocks with the same IDs, the + // new handlers must not be evicted. if ( event.type === Blockly.Events.BLOCK_DELETE && Array.isArray(event.ids) ) { for (const id of event.ids) { - blockHandlerRegistry.delete(id); + if (!workspace.getBlockById(id)) { + blockHandlerRegistry.delete(id); + } } } diff --git a/main/files.js b/main/files.js index c4554ba1b..9f35c9b49 100644 --- a/main/files.js +++ b/main/files.js @@ -3,7 +3,6 @@ import { workspace } from "./blocklyinit.js"; import { translate } from "./translation.js"; import { getMetadata } from "meta-png"; import { AUTOSAVE_KEY } from "../config.js"; -import { blockHandlerRegistry } from "../blocks/blocks.js"; // Function to save the current workspace state export function saveWorkspace(workspace) { @@ -319,14 +318,7 @@ export function loadWorkspaceAndExecute(json, workspace, executeCallback) { // Validate JSON before loading into workspace const validatedJson = validateBlocklyJson(json); - // Clear workspace and handlers - const eventsWereEnabled = Blockly.Events.isEnabled(); - if (eventsWereEnabled) Blockly.Events.disable(); - workspace.clear(); - blockHandlerRegistry.clear(); - if (eventsWereEnabled) Blockly.Events.enable(); - - // Load the validated JSON + // Load the validated JSON Blockly.serialization.workspaces.load(validatedJson, workspace); workspace.scroll(0, 0);