From 7de578430ac2fa149deead2f2a5496f74212ff38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Sat, 27 Jul 2024 18:56:09 +0200 Subject: [PATCH] Obsolete CreateAdHocShape. --- .../ResourceManagement/HtmlShape.cs | 52 +++++++++++++++++++ .../ScriptModuleResourceFilter.cs | 6 +-- .../Shapes/ShapeExtensions.cs | 3 ++ 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 Lombiq.HelpfulLibraries.OrchardCore/ResourceManagement/HtmlShape.cs diff --git a/Lombiq.HelpfulLibraries.OrchardCore/ResourceManagement/HtmlShape.cs b/Lombiq.HelpfulLibraries.OrchardCore/ResourceManagement/HtmlShape.cs new file mode 100644 index 00000000..2ef425bd --- /dev/null +++ b/Lombiq.HelpfulLibraries.OrchardCore/ResourceManagement/HtmlShape.cs @@ -0,0 +1,52 @@ +using Microsoft.AspNetCore.Html; +using OrchardCore.DisplayManagement; +using OrchardCore.DisplayManagement.Html; +using OrchardCore.DisplayManagement.Shapes; +using System.Collections.Frozen; +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.Text.Encodings.Web; +using System.Threading.Tasks; + +namespace Lombiq.HelpfulLibraries.OrchardCore.ResourceManagement; + +// Based on OrchardCore.DisplayManagement.PositionWrapper. +public class HtmlShape : IHtmlContent, IPositioned, IShape +{ + private static readonly IDictionary _dummyAttributes = new Dictionary().ToFrozenDictionary(); + private static readonly IDictionary _dummyProperties = new Dictionary().ToFrozenDictionary(); + + private readonly IHtmlContent _value; + + public string Position { get; set; } + + public ShapeMetadata Metadata { get; set; } = new(); + + public string Id { get; set; } + + public string TagName { get; set; } + + public IList Classes => []; + + public IDictionary Attributes => _dummyAttributes; + + public IDictionary Properties => _dummyProperties; + + public IReadOnlyList Items => []; + + public HtmlShape(IHtmlContent value, string position) + { + _value = value; + Position = position; + } + + public HtmlShape(string value, string position) + : this(new HtmlContentString(value), position) + { + } + + public void WriteTo(TextWriter writer, HtmlEncoder encoder) => _value.WriteTo(writer, encoder); + + public ValueTask AddAsync(object item, string position) => throw new ReadOnlyException(); +} diff --git a/Lombiq.HelpfulLibraries.OrchardCore/ResourceManagement/ScriptModuleResourceFilter.cs b/Lombiq.HelpfulLibraries.OrchardCore/ResourceManagement/ScriptModuleResourceFilter.cs index 1c341e13..66f45c2b 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/ResourceManagement/ScriptModuleResourceFilter.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/ResourceManagement/ScriptModuleResourceFilter.cs @@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; -using OrchardCore.DisplayManagement.Implementation; using OrchardCore.DisplayManagement.Layout; using OrchardCore.ResourceManagement; using System; @@ -25,9 +24,8 @@ public record ScriptModuleResourceFilter(ILayoutAccessor LayoutAccessor) : IAsyn { public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { - var shape = await context.HttpContext.RequestServices.CreateAdHocShapeForCurrentThemeAsync( - nameof(ScriptModuleResourceFilter), - displayContext => Task.FromResult(DisplayScriptModuleResources(displayContext.ServiceProvider))); + var html = DisplayScriptModuleResources(context.HttpContext.RequestServices); + var shape = new HtmlShape(html, "before"); await LayoutAccessor.AddShapeToZoneAsync("Content", shape, "After"); await next(); diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Shapes/ShapeExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/Shapes/ShapeExtensions.cs index 147c0c5c..29cd9303 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Shapes/ShapeExtensions.cs +++ b/Lombiq.HelpfulLibraries.OrchardCore/Shapes/ShapeExtensions.cs @@ -1,3 +1,4 @@ +using Lombiq.HelpfulLibraries.OrchardCore.ResourceManagement; using Microsoft.AspNetCore.Html; using Microsoft.Extensions.DependencyInjection; using OrchardCore.DisplayManagement.Descriptors; @@ -60,6 +61,7 @@ public static T As(this IShape shape) /// name="shapeTable"/>, then a new descriptor is added with binding that uses . /// If is null or empty, a new random unique name is generated. /// + [Obsolete($"This no longer works with the {nameof(DefaultShapeTableManager)}. Use {nameof(HtmlShape)} instead.")] public static IShape CreateAdHocShape(this ShapeTable shapeTable, string type, Func> displayAsync) { if (string.IsNullOrEmpty(type)) type = $"AdHocShape_{Guid.NewGuid():D}"; @@ -102,6 +104,7 @@ public static IShape CreateAdHocShape(this ShapeTable shapeTable, string type, F /// uses . If is null or empty, a new random unique name is /// generated. /// + [Obsolete($"This no longer works with the {nameof(DefaultShapeTableManager)}. Use {nameof(HtmlShape)} instead.")] public static async Task CreateAdHocShapeForCurrentThemeAsync( this IServiceProvider provider, string type,