-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Proposal: Block copy filter #49464
Proposal: Block copy filter #49464
Conversation
Size Change: +11 B (0%) Total Size: 1.34 MB
ℹ️ View Unchanged
|
I have this wild idea I wanted to share. Would it fit as a new type of block transforms, e.g. transforms: {
from: [
{
type: 'copy',
transform: ( attributes ) => {
return createBlock( 'my-plugin/block', {
...attributes,
foo: attributes.foo + 1,
} );
},
},
]
}, I don't know if it conceptually fully makes sense to put in the bucket of transforms, but maybe we wouldn't need a new filter as folks could use the existing |
Actually, I figured out that we could use better syntax by replicating the transforms: {
copy( attributes ) {
return createBlock( 'my-plugin/block', {
...attributes,
foo: attributes.foo + 1,
} );
},
}, This way it would be also simple to replace the |
Yes, I think |
Neat idea. It seems within the spirit of what |
What and why
Adds a new filter that lets extenders customise what happens when the block is duplicated.
The motivation for this is to handle cases such as #29693 where a block wants to track a foreign key of some kind.
It's not implemented in this PR, but it also would let us move away from using
__internalWidgetId
which is how the widget editor tracks which widget entity should be updated when a block is modified. It's a hack that works because__internalWidgetId
does not appear in theblock.json
and so is filtered out when the block is duplicated. Using acopy
function like above would make this behaviour more explicit.How to test
View instructions
Paste this into DevTools:
You will then have a Product block that simulates the use case in #29693. Insert the block and notice that a product with ID 1 is created. Duplicate the block and notice that a product with ID 2 is created.