From 74ccbf242c793fd267db2d937129cf3029ceb6d0 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 25 Apr 2026 09:41:55 +0000 Subject: [PATCH] Fix mesh not updating after reloading same project MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit workspaces.load() disables events, clears the workspace (queueing BLOCK_DELETE events), creates new blocks (registering handlers), then re-enables events — flushing the queued deletes. Because the new blocks share the same IDs as the old ones, the flushed DELETE events were evicting the freshly registered handlers, leaving the registry empty. Guard the DELETE cleanup in blockhandling.js: only remove a handler if no live block with that ID currently exists. When a new block has already claimed the same ID the handler is preserved. This also reverts the workaround in files.js that disabled events around workspace.clear() (which broke Blockly's trashcan by suppressing the DELETE events it relies on to save deleted blocks). https://claude.ai/code/session_01E7VhufX5kQJmQmzgQr2HQp --- main/blockhandling.js | 8 +++++++- main/files.js | 10 +--------- 2 files changed, 8 insertions(+), 10 deletions(-) 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);