可能有一种更简单的方法可以做到这一点,如果没有,您应该在Gutenberg GH问题跟踪程序上打开一个问题(API不是一成不变的)。
Because the API is not set in stone, this answer may be useless when it\'s finally done and merged. The first and best place to ask GB questions is on GitHub
可以使用以下方法消除所有块:
wp.data.dispatch( \'core/editor\' ).resetBlocks([]);
然后可以通过编程方式创建新块:
let block = wp.blocks.createBlock( \'core/paragraph\' );
向其中添加文本:
block.attributes.content.push( \'hello world\' );
然后像这样插入:
wp.data.dispatch( \'core/editor\' ).insertBlocks( block );
<小时>
On further inspection, there\'s a simpler method:
let block = wp.blocks.createBlock( \'core/paragraph\', { content: \'test\' } );
wp.data.dispatch( \'core/editor\' ).insertBlocks( block );