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 2 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,17 +1,24 @@
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using OrchardCore.DisplayManagement.Implementation;
using OrchardCore.DisplayManagement.Shapes;
using System.Linq;
using System.Text.Encodings.Web;
using System.Threading.Tasks;

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,48 +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 == "PageTitle";

// This is needed to have the shape info as a comment, otherwise it would be put in the title.
if (isPageTitle)
{
builder.AppendHtml(context.ChildContent);
builder.AppendHtmlLine("</title>");
}
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 @@ -75,15 +76,29 @@ void AddIfNotNullOrEmpty(string name, string value)
AddIfNotNullOrEmpty(nameof(ShapeMetadata.Prefix), shapeMetadata.Prefix);
AddIfNotNullOrEmpty(nameof(ShapeMetadata.Tab), shapeMetadata.Tab);

builder.AppendHtml(builderShapeInfo);

builder.AppendHtmlLine("-->");

if (!isPageTitle)
// This is needed to have the shape info as a comment, otherwise it would be put in the title.
if (isPageTitle)
{
var log = string.Empty;

using (var writer = new System.IO.StringWriter())
{
builderShapeInfo.WriteTo(writer, HtmlEncoder.Default);
log = writer.ToString();
}
sarahelsaig marked this conversation as resolved.
Show resolved Hide resolved

_logger.LogInformation("PageTitle Shape information:\n{ShapeInformation}", log);
sarahelsaig marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
builder.AppendHtml(context.ChildContent);
context.ChildContent = builder;
}

context.ChildContent = builder;

return Task.CompletedTask;
}

Expand Down