我正在尝试创建一个包含多个文本字段的块。这很容易,但当我选择要编辑的块时,两个工具栏都会显示出来。我搜索了文档,什么也找不到。
这里是最有效的JSX代码。它向块中添加标题、段落和图像。但当一个RichText元素处于焦点时,两个工具栏都会显示,这很烦人。
const { registerBlockType, RichText, AlignmentToolbar, BlockControls, InspectorControls, MediaUpload } = wp.blocks;
const { Button, TextControl } = wp.components;
registerBlockType( \'skulogic/header\', {
title: \'SKULogic Header\',
icon: \'universal-access-alt\',
category: \'layout\',
attributes: {
title: {
type: \'array\',
source: \'children\',
selector: \'h2\',
},
content: {
type: \'array\',
source: \'children\',
selector: \'p\',
},
mediaID: {
type: \'number\',
},
mediaURL: {
type: \'string\',
source: \'attribute\',
selector: \'div\',
attribute: \'data-src\',
},
},
edit( { attributes, className, setAttributes } ) {
const { mediaID, mediaURL, title, content } = attributes;
const onChangeTitle = value => {
setAttributes( { title: value } );
};
const onSelectImage = media => {
setAttributes( {
mediaURL: media.url,
mediaID: media.id,
} );
};
const onChangeContent = newContent => {
setAttributes( { content: newContent } );
};
return (
<div className={ className }>
<RichText
tagName="h2"
placeholder={ \'Page Header\' }
value={ title }
onChange={ onChangeTitle }
/>
<RichText
tagName="p"
placeholder={ \'Page Content\' }
value={ content }
onChange={ onChangeContent }
/>
<MediaUpload
onSelect={ onSelectImage }
type="image"
value={ mediaID }
render={ ( { open } ) => (
<Button className={ mediaID ? \'image-button\' : \'button button-large\' } onClick={ open }>
{ ! mediaID ? \'Upload Image\' : <img src={ mediaURL } /> }
</Button>
)}
/>
</div>
);
},
save( { attributes, className } ) {
const { mediaID, mediaURL, title, content } = attributes;
const divStyle = {};
if( mediaURL ) {
divStyle.backgroundImage = \'URL(\'+mediaURL+\')\';
}
return (
<div className={ className } data-src={ mediaURL } style={ divStyle }>
<h2>{ title }</h2>
<p>{ content }</p>
</div>
);
},
} );
请参阅随附的显示工具栏的屏幕截图,其中显示了
<h2>
和
<p>
元素。我只想显示焦点所在的那个。