Skip to content

Commit

Permalink
Fix ScriptModuleResourceFilter finally.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahelsaig committed Jul 27, 2024
1 parent b341fc1 commit 039f062
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.Html;
using OrchardCore.DisplayManagement.Shapes;
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Data;
Expand All @@ -13,13 +14,12 @@

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;
private readonly Func<IHtmlContent?> _getHtml;

public string? Position { get; set; }

Expand All @@ -37,18 +37,23 @@ public class HtmlShape : IHtmlContent, IPositioned, IShape

public IReadOnlyList<IPositioned> Items => [];

public HtmlShape(IHtmlContent? value, string? position = null)
public HtmlShape(Func<IHtmlContent?> getHtml, string? position = null)
{
_value = value ?? new HtmlString(string.Empty);
_getHtml = getHtml;
Position = position;
}

public HtmlShape(IHtmlContent? value, string? position = null)
: this(() => value, position)
{
}

public HtmlShape(string? value, string? position = null)
: this(new HtmlContentString(value ?? string.Empty), position)
{
}

public void WriteTo(TextWriter writer, HtmlEncoder encoder) => _value.WriteTo(writer, encoder);
public void WriteTo(TextWriter writer, HtmlEncoder encoder) => _getHtml.Invoke()?.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 @@ -24,10 +24,10 @@ public record ScriptModuleResourceFilter(ILayoutAccessor LayoutAccessor) : IAsyn
{
public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
if (DisplayScriptModuleResources(context.HttpContext.RequestServices) is { } html)
{
await LayoutAccessor.AddShapeToZoneAsync(CommonLocationNames.Content, new HtmlShape(html), "After");
}
await LayoutAccessor.AddShapeToZoneAsync(
CommonLocationNames.Content,
new HtmlShape(() => DisplayScriptModuleResources(context.HttpContext.RequestServices)),
"After");

await next();
}
Expand Down

0 comments on commit 039f062

Please sign in to comment.