首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义古登堡块自由格式内容随机消失

自定义古登堡块自由格式内容随机消失
EN

Stack Overflow用户
提问于 2022-06-22 23:30:59
回答 1查看 98关注 0票数 1

我试图解决一个问题,在一个自定义的古腾堡街区中,我的内容似乎是随机消失的(我还没有找到触发点)。

见下面的代码:

代码语言:javascript
复制
export default registerBlockType(
    'biscuit/content-filter',
    {
        title: __( 'Title', 'biscuit' ),
        description: __( 'Story content block.', 'biscuit'),
        category: 'common',
        icon: icons.classic,
        keywords: [
            __( 'Content', 'biscuit' ),
            __( 'Story', 'biscuit' ),
        ],
        transforms: {
            from: [
                {
                    type: 'block',
                    blocks: [ 'core/paragraph' ],
                    transform: function ( attributes ) {
                        const innerBlocks = wp.blocks.rawHandler({
                            innerContent: attributes.content
                        });
                        return wp.blocks.createBlock( 'biscuit/content-filter', {
                        }, innerBlocks );
                    },
                },
            ]
        },
        edit: props => {
            const { attributes: { title }, className, setAttributes } = props;
            return (
                <div className={ className }>
                    <div>
                        <RichText
                            tagName="h3"
                            value={ title }
                            onChange={ title => setAttributes({ title })}
                            placeholder={__('Enter title', 'biscuit')}
                            // keepPlaceholderOnFocus={ true }
                        />
                        <label class="small">{ __( 'Title will only appear on the page.', 'biscuit' ) }</label>
                    </div>Content', 'biscuit' ) }</label></h4>
                    <InnerBlocks template={TEMPLATE} templateLock="all" />
                </div>
            );
        },
        save: props => {
            return <InnerBlocks.Content />;
        },
    },
);

const setExtraPropsToBlockType = (props, blockType, attributes) => {
    if( props.children && blockType.name === 'core/freeform' ) {
        props.children = props.children.replace(/<p><!-- \/wp:paragraph --> <!-- wp:paragraph --><\/p>/g,"");
        props.children = props.children.replace(/<p><!-- wp:paragraph --><\/p>/g,"");
        props.children = props.children.replace(/<p><!-- \/wp:paragraph --><\/p>/g,"");

        props.children = props.children.replace(/<!-- wp:paragraph -->/g,"");
        props.children = props.children.replace(/<!-- \/wp:paragraph -->/g,"");

        props.children = props.children.replace(/&lt;\/p&gt;/g,"");
        props.children = props.children.replace(/&lt;p&gt;/g,"");

        props.children = props.children.replace(/&lt;!-- wp:paragraph --&gt;/g,"");
        props.children = props.children.replace(/&lt;!-- \/wp:paragraph --&gt;/g,"");
        props.children = props.children.replace(/&lt;!– wp:paragraph –&gt;/g,"");
        props.children = props.children.replace(/&lt;!– \/wp:paragraph –&gt;/g,"");

        props.children = props.children.replace(/<!-- wp:quote -->/g,"");
        props.children = props.children.replace(/<!-- \/wp:quote -->/g,"");
        props.children = props.children.replace(/&lt;!-- wp:quote --&gt;/g,"");
        props.children = props.children.replace(/&lt;!-- \/wp:quote --&gt;/g,"");
        props.children = props.children.replace(/&lt;!– wp:quote –&gt;/g,"");
        props.children = props.children.replace(/&lt;!– \/wp:quote –&gt;/g,"");

        props.children = props.children.replace(/<!-- wp:/g,"<!--");
        props.children = props.children.replace(/<!-- \/wp:/g,"<!--");

    }
    return props;
};

wp.hooks.addFilter(
    'blocks.getSaveContent.extraProps',
    'biscuit/content-filter',
    setExtraPropsToBlockType
);

有时,自由形式会决定在随机的帖子上空出来。似乎与内容无关。我可以跳到后的修订,并返回并找到最初发布的内容,所以至少我有。

我曾在一个分期场地进行测试,方法是:

  • 创建一个新的post
  • ,添加自定义块,并从消失的自由格式块
  • 中输入相同的文本,添加其他块--引号、段落、图像等,以查看在块之外保存新内容是否触发了
  • 从受影响的post

F 210复制任何其他元素。

这些都还没有引发这场危机。从一个月到一年后,这个街区可以消失在任何地方。我怀疑这与编辑/保存帖子有关,但是我已经测试过了,而且它没有触发。

如能提供任何帮助,将不胜感激:)

EN

回答 1

Stack Overflow用户

发布于 2022-06-23 09:36:42

<h4>中的第40行中缺少开头的<label>edit()标记

代码语言:javascript
复制
</div>Content', 'biscuit' ) }</label></h4>

代之以:

代码语言:javascript
复制
</div><h4><label>{__('Content', 'biscuit')}</label></h4>

我会假设在某个时候,这个块是有效的,第40行的第一部分被意外地删除了。此场景可能会产生您所看到的副作用:包含块的大多数帖子都是有效的,但是当块变得无效时编辑的一些帖子会出现问题。

完成更改后,确保在测试之前清除浏览器缓存。检查浏览器控制台是否有任何错误,如果有错误,请将它们添加到问题中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72723013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档