$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>block editor - gutenberg attributes|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

block editor - gutenberg attributes

matteradmin10PV0评论

hi and thanks for stopping by.

i try myself in developing reusable gutenberg blocks and i come pretty far but i do have some lacks of understanding, how..

so i want to create one block with 2 textfields. this works fine i can edit and save them, but when i reload the editor, it throws an error aka block validation failed, expected is different from actual.

(function (blocks, editor, components, i18n, element) {
    const
        {registerBlockType} = blocks,
        {Fragment} = element,
        {RichText} = editor;

    registerBlockType('wu/text-image-block', {
        title: i18n.__('whatever'),
        description: i18n.__('yada yada'),
        icon: 'businessman',
        category: 'common',
        attributes: {
            main_text: {
                type: 'array',
                source: 'children',
                selector: 'p'
            },
            more_text: {
                type: 'array',
                source: 'children',
                selector: 'p'
            },
        },

        edit({attributes, className, setAttributes}) {
            const { main_text, more_text } = attributes;

            return (
                <Fragment>
                    <div>
                        <div className='wu-ti-text'>
                            <RichText
                                key='editable'
                                tagName='p'
                                placeholder={ i18n.__('Write some text...') }
                                keepPlaceholderOnFocus={ true }
                                value={ main_text }
                                onChange={ function ( new_text ) {
                                    setAttributes({
                                        main_text: new_text
                                    })
                                } }
                            />
                            <RichText
                                key='editable'
                                tagName='p'
                                placeholder={ i18n.__('Optional text...') }
                                keepPlaceholderOnFocus={ true }
                                value={ more_text }
                                onChange={ function ( new_more_text ) {
                                    setAttributes({
                                        more_text: new_more_text
                                    })
                                } }
                            />
                        </div>
                    </div>
                </Fragment>
            );
        },

        save({ attributes }) {
            const { main_text, more_text } = attributes;

            return (
                <Fragment>
                    <div>
                        <div className='wu-ti-text'>
                            <RichText.Content
                                tagName="p"
                                value={ main_text }
                            />
                            <RichText.Content
                                tagName="p"
                                value={ more_text }
                            />
                        </div>
                    </div>
                </Fragment>
            );
        }
    })

})(
    window.wp.blocks,
    window.wp.editor,
    window.wpponents,
    window.wp.i18n,
    window.wp.element
);

the outcome is fine, but when i reload, the block expects to find the same content from richtext1 inside richtext2, and of course i don't want that, i want 1 in 1 and 2 in 2, saving works, re-editing doesn't. and it is somewhere in the depth of the attributes, i'm not passing the correct source? selector? what?? what is that anyways, i don't get it..

hi and thanks for stopping by.

i try myself in developing reusable gutenberg blocks and i come pretty far but i do have some lacks of understanding, how..

so i want to create one block with 2 textfields. this works fine i can edit and save them, but when i reload the editor, it throws an error aka block validation failed, expected is different from actual.

(function (blocks, editor, components, i18n, element) {
    const
        {registerBlockType} = blocks,
        {Fragment} = element,
        {RichText} = editor;

    registerBlockType('wu/text-image-block', {
        title: i18n.__('whatever'),
        description: i18n.__('yada yada'),
        icon: 'businessman',
        category: 'common',
        attributes: {
            main_text: {
                type: 'array',
                source: 'children',
                selector: 'p'
            },
            more_text: {
                type: 'array',
                source: 'children',
                selector: 'p'
            },
        },

        edit({attributes, className, setAttributes}) {
            const { main_text, more_text } = attributes;

            return (
                <Fragment>
                    <div>
                        <div className='wu-ti-text'>
                            <RichText
                                key='editable'
                                tagName='p'
                                placeholder={ i18n.__('Write some text...') }
                                keepPlaceholderOnFocus={ true }
                                value={ main_text }
                                onChange={ function ( new_text ) {
                                    setAttributes({
                                        main_text: new_text
                                    })
                                } }
                            />
                            <RichText
                                key='editable'
                                tagName='p'
                                placeholder={ i18n.__('Optional text...') }
                                keepPlaceholderOnFocus={ true }
                                value={ more_text }
                                onChange={ function ( new_more_text ) {
                                    setAttributes({
                                        more_text: new_more_text
                                    })
                                } }
                            />
                        </div>
                    </div>
                </Fragment>
            );
        },

        save({ attributes }) {
            const { main_text, more_text } = attributes;

            return (
                <Fragment>
                    <div>
                        <div className='wu-ti-text'>
                            <RichText.Content
                                tagName="p"
                                value={ main_text }
                            />
                            <RichText.Content
                                tagName="p"
                                value={ more_text }
                            />
                        </div>
                    </div>
                </Fragment>
            );
        }
    })

})(
    window.wp.blocks,
    window.wp.editor,
    window.wpponents,
    window.wp.i18n,
    window.wp.element
);

the outcome is fine, but when i reload, the block expects to find the same content from richtext1 inside richtext2, and of course i don't want that, i want 1 in 1 and 2 in 2, saving works, re-editing doesn't. and it is somewhere in the depth of the attributes, i'm not passing the correct source? selector? what?? what is that anyways, i don't get it..

Share Improve this question asked Feb 15, 2019 at 17:24 honk31honk31 1,4081 gold badge13 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

From the docs:

If a selector is specified, the source behavior will be run against the corresponding element(s) contained within the block.

You are telling your block to look at the same Element for the value of both your attributes.(The first p tag)

Change the second selector to something like

p:last-of-type

or give your p tags an id.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far