Skip to content

Commit

Permalink
Obsolete CreateAdHocShape.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahelsaig committed Jul 27, 2024
1 parent 8ab13ea commit 7de5784
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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<string, string> _dummyAttributes = new Dictionary<string, string>().ToFrozenDictionary();
private static readonly IDictionary<string, object> _dummyProperties = new Dictionary<string, object>().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<string> Classes => [];

public IDictionary<string, string> Attributes => _dummyAttributes;

public IDictionary<string, object> Properties => _dummyProperties;

public IReadOnlyList<IPositioned> 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<IShape> AddAsync(object item, string position) => throw new ReadOnlyException();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions Lombiq.HelpfulLibraries.OrchardCore/Shapes/ShapeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Lombiq.HelpfulLibraries.OrchardCore.ResourceManagement;
using Microsoft.AspNetCore.Html;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.DisplayManagement.Descriptors;
Expand Down Expand Up @@ -60,6 +61,7 @@ public static T As<T>(this IShape shape)
/// name="shapeTable"/>, then a new descriptor is added with binding that uses <paramref name="displayAsync"/>.
/// If <paramref name="type"/> is null or empty, a new random unique name is generated.
/// </summary>
[Obsolete($"This no longer works with the {nameof(DefaultShapeTableManager)}. Use {nameof(HtmlShape)} instead.")]
public static IShape CreateAdHocShape(this ShapeTable shapeTable, string type, Func<DisplayContext, Task<IHtmlContent>> displayAsync)
{
if (string.IsNullOrEmpty(type)) type = $"AdHocShape_{Guid.NewGuid():D}";
Expand Down Expand Up @@ -102,6 +104,7 @@ public static IShape CreateAdHocShape(this ShapeTable shapeTable, string type, F
/// uses <paramref name="displayAsync"/>. If <paramref name="type"/> is null or empty, a new random unique name is
/// generated.
/// </summary>
[Obsolete($"This no longer works with the {nameof(DefaultShapeTableManager)}. Use {nameof(HtmlShape)} instead.")]
public static async Task<IShape> CreateAdHocShapeForCurrentThemeAsync(
this IServiceProvider provider,
string type,
Expand Down

0 comments on commit 7de5784

Please sign in to comment.