你可以使用"blocks.registerBlockType"函数来在编辑器内扩展核心块。下面是一个示例:
var el = wp.element.createElement,
registerBlockType = wp.blocks.registerBlockType,
RichText = wp.editor.RichText;
registerBlockType( 'core/my-custom-block', {
title: 'My Custom Block',
icon: 'text-page',
category: 'common',
attributes: {
content: {
type: 'string',
source: 'html',
selector: 'p',
}
},
edit: function( props ) {
var content = props.attributes.content;
function onChangeContent( newContent ) {
props.setAttributes( { content: newContent } );
}
return el(
RichText,
{
tagName: 'p',
className: props.className,
onChange: onChangeContent,
value: content,
}
);
},
save: function( props ) {
return el( RichText.Content, {
tagName: 'p',
value: props.attributes.content,
} );
},
} );
在这个示例中,我们使用"blocks.registerBlockType"函数来创建一个自定义块并扩展它。我们使用了"attributes"来定义"content"属性,然后在"edit"函数中渲染出一个可编辑的文本块。在"save"函数中,我们使用"RichText.Content"组件来展示已保存的内容。