Skip to content

Fix(embed-block): Only call setAttributes() when attrs change #68141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025

Conversation

chrisbellboy
Copy link
Contributor

@chrisbellboy chrisbellboy commented Dec 19, 2024

What?

Fixes: #61778.

Prevents setAttributes() from being called whenever the block remounts and no attributes have changed.

Why?

Due to the way setAttributes() currently works and how the embed block calls setAttributes() with a new shallow copy of attributes whenever the block is remounted, the following steps result in an ugly bug:

  1. Select multiple blocks that include either multiple embed blocks, or an embed block alongside an image block.
  2. And then drag these blocks to a location where they get remounted, such as dragging them inside a group block or outside their parent block.

This causes the full set of attributes from the last embed block selected to be set to all the other dragged embed + image blocks, changing them all into duplicate blocks of the last embed block.

The problem is due to a combination of:

  1. setAttributes() updates all passed attributes to all selected blocks, this was intended for mass block option editing. However, this is not always the desired behaviour.
  2. Whenever the embed block is remounted, it calls setAttributes() using a new shallow copy of the full set of attributes.

How?

The current setAttributes() implementation has desired and undesired effects, and needs to be expanded upon to address the undesired effects. That could indirectly fix this embed block remounting situation.

However, the embed block should not be trying to update its attributes upon every block remount.

This PR Prevents the embed block from calling setAttributes() when no attributes have been changed.

Testing Instructions

  1. Open a post or page.
  2. Add an embed block and and image block.
  3. Add a separate group block.
  4. Select the embed and image blocks, and drag them inside the group block.

Without this PR, the image block would get turned into an embed block as its attributes are changed to be the same as the embed block attributes. Causing the image block to fail and then automatically gets converted into an embed block.

Copy link

github-actions bot commented Dec 19, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: chrisbellboy <chrisdotdotdot@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: cbirdsong <cbirdsong@git.wordpress.org>
Co-authored-by: stronenv <vevas@git.wordpress.org>
Co-authored-by: jeflopodev <jeflopodev@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Mamaduka Mamaduka added [Type] Bug An existing feature does not function as intended [Block] Embed Affects the Embed Block labels Jan 12, 2025
@Mamaduka Mamaduka self-requested a review January 23, 2025 10:01
@Mamaduka
Copy link
Member

Thanks for contributing, @chrisbellboy!

Are you rebasing this branch on top of the last trunk?

P.S. The fix looks good.

@Mamaduka
Copy link
Member

Hi, @chrisbellboy

Do you mind rebasing this on top of the latest trunk? That should resolve some CI check problems and make it easier to test manually.

Thank you!

@chrisbellboy chrisbellboy force-pushed the fix/61778-embed-block branch from 78d824f to 1ad0256 Compare March 25, 2025 10:49
@chrisbellboy
Copy link
Contributor Author

Hi @Mamaduka Of course, no problem, all done 👍

And apologies for not getting back to your previous message sooner!

@Mamaduka
Copy link
Member

Thank you, @chrisbellboy!

I'll try to review and test this properly ASAP.

Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

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

Thanks for contributing, @chrisbellboy!

The fix works as expected ✅

@Mamaduka Mamaduka added the props-bot Adding this label triggers the Props Bot workflow for a PR. label Mar 26, 2025
@github-actions github-actions bot removed the props-bot Adding this label triggers the Props Bot workflow for a PR. label Mar 26, 2025
@Mamaduka Mamaduka merged commit c99c02b into WordPress:trunk Mar 26, 2025
63 checks passed
@github-actions github-actions bot added this to the Gutenberg 20.6 milestone Mar 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Embed Affects the Embed Block [Type] Bug An existing feature does not function as intended
2 participants