古腾堡MediaUpload在编辑器中消失

时间:2018-12-17 19:33:06

标签: wordpress wordpress-gutenberg gutenberg-blocks

我正在尝试在Gutenberg中创建一个自定义块,其中涉及上传图像。我遇到的问题是render中的MediaUpload无法正常工作。我想我缺少了一些东西,但是找不到。

每当我尝试将MediaUpload元素放在一个块中时,它就会为空。在下面的代码中,如果您对其进行检查,则会看到<div class="image-test">,但其中没有任何内容。

我尝试简化以下代码,以确保没有任何干扰,但对我而言仍然无效。

这是block.js代码:

const { registerBlockType } = wp.blocks;
const { MediaUpload, MediaUploadCheck } = wp.editor;
const { Button } = wp.components;

registerBlockType( 'custom/image-test', {
    title: 'Image Test',
    icon: 'warning',
    category: 'custom-blocks',

    edit( { attributes, className, setAttributes } ) {
        return (
            <div className="image-test">
                <MediaUploadCheck>
                    <MediaUpload
                        onSelect={ media => console.log( media.length ) }
                        render={ ({ open }) => (
                            <Button onClick={ open }>
                                Open Media Library
                            </Button>
                        )}
                    />
                </MediaUploadCheck>
            </div>
        );
    },

    save( { attributes } ) {
        return (
            <div class="image-test">
                <p>Image Test</p>
            </div>
        );
    },
} );

这是我在函数中声明块的地方:

function image_test_block(){
    wp_register_script(
        'image-test-script', // name of script
        get_template_directory_uri() . '/js/block-image-test.js', // path to script
        array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-components' ) // dependencies
    );

    register_block_type('custom/image-test', array(
        'editor_script' => 'image-test-script'
    ) );
}
add_action( 'init', 'image_test_block', 10, 0 );

1 个答案:

答案 0 :(得分:0)

首先,据我所知,MediaUploadCheck并不是官方的WP组件。这是proposal ticket

使用create guten block之类的块开发工具来节省配置麻烦。我不确定您的脚本是否入队,很可能是您通过错误的钩子添加了资产。

这是一个工作菜单卡,其中包含mediaUpload组件。

const { __, setLocaleData } = wp.i18n;
const {
    registerBlockType,
} = wp.blocks;
const {
    RichText,
    MediaUpload,
} = wp.editor;
const { Button } = wp.components;

setLocaleData( window.gutenberg_examples_05_esnext.localeData, 'gutenberg-examples' );

registerBlockType( 'gutenberg-examples/example-05-recipe-card-esnext', {
    title: __( 'Example: Recipe Card (esnext)', 'gutenberg-examples' ),
    icon: 'index-card',
    category: 'layout',
    attributes: {
        title: {
            type: 'array',
            source: 'children',
            selector: 'h2',
        },
        mediaID: {
            type: 'number',
        },
        mediaURL: {
            type: 'string',
            source: 'attribute',
            selector: 'img',
            attribute: 'src',
        },
        ingredients: {
            type: 'array',
            source: 'children',
            selector: '.ingredients',
        },
        instructions: {
            type: 'array',
            source: 'children',
            selector: '.steps',
        },
    },
    edit: ( props ) => {
        const {
            className,
            attributes: {
                title,
                mediaID,
                mediaURL,
                ingredients,
                instructions,
            },
            setAttributes,
        } = props;
        const onChangeTitle = ( value ) => {
            setAttributes( { title: value } );
        };

        const onSelectImage = ( media ) => {
            setAttributes( {
                mediaURL: media.url,
                mediaID: media.id,
            } );
        };
        const onChangeIngredients = ( value ) => {
            setAttributes( { ingredients: value } );
        };

        const onChangeInstructions = ( value ) => {
            setAttributes( { instructions: value } );
        };

        return (
            <div className={ className }>
                <RichText
                    tagName="h2"
                    placeholder={ __( 'Write Recipe title…', 'gutenberg-examples' ) }
                    value={ title }
                    onChange={ onChangeTitle }
                />
                <div className="recipe-image">
                    <MediaUpload
                        onSelect={ onSelectImage }
                        allowedTypes="image"
                        value={ mediaID }
                        render={ ( { open } ) => (
                            <Button className={ mediaID ? 'image-button' : 'button button-large' } onClick={ open }>
                                { ! mediaID ? __( 'Upload Image', 'gutenberg-examples' ) : <img src={ mediaURL } alt={ __( 'Upload Recipe Image', 'gutenberg-examples' ) } /> }
                            </Button>
                        ) }
                    />
                </div>
                <h3>{ __( 'Ingredients', 'gutenberg-examples' ) }</h3>
                <RichText
                    tagName="ul"
                    multiline="li"
                    placeholder={ __( 'Write a list of ingredients…', 'gutenberg-examples' ) }
                    value={ ingredients }
                    onChange={ onChangeIngredients }
                    className="ingredients"
                />
                <h3>{ __( 'Instructions', 'gutenberg-examples' ) }</h3>
                <RichText
                    tagName="div"
                    multiline="p"
                    className="steps"
                    placeholder={ __( 'Write the instructions…', 'gutenberg-examples' ) }
                    value={ instructions }
                    onChange={ onChangeInstructions }
                />
            </div>
        );
    },
    save: ( props ) => {
        const {
            className,
            attributes: {
                title,
                mediaURL,
                ingredients,
                instructions,
            },
        } = props;
        return (
            <div className={ className }>
                <RichText.Content tagName="h2" value={ title } />

                {
                    mediaURL && (
                        <img className="recipe-image" src={ mediaURL } alt={ __( 'Recipe Image', 'gutenberg-examples' ) } />
                    )
                }

                <RichText.Content tagName="h2" className="ingredients" value={ ingredients } />

                <RichText.Content tagName="div" className="steps" value={ instructions } />
            </div>
        );
    },
} );

您的mediaUload组件缺少​​媒体ID,缺少必需的 onSelect 功能,并且您还在Search Console中输出值。