Skip to content

Fix for Bug: "Insert blocks by name" creates two undo points when placing a block - #9029

Open
griffpatch wants to merge 2 commits into
ScratchAddons:masterfrom
griffpatch:feature/middle-click-popup-undo-fix
Open

Fix for Bug: "Insert blocks by name" creates two undo points when placing a block#9029
griffpatch wants to merge 2 commits into
ScratchAddons:masterfrom
griffpatch:feature/middle-click-popup-undo-fix

Conversation

@griffpatch

Copy link
Copy Markdown
Contributor

Bug: "Insert blocks by name" creates two undo points when placing a block

Summary
When using the Insert blocks by name addon to place a block, pressing Ctrl+Z once doesn't remove it - instead the block jumps back to where your cursor was when you opened the search popup. A second Ctrl+Z is needed to actually remove the block. Expected behaviour is one Ctrl+Z removes the block entirely.

Bug: "Insert blocks by name" creates two undo points when placing a block

Summary
When using the Insert blocks by name addon to place a block, pressing Ctrl+Z once doesn't remove it - instead the block jumps back to where your cursor was when you opened the search popup. A second Ctrl+Z is needed to actually remove the block. Expected behaviour is one Ctrl+Z removes the block entirely.

Steps to reproduce

  1. Make sure the Insert blocks by name addon is enabled
  2. Open a Scratch project and go to the code editor
  3. Middle-click (or Shift-click) in the workspace to open the block search
  4. Search for any block (e.g. "move"), select it, and drag it onto the workspace
  5. Press Ctrl+Z - the block snaps back to the popup cursor position instead of disappearing
  6. Press Ctrl+Z again - the block is finally removed

Expected behaviour

A single Ctrl+Z removes the block entirely, as it does when dragging a block from the palette.

Root cause

The addon fires a BlockCreate event before starting the drag. Blockly's drag start creates a new event group for the drag, leaving BlockCreate in a separate group - so Scratch sees two distinct undo steps: one for the creation (at the popup cursor position), and one for the drop (where you released the block).

Fix

Fire BlockCreate after onDragStart so it joins the drag's event group. BlockCreate and BlockMove then undo together as a single step, matching the behaviour of blocks dragged from the palette.

…undo point

Previously, `BlockCreate` was fired before `onDragStart`. Blockly's
`onDragStart` calls `Events.setGroup(true)` internally, creating a new
event group for the drag. The `BlockCreate` had no group, so it became
a separate undo point from the `BlockMove` fired on drop — resulting in
two undo steps: one placing the block at the popup cursor, then another
at the drop position.

Fix: move the `BlockCreate` fire to after `onDragStart` (in both the
new and old Blockly paths). The drag group is still active at that
point, so `BlockCreate` and `BlockMove` share the same group and undo
together as a single step.
@DNin01 DNin01 added this to the April-May 2026 bugs milestone May 16, 2026

@mxmou mxmou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main changes look good and the fix works, but I don't understand some of the added ts-ignore comments.

Comment thread addons/middle-click-popup/userscript.js Outdated
Comment thread addons/middle-click-popup/userscript.js Outdated
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.

3 participants