Skip to content

Commit

Permalink
Support Async in IShapeTableProvider (OrchardCMS#14864)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored and urbanit committed Mar 18, 2024
1 parent 00befda commit 9ffa9a5
Show file tree
Hide file tree
Showing 34 changed files with 349 additions and 300 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using OrchardCore.DisplayManagement;
using System.Threading.Tasks;
using OrchardCore.DisplayManagement.Descriptors;
using OrchardCore.Modules;

namespace OrchardCore.ContentFields.Media
{
[RequireFeatures("OrchardCore.Media")]
public class MediaShapes : IShapeTableProvider
public class MediaShapes : ShapeTableProvider
{
public void Discover(ShapeTableBuilder builder)
public override ValueTask DiscoverAsync(ShapeTableBuilder builder)
{
builder.Describe("HtmlField_Edit")
.OnDisplaying(displaying =>
{
IShape editor = displaying.Shape;
var editor = displaying.Shape;
if (editor.Metadata.Type == "HtmlField_Edit__Wysiwyg")
{
Expand All @@ -24,6 +24,8 @@ public void Discover(ShapeTableBuilder builder)
editor.Metadata.Wrappers.Add("Media_Wrapper__HtmlField");
}
});

return ValueTask.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
@model OrchardCore.ContentFields.Settings.YoutubeFieldSettings
@using OrchardCore.ContentFields.Settings
@model YoutubeFieldSettings

@inject OrchardCore.DisplayManagement.Theming.IThemeManager ThemeManager
@inject OrchardCore.DisplayManagement.Descriptors.IShapeTableManager ShapeTableManager
@{

}
<div class="mb-3">
<div class="form-check">
<input asp-for="Required" type="checkbox" class="form-check-input">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@{
var theme = await ThemeManager.GetThemeAsync();
var shapeTable = ShapeTableManager.GetShapeTable(theme?.Id);
var shapeTable = await ShapeTableManager.GetShapeTableAsync(theme?.Id);
var editorShapes = shapeTable.Bindings.Keys.Where(x => x.StartsWith(Model.PartFieldDefinition.FieldDefinition.Name + "_Option__", StringComparison.OrdinalIgnoreCase) || x.Equals(Model.PartFieldDefinition.FieldDefinition.Name + "_Option", StringComparison.OrdinalIgnoreCase));
var displayShapes = shapeTable.Bindings.Keys.Where(x => x.StartsWith(Model.PartFieldDefinition.FieldDefinition.Name + "_DisplayOption__", StringComparison.OrdinalIgnoreCase) || x.Equals(Model.PartFieldDefinition.FieldDefinition.Name + "_DisplayOption", StringComparison.OrdinalIgnoreCase));
var returnUrl = ViewData["returnUrl"]?.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@{
var typePart = Model.TypePartDefinition;
var theme = await ThemeManager.GetThemeAsync();
var shapeTable = ShapeTableManager.GetShapeTable(theme?.Id);
var shapeTable = await ShapeTableManager.GetShapeTableAsync(theme?.Id);
var displayShapes = shapeTable.Bindings.Keys.Where(x => x.StartsWith(Model.TypePartDefinition.PartDefinition.Name + "_DisplayOption__", StringComparison.OrdinalIgnoreCase) || x.Equals(Model.TypePartDefinition.PartDefinition.Name + "_DisplayOption", StringComparison.OrdinalIgnoreCase));
var editorShapes = shapeTable.Bindings.Keys.Where(x => x.StartsWith(Model.TypePartDefinition.PartDefinition.Name + "_Option__", StringComparison.OrdinalIgnoreCase) || x.Equals(Model.TypePartDefinition.PartDefinition.Name + "_Option", StringComparison.OrdinalIgnoreCase));
}
Expand Down
10 changes: 6 additions & 4 deletions src/OrchardCore.Modules/OrchardCore.Contents/Shapes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Display;
Expand All @@ -9,9 +9,9 @@

namespace OrchardCore.Contents
{
public class Shapes : IShapeTableProvider
public class Shapes : ShapeTableProvider
{
public void Discover(ShapeTableBuilder builder)
public override ValueTask DiscoverAsync(ShapeTableBuilder builder)
{
builder.Describe("Content")
.OnDisplaying(displaying =>
Expand Down Expand Up @@ -91,8 +91,10 @@ public void Discover(ShapeTableBuilder builder)
displayShape.Metadata.Alternates.Add(alternate);
}
await context.Shape.AddAsync(displayShape, "");
await context.Shape.AddAsync(displayShape, string.Empty);
});

return ValueTask.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Html;

namespace OrchardCore.DisplayManagement.Descriptors
{
public class DemoShapeProvider : IShapeTableProvider, IShapeAttributeProvider
public class DemoShapeProvider : ShapeTableProvider, IShapeAttributeProvider
{
public void Discover(ShapeTableBuilder builder)
public override ValueTask DiscoverAsync(ShapeTableBuilder builder)
{
builder.Describe("Foo")
.OnDisplaying(displaying =>
displaying.ChildContent = new HtmlString("<h1>Hi</h1>")
);

return ValueTask.CompletedTask;
}

[Shape]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,27 @@
using OrchardCore.Liquid;
using OrchardCore.Modules;

namespace OrchardCore.Facebook.Widgets.Services
namespace OrchardCore.Facebook.Widgets.Services;

[Feature(FacebookConstants.Features.Widgets)]
public class LiquidShapes(HtmlEncoder htmlEncoder) : ShapeTableProvider
{
[Feature(FacebookConstants.Features.Widgets)]
public class LiquidShapes : IShapeTableProvider
{
private readonly HtmlEncoder _htmlEncoder;
private readonly HtmlEncoder _htmlEncoder = htmlEncoder;

public LiquidShapes(HtmlEncoder htmlEncoder)
{
_htmlEncoder = htmlEncoder;
}
public override ValueTask DiscoverAsync(ShapeTableBuilder builder)
{
builder.Describe("FacebookPluginPart").OnProcessing(BuildViewModelAsync);
builder.Describe("FacebookPluginPart_Summary").OnProcessing(BuildViewModelAsync);

private async Task BuildViewModelAsync(ShapeDisplayContext shapeDisplayContext)
{
var model = shapeDisplayContext.Shape as FacebookPluginPartViewModel;
var liquidTemplateManager = shapeDisplayContext.ServiceProvider.GetRequiredService<ILiquidTemplateManager>();
return ValueTask.CompletedTask;
}

model.Html = await liquidTemplateManager.RenderStringAsync(model.FacebookPluginPart.Liquid, _htmlEncoder, shapeDisplayContext.DisplayContext.Value,
new Dictionary<string, FluidValue>() { ["ContentItem"] = new ObjectValue(model.ContentItem) });
}
private async Task BuildViewModelAsync(ShapeDisplayContext shapeDisplayContext)
{
var model = shapeDisplayContext.Shape as FacebookPluginPartViewModel;
var liquidTemplateManager = shapeDisplayContext.ServiceProvider.GetRequiredService<ILiquidTemplateManager>();

public void Discover(ShapeTableBuilder builder)
{
builder.Describe("FacebookPluginPart").OnProcessing(BuildViewModelAsync);
builder.Describe("FacebookPluginPart_Summary").OnProcessing(BuildViewModelAsync);
}
model.Html = await liquidTemplateManager.RenderStringAsync(model.FacebookPluginPart.Liquid, _htmlEncoder, shapeDisplayContext.DisplayContext.Value,
new Dictionary<string, FluidValue>() { ["ContentItem"] = new ObjectValue(model.ContentItem) });
}
}
38 changes: 20 additions & 18 deletions src/OrchardCore.Modules/OrchardCore.Html/Media/MediaShapes.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
using System.Threading.Tasks;
using OrchardCore.DisplayManagement.Descriptors;
using OrchardCore.Modules;

namespace OrchardCore.Html.Media
namespace OrchardCore.Html.Media;

[RequireFeatures("OrchardCore.Media")]
public class MediaShapes : ShapeTableProvider
{
[RequireFeatures("OrchardCore.Media")]
public class MediaShapes : IShapeTableProvider
public override ValueTask DiscoverAsync(ShapeTableBuilder builder)
{
public void Discover(ShapeTableBuilder builder)
{
builder.Describe("HtmlBodyPart_Edit")
.OnDisplaying(displaying =>
builder.Describe("HtmlBodyPart_Edit")
.OnDisplaying(displaying =>
{
var editor = displaying.Shape;
if (editor.Metadata.Type == "HtmlBodyPart_Edit__Wysiwyg")
{
var editor = displaying.Shape;
editor.Metadata.Wrappers.Add("Media_Wrapper__HtmlBodyPart");
}
if (editor.Metadata.Type == "HtmlBodyPart_Edit__Wysiwyg")
{
editor.Metadata.Wrappers.Add("Media_Wrapper__HtmlBodyPart");
}
if (editor.Metadata.Type == "HtmlBodyPart_Edit__Trumbowyg")
{
editor.Metadata.Wrappers.Add("Media_Wrapper__HtmlBodyPart");
}
});

if (editor.Metadata.Type == "HtmlBodyPart_Edit__Trumbowyg")
{
editor.Metadata.Wrappers.Add("Media_Wrapper__HtmlBodyPart");
}
});
}
return ValueTask.CompletedTask;
}
}
43 changes: 24 additions & 19 deletions src/OrchardCore.Modules/OrchardCore.Liquid/Services/LiquidShapes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,36 @@
using OrchardCore.Liquid.ViewModels;
using OrchardCore.Modules;

namespace OrchardCore.Liquid.Services
namespace OrchardCore.Liquid.Services;

[RequireFeatures("OrchardCore.Contents")]
public class LiquidShapes(HtmlEncoder htmlEncoder) : ShapeTableProvider
{
[RequireFeatures("OrchardCore.Contents")]
public class LiquidShapes : IShapeTableProvider
{
private readonly HtmlEncoder _htmlEncoder;
private readonly HtmlEncoder _htmlEncoder = htmlEncoder;

public LiquidShapes(HtmlEncoder htmlEncoder)
{
_htmlEncoder = htmlEncoder;
}
public override ValueTask DiscoverAsync(ShapeTableBuilder builder)
{
builder.Describe("LiquidPart").OnProcessing(BuildViewModelAsync);
builder.Describe("LiquidPart_Summary").OnProcessing(BuildViewModelAsync);

private async Task BuildViewModelAsync(ShapeDisplayContext shapeDisplayContext)
{
var model = shapeDisplayContext.Shape as LiquidPartViewModel;
var liquidTemplateManager = shapeDisplayContext.ServiceProvider.GetRequiredService<ILiquidTemplateManager>();
return ValueTask.CompletedTask;
}

model.Html = await liquidTemplateManager.RenderStringAsync(model.LiquidPart.Liquid, _htmlEncoder, shapeDisplayContext.DisplayContext.Value,
new Dictionary<string, FluidValue>() { ["ContentItem"] = new ObjectValue(model.ContentItem) });
}
private async Task BuildViewModelAsync(ShapeDisplayContext shapeDisplayContext)
{
var model = shapeDisplayContext.Shape as LiquidPartViewModel;

public void Discover(ShapeTableBuilder builder)
if (model?.LiquidPart is null)
{
builder.Describe("LiquidPart").OnProcessing(BuildViewModelAsync);
builder.Describe("LiquidPart_Summary").OnProcessing(BuildViewModelAsync);
return;
}

var liquidTemplateManager = shapeDisplayContext.ServiceProvider.GetRequiredService<ILiquidTemplateManager>();

model.Html = await liquidTemplateManager.RenderStringAsync(model.LiquidPart.Liquid, _htmlEncoder, shapeDisplayContext.DisplayContext.Value,
new Dictionary<string, FluidValue>()
{
["ContentItem"] = new ObjectValue(model.ContentItem)
});
}
}
46 changes: 24 additions & 22 deletions src/OrchardCore.Modules/OrchardCore.Markdown/Media/MediaShapes.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
using System.Threading.Tasks;
using OrchardCore.DisplayManagement.Descriptors;
using OrchardCore.Modules;

namespace OrchardCore.Markdown.Media
namespace OrchardCore.Markdown.Media;

[RequireFeatures("OrchardCore.Media")]
public class MediaShapes : ShapeTableProvider
{
[RequireFeatures("OrchardCore.Media")]
public class MediaShapes : IShapeTableProvider
public override ValueTask DiscoverAsync(ShapeTableBuilder builder)
{
public void Discover(ShapeTableBuilder builder)
{
builder.Describe("MarkdownBodyPart_Edit")
.OnDisplaying(displaying =>
builder.Describe("MarkdownBodyPart_Edit")
.OnDisplaying(displaying =>
{
var editor = displaying.Shape;
if (editor.Metadata.Type == "MarkdownBodyPart_Edit__Wysiwyg")
{
var editor = displaying.Shape;
editor.Metadata.Wrappers.Add("Media_Wrapper__MarkdownBodyPart");
}
});

if (editor.Metadata.Type == "MarkdownBodyPart_Edit__Wysiwyg")
{
editor.Metadata.Wrappers.Add("Media_Wrapper__MarkdownBodyPart");
}
});
builder.Describe("MarkdownField_Edit")
.OnDisplaying(displaying =>
{
var editor = displaying.Shape;
builder.Describe("MarkdownField_Edit")
.OnDisplaying(displaying =>
if (editor.Metadata.Type == "MarkdownField_Edit__Wysiwyg")
{
var editor = displaying.Shape;
editor.Metadata.Wrappers.Add("Media_Wrapper__MarkdownBodyPart");
}
});

if (editor.Metadata.Type == "MarkdownField_Edit__Wysiwyg")
{
editor.Metadata.Wrappers.Add("Media_Wrapper__MarkdownBodyPart");
}
});
}
return ValueTask.CompletedTask;
}
}
8 changes: 5 additions & 3 deletions src/OrchardCore.Modules/OrchardCore.Menu/MenuShapes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.ContentManagement;
using OrchardCore.DisplayManagement;
Expand All @@ -11,9 +11,9 @@

namespace OrchardCore.Menu
{
public class MenuShapes : IShapeTableProvider
public class MenuShapes : ShapeTableProvider
{
public void Discover(ShapeTableBuilder builder)
public override ValueTask DiscoverAsync(ShapeTableBuilder builder)
{
builder.Describe("Menu")
.OnProcessing(async context =>
Expand Down Expand Up @@ -174,6 +174,8 @@ public void Discover(ShapeTableBuilder builder)
menuItem.Metadata.Alternates.Add("MenuItemLink__" + differentiator + "__" + encodedContentType + "__level__" + level);
}
});

return ValueTask.CompletedTask;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -9,9 +10,9 @@

namespace OrchardCore.Navigation
{
public class NavigationShapes : IShapeTableProvider
public class NavigationShapes : ShapeTableProvider
{
public void Discover(ShapeTableBuilder builder)
public override ValueTask DiscoverAsync(ShapeTableBuilder builder)
{
builder.Describe("Navigation")
.OnDisplaying(displaying =>
Expand Down Expand Up @@ -103,6 +104,8 @@ public void Discover(ShapeTableBuilder builder)
menuItem.Metadata.Alternates.Add("NavigationItemLink__" + encodedMenuName);
menuItem.Metadata.Alternates.Add("NavigationItemLink__" + encodedMenuName + "__level__" + level);
});

return ValueTask.CompletedTask;
}
}
}
Loading

0 comments on commit 9ffa9a5

Please sign in to comment.