Skip to content

[Google Blockly] do not save ids with copied blocks#67021

Merged
mikeharv merged 2 commits intostagingfrom
mike/function-copy
Jul 11, 2025
Merged

[Google Blockly] do not save ids with copied blocks#67021
mikeharv merged 2 commits intostagingfrom
mike/function-copy

Conversation

@mikeharv
Copy link
Copy Markdown
Contributor

@mikeharv mikeharv commented Jul 10, 2025

A user reported an issue where copying and pasting a block between functions was causing the originally copied block to get deleted: https://codeorg.zendesk.com/agent/tickets/545093

This is reproduced below:

before.mp4

This seems to be due to a conflict between two plugins we use: cross-tab copy/paste and shareable procedures.

As a reminder, this our set up:

  1. main student workspace
  2. hidden procedures workspace
  3. function editor workspace (we mirror all events to the hidden workspace)

This is the flow:

  1. copy a block with a given id on the function editor w.s. (this block/id are also used on the hidden workspace)
  2. open a different function and paste the block (this action gets mirrored to the hidden workspace)
  3. the result is that the original copied block is gone

I believe this is happening because you can't have two blocks on the same workspace with the same id, so the old one gets removed. Ordinarily, if a block id is in use, Blockly will generate another. I suspect that doesn't happen because of the way the block creation event is mirrored, which doesn't give Blockly a chance to generate a new id.

To fix this, I was able to override BlockSvg.toCopyData so that it explicitly doesn't save ids. This means Blockly will always generate a new id for a pasted block. The original method from Core is defined here:

https://github.com/google/blockly/blob/60b7ee1325fc366d8ae6dd1699dd9fc95b208f3c/core/block_svg.ts#L956-L968

The result is that a block can now be safely copied between workspace.

after.mp4

Blockly issue filed:

Links

  • Jira:

Testing story

Deployment strategy

Follow-up work

Privacy

Security

Caching

PR Checklist:

  • Tests provide adequate coverage
  • Privacy and Security impacts have been assessed
  • Code is well-commented
  • New features are translatable or updates will not break translations
  • Relevant documentation has been added or updated
  • User impact is well-understood and desirable
  • Pull Request is labeled appropriately
  • Follow-up work items (including potential tech debt) are tracked and linked

addCoordinates: true,
addNextBlocks: false,
// We intentionally do not save IDs, because this can break student code
// on the hidden procedure definition workspace.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can you add a link to the blockly ticket here?

@mikeharv mikeharv merged commit 948f225 into staging Jul 11, 2025
5 of 6 checks passed
@mikeharv mikeharv deleted the mike/function-copy branch July 11, 2025 16:18
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