reactjs - 古腾堡块变化选择器不工作
问题描述
我正在尝试像在 WordPress Github 示例中一样添加 BlockVariationPicker :
import { useSelect } from '@wordpress/data';
import {
__experimentalBlockVariationPicker as BlockVariationPicker,
store as blockEditorStore,
} from '@wordpress/block-editor';
const MyBlockVariationPicker = ( { blockName } ) => {
const variations = useSelect(
( select ) => {
const { getBlockVariations } = select( blocksStore );
return getBlockVariations( blockName, 'block' );
},
[ blockName ]
);
return <BlockVariationPicker variations={ variations } />;
};
在我的编辑功能中,我添加:
{ MyBlockVariationPicker }
块变化选择器不显示。
我已经用范围块注册了我的集团变体:
registerBlockVariation(
'my/testimonial',
[
{
name: 'testimonial-1',
title: 'Testimonial 1',
scope: ['block'],
attributes: {
example: 'testimonial-1'
},
},
{
name: 'testimonial-2',
title: 'Testimonial 2',
scope: ['block'],
attributes: {
example: 'testimonial-2'
},
}
]
);
块变化应该显示,{ MyBlockVariationPicker }
但不显示。不幸的是,没有太多关于此的文档。我们如何使用 Github 示例中所示的块变化选择器渲染块的变化?
解决方案
Columns和Query块的使用和__experimentalBlockVariationPicker
它的一个非常好的组件/UI,我同意,它没有很多如何使用它的例子,很可能因为它仍然是“实验性的”并且仍然可能改变。
我发现 Columns 和 Query 块都BlockVariationPicker
通过检查当前块(按 clientId)是否包含任何 InnerBlocks 来显示;如果没有,BlockVariationPicker
则显示。在您自己的块中使用此组件时,您将需要一些属性或属性来检查是否已选择变体。
我已经使用my/testimonial
块 + 变体的结构并基于 BlockVariationPicker在 Columns 块中的实现方式组合了一个基本/工作示例:
import { get } from 'lodash';
import { useSelect } from '@wordpress/data';
import { registerBlockType, registerBlockVariation, store as blocksStore } from '@wordpress/blocks';
import { useBlockProps, __experimentalBlockVariationPicker as BlockVariationPicker } from '@wordpress/block-editor';
// Create our own BlockVariationPicker
const MyBlockVariationPicker = ({ name, setAttributes }) => { // Note: We need "name" and "setAttributes" from edit() props
const { blockType, variations, defaultVariation } = useSelect(
(select) => {
const { getBlockVariations, getBlockType, getDefaultBlockVariation } = select(blocksStore);
return {
blockType: getBlockType(name),
defaultVariation: getDefaultBlockVariation(name, 'block'),
variations: getBlockVariations(name, 'block')
};
},
[name]
);
return <BlockVariationPicker
variations={variations}
icon={get(blockType, ['icon', 'src'])}
label={get(blockType, ['title'])}
onSelect={(nextVariation = defaultVariation) => {
if (nextVariation.attributes) {
setAttributes(nextVariation.attributes); // Use setAttributes to set the selected variation attributes
}
}}
/>;
};
// Register the Block Variations
registerBlockVariation(
'my/testimonial',
[
{
name: 'testimonial-1',
title: 'Testimonial 1',
icon: 'admin-comments', // Added icon so the variation is visibly different (optional)
scope: ['block'],
attributes: {
example: 'testimonial-1'
},
isDefault: true
},
{
name: 'testimonial-2',
title: 'Testimonial 2',
icon: 'admin-links',
scope: ['block'],
attributes: {
example: 'testimonial-2'
},
}
]
);
registerBlockType('my/testimonial', {
title: 'My Testimonial',
keywords: ['testimonial'],
icon: 'admin-post',
attributes: {
example: {
type: "string", // no default set, example is "undefined"
}
},
edit(props) {
const { attributes, setAttributes } = props;
// If example is undefined, show Variation Picker
if (attributes.example === undefined) {
return (
<MyBlockVariationPicker {...props} />
);
}
// Otherwise show the Editor
return (<div {...useBlockProps()}><h2>{attributes.example}</h2></div>);
},
save: ({ attributes }) => {
return <div {...useBlockProps.save()}><h2>{attributes.example}</h2></div>;
}
})
推荐阅读
- android - 在 Android 上使用 requestNetwork 后如何恢复 Wi-Fi 连接
- python - 导入 csv 文件时列表索引超出范围
- linux - 为什么我的 Postgres 数据库工作了一段时间,然后重新启动后无法“启动服务器”?
- javascript - Google Apps 脚本 - 根据多个条件返回数据结果
- python - 时钟上的绘图线
- sql-server - 我的 sql server 没有维护计划向导
- javascript - 如何获得电子商务网站中订购商品的运费
- swift - Swiftui coredata foreach 滚动视图:删除按钮
- java - 如何在不实例化包含方法的类的情况下从其他类调用方法
- lua - 我怎样才能让它改变 IntValue 是永久性的?