WordPress Gutenberg RichText,如何在使用 `multiline="p"` 时使用 source: 'children' 设置默认值?
WordPress Gutenberg RichText, how can I set a default value with source: 'children' when using `multiline="p"`?
注册新块时,如果使用带有 multiline
标签的 RichText
,我无法设置默认值
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>]
}
注册新块时,如果使用带有 multiline
标签的 RichText
,我无法设置默认值
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>]
}