嗨所以我正在尝试创建一个富文本块,其中第一行将是一个h1,当你按下输入你得到键入一个阶段,我尝试使用值为“p”的多行属性,但这不是工作,
我想知道是否有人可以帮助我。
到目前为止这是我的代码。
/**
* Block dependencies
*/
import './style.scss';
/**
* Internal block libraries
*/
const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const { RichText } = wp.editor;
/**
* Register block
*/
export default registerBlockType('my-plugin/header-2', {
title: __('h1 Title'),
description: __('h1 title'),
icon: 'heart',
category: 'common',
keywords: [
__('richtext-block'),
__('weconnect'),
__('h2')
],
attributes: {
content: {
type: 'array',
source: 'children',
selector: 'h2',
},
},
edit: function ({ attributes, setAttributes, className, isSelected }) {
return (
<RichText
tagName="h2"
className={className}
value={attributes.content}
onChange={(content) => setAttributes({ content })}
placeholder={__('Enter text...', 'custom-block')}
keepPlaceholderOnFocus={true}
/>
);
},
save: function( { attributes } ) {
return (
<RichText.Content tagName="h2" value={ attributes.content } />
);
}
});
您的块目前仅适用于H2标签。在代码中没有任何地方你有“P”标签的代码,因此它不起作用。试试这个代码 -
export default registerBlockType('my-plugin/header-2', {
title: __('h1 Title'),
description: __('h1 title'),
icon: 'heart',
category: 'common',
keywords: [
__('richtext-block'),
__('weconnect'),
__('h2')
],
attributes: {
content: {
type: 'array',
source: 'children',
selector: 'h2',
},
pcontent: {
type: 'array',
source: 'children',
selector: 'p',
},
},
edit: function ({ attributes, setAttributes, className, isSelected }) {
return (
<div className={className}>
<RichText
tagName="h2"
className={className}
value={attributes.content}
onChange={(content) => setAttributes({ content })}
placeholder={__('Enter text...', 'custom-block')}
keepPlaceholderOnFocus={true}
/>
<RichText
tagName="p"
className={className}
value={attributes.pcontent}
onChange={(pcontent) => setAttributes({ pcontent })}
placeholder={__('Enter p text...', 'custom-block')}
keepPlaceholderOnFocus={true}
/>
</div>
);
},
save: function( { attributes } ) {
return (
<div>
<RichText.Content tagName="h2" value={ attributes.content } />
<RichText.Content tagName="p" value={ attributes.pcontent } />
</div>
);
}
});
我做的改变 -