Skip to content

Commit

Permalink
Creating ContentPart_Edit Shape using ShapeResult to support placement (
Browse files Browse the repository at this point in the history
#11098)

Fixes #11088
Fixes #6784
Fixes #6850
  • Loading branch information
ns8482e authored Mar 31, 2022
1 parent ee83d32 commit 5d3cdca
Showing 1 changed file with 83 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,53 @@ public async Task BuildEditorAsync(ContentItem contentItem, BuildEditorContext c
foreach (var typePartDefinition in contentTypeDefinition.Parts)
{
var partTypeName = typePartDefinition.PartDefinition.Name;
var partName = typePartDefinition.Name;
var contentType = typePartDefinition.ContentTypeDefinition.Name;
var activator = _contentPartFactory.GetTypeActivator(partTypeName);
var part = (ContentPart)contentItem.Get(activator.Type, typePartDefinition.Name) ?? activator.CreateInstance();
var part = (ContentPart)contentItem.Get(activator.Type, partName) ?? activator.CreateInstance();
var isNamedPart = typePartDefinition.PartDefinition.IsReusable() && partName != partTypeName;
var partPosition = typePartDefinition.GetSettings<ContentTypePartSettings>().Position ?? "before";
part.ContentItem = contentItem;

// Create a custom shape to render all the part shapes into it
var typePartShape = await context.ShapeFactory.CreateAsync("ContentPart_Edit");
typePartShape.Properties["ContentPart"] = part;
typePartShape.Properties["ContentTypePartDefinition"] = typePartDefinition;
var shapeType = "ContentPart_Edit";
var typePartShapeResult = new ShapeResult(shapeType, ctx => ctx.ShapeFactory.CreateAsync(shapeType));
typePartShapeResult.Differentiator($"{contentType}-{partName}");
typePartShapeResult.Name(partName);
typePartShapeResult.Location($"Parts:{partPosition}");

var partPosition = typePartDefinition.GetSettings<ContentTypePartSettings>().Position ?? "before";
typePartShapeResult.Displaying(ctx =>
{
// ContentPart_Edit__[PartType]
// eg ContentPart-ServicePart.Edit
ctx.Shape.Metadata.Alternates.Add($"{shapeType}__{partTypeName}");

// ContentPart_Edit__[ContentType]__[PartType]
// e.g. ContentPart-LandingPage-ServicePart.Edit
ctx.Shape.Metadata.Alternates.Add($"{shapeType}__{contentType}__{partTypeName}");

await partsShape.AddAsync(typePartShape, partPosition);
partsShape.Properties[typePartDefinition.Name] = typePartShape;
if (isNamedPart)
{
// ContentPart_Edit__[ContentType]__[PartName]
// e.g. ContentPart-LandingPage-BillingService.Edit ContentPart-LandingPage-HelplineService.Edit
ctx.Shape.Metadata.Alternates.Add($"{shapeType}__{contentType}__{partName}");
}
});

await typePartShapeResult.ApplyAsync(context);
var typePartShape = typePartShapeResult.Shape;

if (typePartShape == null)
{
// Part is explicitly noop in placement then stop rendering execution
continue;
}

typePartShape.Properties["ContentPart"] = part;
typePartShape.Properties["ContentTypePartDefinition"] = typePartDefinition;
partsShape.Properties[partName] = typePartShape;

context.DefaultZone = $"Parts.{typePartDefinition.Name}";
context.DefaultZone = $"Parts.{partName}";
context.DefaultPosition = partPosition;

var partDisplayDrivers = _contentPartDisplayDriverResolver.GetEditorDrivers(partTypeName, typePartDefinition.Editor());
Expand All @@ -258,7 +290,7 @@ await partDisplayDrivers.InvokeAsync(async (driver, part, typePartDefinition, co
var fieldName = partFieldDefinition.Name;
var fieldPosition = partFieldDefinition.GetSettings<ContentPartFieldSettings>().Position ?? "before";

context.DefaultZone = $"Parts.{typePartDefinition.Name}:{fieldPosition}";
context.DefaultZone = $"Parts.{partName}:{fieldPosition}";
var fieldDisplayDrivers = _contentFieldDisplayDriverResolver.GetEditorDrivers(partFieldDefinition.FieldDefinition.Name, partFieldDefinition.Editor());
await fieldDisplayDrivers.InvokeAsync(async (driver, part, partFieldDefinition, typePartDefinition, context) =>
{
Expand Down Expand Up @@ -306,20 +338,54 @@ public async Task UpdateEditorAsync(ContentItem contentItem, UpdateEditorContext
foreach (var typePartDefinition in contentTypeDefinition.Parts)
{
var partTypeName = typePartDefinition.PartDefinition.Name;
var partName = typePartDefinition.Name;
var contentType = typePartDefinition.ContentTypeDefinition.Name;
var activator = _contentPartFactory.GetTypeActivator(partTypeName);
var part = (ContentPart)contentItem.Get(activator.Type, typePartDefinition.Name) ?? activator.CreateInstance();
var part = (ContentPart)contentItem.Get(activator.Type, partName) ?? activator.CreateInstance();
var isNamedPart = typePartDefinition.PartDefinition.IsReusable() && partName != partTypeName;
var partPosition = typePartDefinition.GetSettings<ContentTypePartSettings>().Position ?? "before";
part.ContentItem = contentItem;

// Create a custom shape to render all the part shapes into it
var typePartShape = await context.ShapeFactory.CreateAsync("ContentPart_Edit");
var shapeType = "ContentPart_Edit";
var typePartShapeResult = new ShapeResult(shapeType, ctx => ctx.ShapeFactory.CreateAsync(shapeType));

typePartShapeResult.Differentiator($"{contentType}-{partName}");
typePartShapeResult.Name(partName);
typePartShapeResult.Location($"Parts:{partPosition}");

typePartShapeResult.Displaying(ctx =>
{
// ContentPart_Edit__[PartType]
// eg ContentPart-ServicePart.Edit
ctx.Shape.Metadata.Alternates.Add($"{shapeType}__{partTypeName}");

// ContentPart_Edit__[ContentType]__[PartType]
// e.g. ContentPart-LandingPage-ServicePart.Edit
ctx.Shape.Metadata.Alternates.Add($"{shapeType}__{contentType}__{partTypeName}");

if (isNamedPart)
{
// ContentPart_Edit__[ContentType]__[PartName]
// e.g. ContentPart-LandingPage-BillingService.Edit ContentPart-LandingPage-HelplineService.Edit
ctx.Shape.Metadata.Alternates.Add($"{shapeType}__{contentType}__{partName}");
}
});

await typePartShapeResult.ApplyAsync(context);
var typePartShape = typePartShapeResult.Shape;

if (typePartShape == null)
{
// Part is explicitly hidden in placement then stop rendering it
continue;
}

typePartShape.Properties["ContentPart"] = part;
typePartShape.Properties["ContentTypePartDefinition"] = typePartDefinition;
var partPosition = typePartDefinition.GetSettings<ContentTypePartSettings>().Position ?? "before";

await partsShape.AddAsync(typePartShape, partPosition);
partsShape.Properties[typePartDefinition.Name] = typePartShape;
partsShape.Properties[partName] = typePartShape;

context.DefaultZone = $"Parts.{typePartDefinition.Name}:{partPosition}";
context.DefaultZone = $"Parts.{partName}:{partPosition}";
var partDisplayDrivers = _contentPartDisplayDriverResolver.GetEditorDrivers(partTypeName, typePartDefinition.Editor());
await partDisplayDrivers.InvokeAsync(async (driver, part, typePartDefinition, context) =>
{
Expand All @@ -335,7 +401,7 @@ await partDisplayDrivers.InvokeAsync(async (driver, part, typePartDefinition, co
var fieldName = partFieldDefinition.Name;
var fieldPosition = partFieldDefinition.GetSettings<ContentPartFieldSettings>().Position ?? "before";

context.DefaultZone = $"Parts.{typePartDefinition.Name}:{fieldPosition}";
context.DefaultZone = $"Parts.{partName}:{fieldPosition}";
var fieldDisplayDrivers = _contentFieldDisplayDriverResolver.GetEditorDrivers(partFieldDefinition.FieldDefinition.Name, partFieldDefinition.Editor());
await fieldDisplayDrivers.InvokeAsync(async (driver, part, partFieldDefinition, typePartDefinition, context) =>
{
Expand Down

0 comments on commit 5d3cdca

Please sign in to comment.