WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.34k stars 4.13k forks source link

Add a way to modify what happens when you duplicate a block #35755

Open fabiankaegy opened 2 years ago

fabiankaegy commented 2 years ago

What problem does this address?

When you duplicate a block it copies over 1to1 all the attributes and nested blocks. Which is exactly what it should do in 99% of cases. But when you for example duplicate a block that has a custom anchor set for your block that now causes an issue since the id attribute no longer is unique and you have multiple elements on the page that share the same ID. This is a problem every time you need to store a unique identified in a block.

What is your proposed solution?

Like the migration callback function you can define for block depreciation it would be cool to add something similar to block duplication that allows you to override the behavior of core.

Alternatively, a hook that fires inside the cloning process would also solve the problem and would also work for things like the core anchor support.

gziolo commented 2 years ago

As noted in https://github.com/WordPress/gutenberg/pull/35757#issuecomment-957568383, we might want to take a different approach and provide options for block attributes to customize the behavior inside the framework when certain actions happen like duplication in this case.