WordPress的古腾堡.updateBlock

时间:2019-06-14 16:37:42

标签: wordpress plugins block wordpress-gutenberg

我相信有一种方法可以满足我的需要。但是我需要一个使用它的例子: https://developer.wordpress.org/block-editor/data/data-core-editor/#updateBlockAttributes 这怎么可能?也许有人有吗?

1 个答案:

答案 0 :(得分:1)

您可以在Block Editor Handbook中阅读该函数的(很小的)文档。这是我自己的代码在new slider block for wordpress上的一个实时示例。它使用Department高阶组件为组件提供动作。

set.seed(12)
data = Sonar[sample(nrow(Sonar)),]#reshufles the data
bound = floor(0.7 * nrow(data))
df_train = data[1:bound,]
df_test = data[(bound+1):nrow(data),]

要使用wordpress数据模块(该模块向客户端提供有关编辑器或块的数据),您还可以使用withDispatch-Module。在后端时,您可以例如转到浏览器控制台,然后键入withDispatch( ( dispatch, ownProps, registry ) => { return { updateEditable( isEditing ) { const { clientId, setAttributes } = ownProps; const { getBlockOrder } = registry.select( 'core/block-editor' ); const { updateBlockAttributes } = dispatch( 'core/block-editor' ); //update own isEditable setAttributes( { isEditing } ); const innerBlockIds = getBlockOrder( clientId ); innerBlockIds.forEach( ( innerBlockId ) => { updateBlockAttributes( innerBlockId, { isEditing, } ); } ); }, }; } ) 以测试该功能的作用。

您也可以在gutenberg github存储库中查看。核心块也使用wp.data。例如。在columns

如果您想了解有关gutenberg中数据处理的更多信息,可以阅读这篇很好的blob文章,其中介绍了wp.data api以及wp.data.dispatch( 'core/editor' ).updateBlockAttributes()updateBlockAttributes高阶组件的概念。