问题描述
在注册新块时,如果使用带有 RichText
标记的 multiline
,我无法设置默认值
attributes: {
header: {
type: 'string',source: 'text',selector: 'h2',default: 'default',},text: {
type: 'array',source: 'children',selector: 'div',default: 'Lorem Ipsum Dolor Sit Amet',// also tested,same result
// default: ['Lorem Ipsum Dolor Sit Amet','test','test2'],// default: () => <p>Lorem Ipsum Dolor Sit Amet</p>,// default: [() => <p>Lorem Ipsum Dolor Sit Amet</p>],edit: ({ attributes: { header,text },setAttributes }) => {
const blockProps = useBlockProps()
return (
<div {...blockProps}>
<RichText tagName="h2" placeholder={'Header'} onChange={header => setAttributes({ header })} value={header} />
<RichText
tagName="div"
multiline="p"
placeholder={'Write here the description'}
onChange={text => setAttributes({ text })}
value={text}
/>
</div>
)
},
如果我删除 multiline="p"
行,我会看到默认文本,但使用它我什么也看不到
错误在哪里?
解决方法
我已经能够通过使用您手动编辑文本时收集的内容来做到这一点
default: [
{ type: 'p',props: { children: ['Lorem Ipsum Dolor Sit Amet'] } },{ type: 'p',props: { children: ['test'] } },],
,
使用属性为带有 multiline="p"
的 RichText 设置默认值的另一种简化方法是:
attributes: {
...
text: {
type: 'string',// changed from array
source: 'children',selector: 'div',default: [<p>Lorem Ipsum Dolor Sit Amet</p>,<p>test</p>,<p>test2</p>]
}