Short Answer: 保存属性时,始终遵守属性type。所以如果你的属性是array
键入,然后将值另存为数组,例如。props.setAttributes( { innerContent: [ content ] } )
.
function hall_register_block() {
wp_register_script(
\'hall-block-server-side-render\',
\'/path/to/block.js\',
array( \'wp-i18n\', \'wp-blocks\', \'wp-block-editor\' ),
\'1.0\'
);
register_block_type( \'hall/block-server-side-render\', array(
\'render_callback\' => \'hall_render_inner_content\',
\'attributes\' => array(
\'innerContent\' => array(
\'type\' => \'array\',
// You should set a default value:
\'default\' => array(), // if the attribute is an array
// \'default\' => \'\', // if the attribute is a string
),
),
// Here, your code is missing the block\'s script:
\'editor_script\' => \'hall-block-server-side-render\',
) );
}
add_action( \'init\', \'hall_register_block\' );
您定义了
innerContent
属性作为数组,因此需要保存它(通过
props.setAttributes()
) 作为阵列:
function onChangeContent( content ) {
// Save the value as an array.
props.setAttributes( { innerContent: [ content ] } );
}
并忽略属性的
source
和
selector
属性,以便将属性保存到块的
comment delimiter.
attributes: {
innerContent: {
type: \'array\',
// No need for these two:
// source: \'children\',
// selector: \'p\'
}
},
但是,在块渲染回调中(
hall_render_inner_content()
), 您还需要在属性值中解析HTML(这不是一个简单的数组,例如
[ \'HTML here\', \'another HTML here\', \'etc etc\' ]
), 请注意,在该回调中
$content
是(或应该是)数组而不是字符串。
所以你应该change the attribute type to string
, i、 e.只需更改以上内容type: \'array\',
到type: \'string\',
. 这样,就不需要将属性保存为数组。
您应该导入RichText
从…起wp.blockEditor
而不是wp.editor
:
const { RichText } = wp.blockEditor; // use this
const { RichText } = wp.editor; // not this