Jeavon / Slimsy

Effortless Responsive & Lazy Images with LazySizes and Umbraco
MIT License
72 stars 23 forks source link

Support for Rich Text images in Block List/Grid #56

Closed robertjf closed 1 year ago

robertjf commented 1 year ago

Hi,

I made the following extensions to enable easy support for Block List and Grid-embedded Rich Text editor images:

public static class SlimsyExtensions
{
    /// <summary>
    /// Convert img to img srcset, extracts width and height from querystrings
    /// </summary>
    /// <param name="htmlHelper"></param>
    /// <param name="publishedElement"></param>
    /// <param name="propertyAlias">Alias of the TinyMce property</param>
    /// <param name="generateLqip">Set to true if you want LQIP markup to be generated</param>
    /// <param name="removeStyleAttribute">If you don't want the inline style attribute added by TinyMce to render</param>
    /// <param name="renderPicture"></param>
    /// <param name="pictureSources"></param>
    /// <returns>HTML Markup</returns>
    public static IHtmlEncodedString? ConvertImgToResponsive(this IHtmlHelper htmlHelper, IPublishedElement publishedElement, string propertyAlias, bool generateLqip = true, bool removeStyleAttribute = true, bool renderPicture = false, string[]? pictureSources = null)
    {
        var sourceValue = publishedElement.GetProperty(propertyAlias)?.GetSourceValue()?.ToString();
        if (sourceValue is null) {
            return null;
        }

        return htmlHelper.ConvertImgToResponsive(sourceValue, generateLqip, removeStyleAttribute, renderPicture, pictureSources);
    }

    /// <summary>
    /// Convert img to img srcset, extracts width and height from querystrings
    /// </summary>
    /// <param name="htmlHelper"></param>
    /// <param name="publishedElement"></param>
    /// <param name="propertyAlias">Alias of the TinyMce property</param>
    /// <param name="generateLqip">Set to true if you want LQIP markup to be generated</param>
    /// <param name="removeStyleAttribute">If you don't want the inline style attribute added by TinyMce to render</param>
    /// <param name="renderPicture"></param>
    /// <param name="pictureSources"></param>
    /// <returns>HTML Markup</returns>
    public static IHtmlEncodedString? ConvertImgToResponsive(this IHtmlHelper htmlHelper, PublishedElementWrapped publishedElement, string propertyAlias, bool generateLqip = true, bool removeStyleAttribute = true, bool renderPicture = false, string[]? pictureSources = null)
    {
        return htmlHelper.ConvertImgToResponsive(publishedElement.Unwrap(), propertyAlias, generateLqip, removeStyleAttribute, renderPicture, pictureSources);
    }
}

Then in a Block List view we can just use this:

@Html.ConvertImgToResponsive(Model.Content, "text", renderPicture: true, pictureSources: new []{"webp"})
Jeavon commented 1 year ago

This looks great @robertjf

Jeavon commented 1 year ago

Do you want to send a PR?

robertjf commented 1 year ago

@Jeavon will do when I get a spare 5 minutes - maybe even now! :)

robertjf commented 1 year ago

@Jeavon PR #57 ready for your perusal :)

khraibani commented 9 months ago

@Jeavon Can this PR be merged in V3 please? As I couldn't get Rich Text editor images works within Block List. I have tried @SlimsyExtensions.ConvertImgToResponsive(Html, Model.value.ToString(), true) Doesnt seem to work, on Umbraco v8.18.8