Skip to content

Fix keyboard shortcuts (Copy/Cut/Delete) when using lasso selection#145

Open
bmouse116 wants to merge 2 commits into
mit-cml:mainfrom
bmouse116:fix/keyboard-shortcuts-lasso
Open

Fix keyboard shortcuts (Copy/Cut/Delete) when using lasso selection#145
bmouse116 wants to merge 2 commits into
mit-cml:mainfrom
bmouse116:fix/keyboard-shortcuts-lasso

Conversation

@bmouse116
Copy link
Copy Markdown

The basics

The details

Resolves

Fixes #144

Proposed Changes

Modified registerShortcutDelete, registerCopy, and registerCut in multiselect_shortcut.js.
The updated logic now checks the dragSelection map directly within the shortcut callbacks. If the map is not empty, the shortcuts will operate on the contained blocks/comments, even if Blockly.common.getSelected() does not return a MultiselectDraggable proxy instance at that moment.

Reason for Changes

Reason for Changes
In certain states — specifically when multiselect mode is toggled via an external UI button or when using Lasso selection — the internal dragSelection map becomes populated, but the global Blockly selection state might not yet be synchronized with the MultiselectDraggable wrapper.
Before this change, shortcuts would only work if getSelected() returned a MultiselectDraggable instance, causing them to fail during Lasso selection. This PR ensures that visual selection (the blocks you actually see highlighted) always matches the shortcut behavior.

Test Coverage

Manual testing was performed in Chrome:

  1. Enabled multiselect mode via an external toggle.
  2. Selected multiple blocks using Lasso (rectangular) selection.
  3. Verified that the Delete key successfully removes all selected blocks.
  4. Verified that Ctrl+C and Ctrl+V successfully copy and paste the entire selection.
  5. Verified that standard Shift+Click selection and single-block selection still function as expected without regressions.

Documentation

N/A

Additional Information

N/A

@mjgallag
Copy link
Copy Markdown
Contributor

mjgallag commented May 21, 2026

@bmouse116 thanks! Definitely need some tests to cover this fix though. I am wondered if based on your description perhaps it can be fixed on selection side since you say it is not properly returning a multiselect draggable when it should be. If it did perhaps the existing shortcut code would work? Wondering if we need more tests here to cover this if certain slection paths aren't correctly returning a multiselect draggable. Can you try adding selection tests for the paths you found to be failing here to show they are not returning a multiselect draggable when they should be.

@bmouse116
Copy link
Copy Markdown
Author

Hi @mjgallag
I have refactored the fix to address the root cause on the selection side as you suggested.
Changes
Selection Logic: Modified src/multiselect_controls.js to call this.updateMultiselect() within the elementselect and elementunselect subscriptions of the DragSelect instance. This ensures that every Lasso action triggers a global selection update in Blockly.
The shortcuts now work out-of-the-box using the original logic because getSelected() now returns the correct object.
E2E Tests: Added new test cases in test-e2e/selection/multiselect/block.spec.ts to verify:
That Blockly.common.getSelected() returns a MultiselectDraggable instance during Lasso operations.

That keyboard shortcuts (specifically Delete) function correctly after selecting blocks with the Lasso tool.
Verification
I verified the fix both with the new E2E tests (all passing) and by monitoring the global selection state in the console. After the changes, getSelected() correctly transitions from null to BlockSvg and then to MultiselectDraggable as the Lasso rectangle expands.

@mjgallag
Copy link
Copy Markdown
Contributor

Awesome. I am on vacation but will review upon my return, thank you!

@mjgallag mjgallag self-requested a review May 24, 2026 12:59
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.

Keyboard shortcuts (Copy/Cut/Delete) not working with Lasso selection in certain states

2 participants