admin管理员组文章数量:1122846
I have a simple block variation:
wp.blocks.registerBlockVariation(
'core/heading',
{
name: 'headline',
title: __('Big title'),
attributes: {
className: 'headline'
},
},
);
And I want to be able to transform from this variant to standard core/heading
and back. How to achieve this? Tried to get it from Handbook, but with no success.
Thanks a lot in advance!
I have a simple block variation:
wp.blocks.registerBlockVariation(
'core/heading',
{
name: 'headline',
title: __('Big title'),
attributes: {
className: 'headline'
},
},
);
And I want to be able to transform from this variant to standard core/heading
and back. How to achieve this? Tried to get it from Handbook, but with no success.
Thanks a lot in advance!
Share Improve this question edited Feb 24, 2022 at 14:19 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Feb 8, 2022 at 21:18 Karolína VyskočilováKarolína Vyskočilová 4291 gold badge8 silver badges23 bronze badges 3- This seems like it would be better implemented as a block style: developer.wordpress.org/block-editor/reference-guides/block-api/… Then you’d be able to just set the style to default. – Jacob Peattie Commented Feb 8, 2022 at 23:29
- @JacobPeattie Thanks for the tip, but this doesn't answer my question. The problem is simplified (and I can think about many other usages) with block styles is that you have only one CSS class applied - with variants you can set it up way more, besides - only one Style can be applied at the time. – Karolína Vyskočilová Commented Feb 9, 2022 at 9:35
- 1 That’s why it didn’t post it as an answer… – Jacob Peattie Commented Feb 9, 2022 at 9:36
2 Answers
Reset to default 2I've found that there's also a way to set a 'default' to revert to, using the same wp.blocks.registerBlockVariation()
function. Here's a variant on the column block that I set up, along with a 'default' that reverts the styling successfully:
wp.blocks.registerBlockVariation(
'core/column',
[
{
name: 'default',
title: 'Default',
attributes: {
className: '',
style: false
},
scope: ['transform'],
isDefault: true,
isActive: ['className']
},
{
name: 'card-column',
title: 'Card Column',
attributes: {
className: 'is-style-card-column',
style: {
color: {
background: '#ffffff',
},
spacing: {
padding: {
top:'var:preset|spacing|50',
left:'var:preset|spacing|30',
bottom:'var:preset|spacing|50',
right:'var:preset|spacing|30'
},
},
border: {
radius: '0.75rem',
color: 'var:preset|color|vivid-cyan-blue',
width: '2px',
style: 'solid'
}
}
},
scope: ['transform'],
isActive: ['className']
},
],
);
Setting the style attribute to false
succeeded in reverting the styles, but the real clincher was setting the className
to an empty string and making sure that the isActive
used className
. Without the isActive
setting, the 'Default' variation was not being selected when choosing the variant. Hope this helps!
Carolina Nymark pointed out, that there is possibility to filter transforms as any other settings, so one can modify the transformation like this:
const { addFilter } = wp.hooks;
const { assign, merge } = lodash;
const { createBlock } = wp.blocks;
function transformHeading(settings, name) {
// Filter only heading.
if (name !== 'core/heading') {
return settings;
}
return assign({}, settings, {
transforms: merge(settings.transforms, {
from: [
{
type: 'block',
blocks: ['core/paragraph'],
transform: ({ content }) => {
return createBlock('core/heading', {
content,
className: 'headline',
});
},
}
],
}),
});
}
addFilter(
'blocks.registerBlockType',
'my-filters/transforms',
transformHeading,
);
But there are still a few things that you can't modify:
- Name of the transformation (it's still named by the default block, not the variation)
- If the transformation for the "to" block is already in among the transforms, you'll replace it (you can't have two transforms to the same block)
- as of WP 5.9.2 it doesn't support transform within the same block - doesn't migrate the content (not sure why)
本文标签: How to transform block variant to default version
版权声明:本文标题:How to transform block variant to default version? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736291135a1928660.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论