Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSOE-589: Shape Tracing should skip the PageTitle shape #173

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.Extensions.Logging;
using OrchardCore.DisplayManagement.Implementation;
using OrchardCore.DisplayManagement.Shapes;
using System.Linq;
Expand All @@ -10,8 +12,13 @@ namespace Lombiq.HelpfulExtensions.Extensions.ShapeTracing;
internal sealed class ShapeTracingShapeEvents : IShapeDisplayEvents
{
private readonly IHttpContextAccessor _hca;
private readonly ILogger<ShapeTracingShapeEvents> _logger;

public ShapeTracingShapeEvents(IHttpContextAccessor hca) => _hca = hca;
public ShapeTracingShapeEvents(IHttpContextAccessor hca, ILogger<ShapeTracingShapeEvents> logger)
{
_hca = hca;
_logger = logger;
}

public Task DisplayedAsync(ShapeDisplayContext context)
{
Expand All @@ -20,39 +27,42 @@ public Task DisplayedAsync(ShapeDisplayContext context)
// We could also use _orchardHelper.ConsoleLog(context.Shape) here but that causes an OutOfMemoryException.

var builder = new HtmlContentBuilder(6);
var builderShapeInfo = new HtmlContentBuilder(6);
var shapeMetadata = context.Shape.Metadata;

var isPageTitle = shapeMetadata.Type == nameof(PageTitleShapes.PageTitle);

builder.AppendLine();
builder.AppendHtmlLine("<!-- ");
builder.AppendHtmlLine(shapeMetadata.Type);
builder.AppendLine();
builderShapeInfo.AppendHtmlLine(shapeMetadata.Type);
builderShapeInfo.AppendLine();

void AddIfNotNullOrEmpty(string name, string value)
{
if (!string.IsNullOrEmpty(value))
{
builder.AppendHtml(name);
builder.AppendHtml(": ");
builder.AppendHtmlLine(value);
builderShapeInfo.AppendHtml(name);
builderShapeInfo.AppendHtml(": ");
builderShapeInfo.AppendHtmlLine(value);
}
}

if (shapeMetadata.Alternates.Count != 0)
{
builder.AppendHtml("Alternates: ");
builder.AppendHtmlLine(string.Join(", ", shapeMetadata.Alternates));
builderShapeInfo.AppendHtml("Alternates: ");
builderShapeInfo.AppendHtmlLine(string.Join(", ", shapeMetadata.Alternates));
}

if (shapeMetadata.BindingSources.Any())
{
builder.AppendHtml("Binding sources: ");
builder.AppendHtmlLine(string.Join(", ", shapeMetadata.BindingSources));
builderShapeInfo.AppendHtml("Binding sources: ");
builderShapeInfo.AppendHtmlLine(string.Join(", ", shapeMetadata.BindingSources));
}

if (shapeMetadata.Wrappers.Count != 0)
{
builder.AppendHtml("Wrappers: ");
builder.AppendHtmlLine(string.Join(", ", shapeMetadata.Wrappers));
builderShapeInfo.AppendHtml("Wrappers: ");
builderShapeInfo.AppendHtmlLine(string.Join(", ", shapeMetadata.Wrappers));
}

AddIfNotNullOrEmpty(nameof(ShapeMetadata.Card), shapeMetadata.Card);
Expand All @@ -66,11 +76,18 @@ void AddIfNotNullOrEmpty(string name, string value)
AddIfNotNullOrEmpty(nameof(ShapeMetadata.Prefix), shapeMetadata.Prefix);
AddIfNotNullOrEmpty(nameof(ShapeMetadata.Tab), shapeMetadata.Tab);

builder.AppendHtmlLine("-->");
_logger.LogInformation("Shape information:\n{ShapeInformation}", builderShapeInfo.Html());

builder.AppendHtml(context.ChildContent);
builder.AppendHtml(builderShapeInfo);

context.ChildContent = builder;
builder.AppendHtmlLine("-->");

// We are skipping the PageTitle shape, otherwise the shape information would be put in the title.
if (!isPageTitle)
{
builder.AppendHtml(context.ChildContent);
context.ChildContent = builder;
}

return Task.CompletedTask;
}
Expand Down