目前我正在建造我的第一个古腾堡街区。一切似乎都很好,除了编辑器字段显示 h2 和 p 元素双精度。有什么我想念的吗?
我已经在整个互联网上搜索了这个问题,但我无法找到解决这个问题的方法。
下面你会看到我写的古腾堡代码。也许我错过了什么?还是有错别字?
/**
* Block dependencies
*/
import icons from './icons';
import './editor.scss';
/**
* Internal block libraries
*/
const { __ } = wp.i18n;
const {
registerBlockType,
} = wp.blocks;
const {
InspectorControls,
RichText,
MediaUpload,
} = wp.editor;
const {
Tooltip,
PanelBody,
PanelRow,
FormToggle,
Button,
} = wp.components;
/**
* Register example block
*/
export default registerBlockType('mfgb/banner', {
title: __( 'Banner Block', 'mfgb' ),
description: __( 'Voeg een banner toe aan de website (met of zonder tekst)', 'mfgb'),
category: 'common',
icon: {
background: 'rgba(254, 243, 224, 0.52)',
src: icons.upload,
},
keywords: [
__( 'Image', 'mfgb' ),
__( 'MediaUpload', 'mfgb' ),
__( 'Message', 'mfgb' ),
],
attributes: {
title: {
type: 'array',
source: 'children',
selector: 'h2',
},
content: {
type: 'array',
source: 'children',
selector: 'p',
},
backgroundImage: {
type: 'string',
default: '', // no image by default!
},
contentControl: {
type: 'boolean',
default: false,
},
},
edit: props => {
const { attributes: { title, content, backgroundImage, contentControl, Component },
className, setAttributes } = props;
const toggleContentControl = () => setAttributes( { contentControl: ! contentControl } );
function onTitleChange(changes) {
setAttributes({
title: changes
});
}
function onContentChange(changes) {
setAttributes({
content: changes
});
}
function onImageSelect(imageObject) {
setAttributes({
backgroundImage: imageObject.sizes.full.url
})
}
return ([
<InspectorControls>
<PanelBody
title={ __( 'Tekst opties', 'mfgb' ) }
>
<PanelRow>
<label
htmlFor="has-text-form-toggle"
>
{ __( 'Bevat deze banner tekst?', 'mfgb' ) }
</label>
<FormToggle
id="has-text-form-toggle"
label={ __( 'Bevat tekst...', 'mfgb' ) }
checked={ contentControl }
onChange={ toggleContentControl }
/>
</PanelRow>
</PanelBody>
<PanelBody
title={ __( 'Selecteer een achtergrond afbeelding', 'mfgb' ) }
>
<PanelRow>
<MediaUpload
onSelect={onImageSelect}
type="image"
value={backgroundImage} // make sure you destructured backgroundImage from props.attributes!
render={ ( { open } ) => (
<Button
className={ "button button-large" }
onClick={ open }
>
{ icons.upload }
{ __( ' Upload Image', 'mfgb' ) }
</Button>
) }
/>
<img src={backgroundImage} />
</PanelRow>
</PanelBody>
</InspectorControls>,
<div
className={className}
style={{
backgroundImage: `url(${backgroundImage})`,
backgroundSize: 'cover',
backgroundPosition: 'center'
}}>
<div className="overlay"></div> {/* Adding an overlay element */}
{ contentControl == true &&
<div>
<RichText
tagName="h2"
className="title" // adding a class we can target
value={title}
onChange={onTitleChange}
placeholder="Voer de titel in"
/>
<RichText
tagName="p"
className="content" // adding a class we can target
value={content}
onChange={onContentChange}
placeholder="Voer de text in..."
/>
</div>
}
</div>
]);
},
save: props => {
const { attributes, className } = props;
const { title, content, contentControl, backgroundImage } = props.attributes;
return (
<div
className={className}
style={{
backgroundImage: `url(${backgroundImage})`,
backgroundSize: 'cover',
backgroundPosition: 'center'
}}>
<div className="overlay"></div>
{/* the class also needs to be added to the h2 for RichText */}
{ contentControl == true && (
<h2 class="title">{title}</h2>
)}
{ contentControl == true && (
<p class="content">{content}</p>
)}
</div>
);
},
},
);