Skip to content

Handle BLOCK_CREATE events for nested blocks in snippets#413

Merged
tracygardner merged 3 commits intomainfrom
claude/fix-nested-mesh-visibility-9eedJ
Mar 18, 2026
Merged

Handle BLOCK_CREATE events for nested blocks in snippets#413
tracygardner merged 3 commits intomainfrom
claude/fix-nested-mesh-visibility-9eedJ

Conversation

@tracygardner
Copy link
Contributor

@tracygardner tracygardner commented Mar 18, 2026

Summary

This PR fixes handling of lifecycle events for blocks that are created as part of a snippet creation. Previously, blocks nested inside a snippet's root block would not properly handle their creation events because the event's blockId field only contains the root block's ID, not the nested blocks' IDs.

Key Changes

  • Added detection for BLOCK_CREATE events where the current block's ID is in the changeEvent.ids array (indicating the block was created as part of a multi-block creation like snippet instantiation)
  • Updated lifecycle event handling in three locations (blocks/models.js and blocks/blocks.js) to trigger handleMeshLifecycleChange for both:
    • Direct block events (where changeEvent.blockId === block.id)
    • Nested block creation events (where the block ID is in changeEvent.ids)
  • Applied consistent logic across all affected block model definitions

Implementation Details

The fix introduces a reusable check that validates:

  1. The event type is Blockly.Events.BLOCK_CREATE
  2. The event has an ids array property
  3. The current block's ID is included in that array

This allows blocks to properly initialize their mesh lifecycle state when created as part of a snippet, ensuring consistent behavior whether a block is created individually or as part of a larger structure.

https://claude.ai/code/session_01FZ6vktw81VhbPuv9QePcJE

Summary by CodeRabbit

  • Bug Fixes
    • Improved block lifecycle handling so mesh lifecycle updates are detected for blocks created directly or as part of nested/snippet creations, ensuring consistent behavior for newly created or moved blocks.
    • Allow mesh creation to proceed during loading when the change event is marked for undo, preserving expected undo/redo behavior during load.

When a snippet (e.g. box or character) is dropped from the toolbox, Blockly
fires a single BLOCK_CREATE event where changeEvent.blockId is the root block
(e.g. `start`) but changeEvent.ids includes all created block IDs including
nested ones like `create_box` or `load_character`.

The block handlers were only calling handleMeshLifecycleChange when
changeEvent.blockId === block.id, so nested creation blocks never got their
meshes created on drop. Sky/ground blocks worked because they are in the
special-cased list in updateOrCreateMeshFromBlock.

Fix the guards in handleBlockChange (covers all create_* shape blocks) and
the individual handlers for load_character, load_multi_object, and load_model
to also trigger handleMeshLifecycleChange when the block's own id is found
in changeEvent.ids during a BLOCK_CREATE event.

https://claude.ai/code/session_01FZ6vktw81VhbPuv9QePcJE
@coderabbitai
Copy link

coderabbitai bot commented Mar 18, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0abd3a3f-fb2b-4fc4-b7ba-15c8b6d9397b

📥 Commits

Reviewing files that changed from the base of the PR and between 91ed3e6 and 169eb10.

📒 Files selected for processing (1)
  • ui/blockmesh.js

📝 Walkthrough

Walkthrough

Lifecycle handling extended so BLOCK_CREATE events that include the current block's ID in changeEvent.ids are treated like direct targets; mesh lifecycle handlers now trigger when changeEvent.blockId === id OR the block was created as part of a snippet (present in ids).

Changes

Cohort / File(s) Summary
Block lifecycle logic
blocks/blocks.js, blocks/models.js
Added isThisBlockCreated detection for BLOCK_CREATE (checks changeEvent.ids includes the block's id) and expanded conditionals to call handleMeshLifecycleChange when changeEvent.blockId === id OR the block was created within a snippet. No public API changes.
Load-time/undo gating
ui/blockmesh.js
Broadened early-return in updateOrCreateMeshFromBlock: it now only short-circuits when window.loadingCode is true AND changeEvent.recordUndo is falsy, allowing mesh creation to proceed during loading when the event records an undo. Existing disposed check unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I nibble on changes, tidy and neat,
When snippets birth blocks, the lifecycle meets,
IDs hop in line, the mesh blinks awake,
Small code-footsteps over logic's greenake,
🥕✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: fixing lifecycle event handling for blocks created as part of snippet instantiation by detecting BLOCK_CREATE events for nested blocks.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/fix-nested-mesh-visibility-9eedJ
📝 Coding Plan
  • Generate coding plan for human review comments

Comment @coderabbitai help to get the list of available commands and usage tips.

claude added 2 commits March 18, 2026 14:35
When the Snippets flyout is open, window.loadingCode is set to true,
which caused handleMeshLifecycleChange to skip mesh creation for all
BLOCK_CREATE events — including when the user actually drops a snippet.

Sky/ground had a special bypass; box snippets live in the Physics
category (loadingCode=false). Characters in the Snippets category were
silently blocked.

Fix: only skip when loadingCode=true AND recordUndo=false (file loading).
User-initiated drops always have recordUndo=true, so they now proceed
to create the mesh even while the Snippets flyout is open.

https://claude.ai/code/session_01FZ6vktw81VhbPuv9QePcJE
updateOrCreateMeshFromBlock also checked window.loadingCode unconditionally,
which prevented createMeshOnCanvas from being called even after the fix in
handleMeshLifecycleChange. Apply the same recordUndo distinction: skip only
when loading saved code (recordUndo=false), not when user drops a block.

https://claude.ai/code/session_01FZ6vktw81VhbPuv9QePcJE
@tracygardner tracygardner merged commit 30e7595 into main Mar 18, 2026
6 checks passed
@tracygardner tracygardner deleted the claude/fix-nested-mesh-visibility-9eedJ branch March 19, 2026 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants