微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WordPress Gutenberg RichText,如何在使用 `multiline="p"` 时使用 source: 'children' 设置默认值?

如何解决WordPress Gutenberg RichText,如何在使用 `multiline="p"` 时使用 source: 'children' 设置默认值?

注册新块时,如果使用带有 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>]
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。