admin管理员组

文章数量:1287833

I'm building a few custom blocks and have come across something I am not sure is either possible at the current time (as there does not seem to be docs on it), or it is something people are still figuring out.

I'm wondering is it possible to access the image srcsets in a custom blocks. This is my current markup which is accessing the sizes array to get my chosen image size. Which is ok, but obviously not ideal when you get down to a mobile size.


attributes: {
            mediaID: {
                type: "number",
            },
            mediaURL: {
                type: "string",
                source: "attribute",
                selector: "img.promogroupitem__media--img",
                attribute: "src",
            },
            mediaAlt: {
                type: 'string',
                source: 'attribute',
                selector: 'img.promogroupitem__media--img',
                attribute: 'alt'
            },
            mediaWidth: {
                type: 'number',
                source: 'attribute',
                selector: 'img.promogroupitem__media--img',
                attribute: 'width',
            },
            mediaHeight: {
                type: 'number',
                source: 'attribute',
                selector: 'img.promogroupitem__media--img',
                attribute: 'height',
            },  
        },
        parent: 'wpboiler-core/promo-group',

        edit: function(props) {
            const { attributes, setAttributes } = props;
            const { 
                mediaID,
                mediaURL,
                mediaAlt,
                mediaWidth,
                mediaHeight, 
            } = attributes;

            const onSelectImage = media => setAttributes({ 
                mediaID: media.id, 
                mediaURL: (media.sizes.promo ? media.sizes.promo.url : media.url),
                mediaAlt: media.alt,
                mediaWidth: (media.sizes.promo ? media.sizes.promo.width : media.width),
                mediaHeight: (media.sizes.promo ? media.sizes.promo.height : media.height),
            });

            return el(
                'article',
                useBlockProps(attributes),

                el(
                    InspectorControls, 
                    null,

                    // IMAGE UPLOAD BEGIN
                    el(
                        PanelBody,
                        {
                          title: "Background Image",
                        },
            
                        el(MediaUpload, {
                          onSelect: onSelectImage,
                          allowedTypes: "image",
                          value: mediaID,
                          render: (obj) => {
                            return el(
                              Button,
                              {
                                className: "components-button is-primary",
                                onClick: obj.open,
                              },
                              !mediaID
                                ? __("Upload Image", "card-group")
                                : __("Replace Image", "card-group")
                            );
                          },
                        }),
            
                        mediaID
                          ? el(
                              Button,
                              {
                                className: "components-button is-tertiary",
                                style: { marginLeft: "5px" },
                                onClick: () => setAttributes({ 
                                    mediaID: "", 
                                    mediaURL: "",
                                    mediaAlt: "",
                                    mediaHeight: "",
                                    mediaWidth: "",
                                }),
                              },
                              "Remove Image"
                            )
                          : ""
                      )
                      // IMAGE UPLOAD END

                ),
                
                el(
                    'div',
                    { className: 'promogroupitem__container' },

                    mediaURL ? 
                    el(
                        'div',
                        { className: 'promogroupitem__media' },

                        el(
                            'img',
                            { 
                                className: 'promogroupitem__media--img',
                                src: mediaURL,
                                loading: 'lazy',
                                width: mediaWidth,
                                height: mediaHeight,
                                alt: mediaAlt,
                            }
                        ),
                    ) : '',
                )
            );
        },

save: function(props) {
            const { attributes } = props;
            const { 
                mediaURL, 
                mediaAlt,
                mediaHeight,
                mediaWidth, 
            } = attributes;

            return el(
                'article',
                { className: 'promogroupitem' },

                el(
                    'div',
                    { className: 'promogroupitem__container' },

                    mediaURL ?
                    el(
                        'div',
                        { className: 'promogroupitem__media' },

                        el(
                            'img', { 
                                className: 'promogroupitem__media--img',
                                src: mediaURL,
                                loading: 'lazy',
                                width: mediaWidth,
                                height: mediaHeight,
                                alt: mediaAlt,
                            }
                        ),
                    ) : '',

                ),
            );
        },

Its something I am not quite sure how to resolve. Has anyone come across this and developed/found a solution for it?

本文标签: imagesUsing srcsets in a custom block