-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c024a8a
commit ec7e433
Showing
8 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/Perplex.ContentBlocks.DeliveryApi/ApiContentBlockViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Umbraco.Cms.Core.Models.DeliveryApi; | ||
|
||
namespace Perplex.ContentBlocks.DeliveryApi; | ||
|
||
public class ApiContentBlockViewModel : IApiContentBlockViewModel | ||
{ | ||
public Guid Id { get; init; } | ||
|
||
public Guid DefinitionId { get; init; } | ||
|
||
public Guid LayoutId { get; init; } | ||
|
||
public IApiElement? Content { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Perplex.ContentBlocks.DeliveryApi; | ||
public class ApiContentBlocks | ||
{ | ||
public static readonly ApiContentBlocks Empty = new(); | ||
|
||
public IApiContentBlockViewModel? Header { get; init; } | ||
public IEnumerable<IApiContentBlockViewModel> Blocks { get; init; } = Array.Empty<IApiContentBlockViewModel>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Perplex.ContentBlocks.PropertyEditor; | ||
using Umbraco.Cms.Core.Composing; | ||
using Umbraco.Cms.Core.DependencyInjection; | ||
|
||
namespace Perplex.ContentBlocks.DeliveryApi; | ||
public class Composer : IComposer | ||
{ | ||
public void Compose(IUmbracoBuilder builder) | ||
{ | ||
builder.PropertyValueConverters().Remove<ContentBlocksValueConverter>(); | ||
|
||
// We use the original value converter in the Delivery API value converter so it needs to be registered. | ||
builder.Services | ||
.RemoveAll<ContentBlocksValueConverter>() | ||
.AddSingleton<ContentBlocksValueConverter>(); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/Perplex.ContentBlocks.DeliveryApi/ContentBlocksApiValueConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Perplex.ContentBlocks.PropertyEditor; | ||
using Perplex.ContentBlocks.Rendering; | ||
using Umbraco.Cms.Core.DeliveryApi; | ||
using Umbraco.Cms.Core.Models.PublishedContent; | ||
using Umbraco.Cms.Core.PropertyEditors; | ||
using Umbraco.Cms.Core.PropertyEditors.DeliveryApi; | ||
|
||
namespace Perplex.ContentBlocks.DeliveryApi; | ||
|
||
public class ContentBlocksApiValueConverter : IDeliveryApiPropertyValueConverter | ||
{ | ||
private readonly ContentBlocksValueConverter _contentBlocksValueConverter; | ||
private readonly IApiElementBuilder _apiElementBuilder; | ||
|
||
public ContentBlocksApiValueConverter( | ||
ContentBlocksValueConverter contentBlocksValueConverter, | ||
IApiElementBuilder apiElementBuilder) | ||
{ | ||
_contentBlocksValueConverter = contentBlocksValueConverter; | ||
_apiElementBuilder = apiElementBuilder; | ||
} | ||
|
||
public bool IsConverter(IPublishedPropertyType propertyType) | ||
=> propertyType.EditorAlias == Constants.PropertyEditor.Alias; | ||
|
||
public PropertyCacheLevel GetDeliveryApiPropertyCacheLevel(IPublishedPropertyType propertyType) | ||
=> GetPropertyCacheLevel(propertyType); | ||
|
||
public Type GetDeliveryApiPropertyValueType(IPublishedPropertyType propertyType) | ||
=> typeof(IApiContentBlocks); | ||
|
||
public object? ConvertIntermediateToDeliveryApiObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview, bool expanding) | ||
{ | ||
var modelValue = _contentBlocksValueConverter.ConvertIntermediateToObject(owner, propertyType, referenceCacheLevel, inter, preview); | ||
if (modelValue is not IContentBlocks contentBlocks) | ||
{ | ||
return null; | ||
} | ||
|
||
return new ApiContentBlocks | ||
{ | ||
Header = Map(contentBlocks.Header), | ||
Blocks = contentBlocks.Blocks.Select(Map).OfType<IApiContentBlockViewModel>().ToArray(), | ||
}; | ||
|
||
ApiContentBlockViewModel? Map(IContentBlockViewModel? vm) | ||
{ | ||
if (vm is null) | ||
{ | ||
return null; | ||
} | ||
|
||
return new ApiContentBlockViewModel | ||
{ | ||
Id = vm.Id, | ||
DefinitionId = vm.DefinitionId, | ||
LayoutId = vm.LayoutId, | ||
Content = _apiElementBuilder.Build(vm.Content), | ||
}; | ||
} | ||
} | ||
|
||
#region Forwarded to ContentBlocksValueConverter | ||
public object? ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview) => _contentBlocksValueConverter.ConvertIntermediateToObject(owner, propertyType, referenceCacheLevel, inter, preview); | ||
public object? ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview) => _contentBlocksValueConverter.ConvertIntermediateToXPath(owner, propertyType, referenceCacheLevel, inter, preview); | ||
public object? ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview) => _contentBlocksValueConverter.ConvertSourceToIntermediate(owner, propertyType, source, preview); | ||
public PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType) => _contentBlocksValueConverter.GetPropertyCacheLevel(propertyType); | ||
public Type GetPropertyValueType(IPublishedPropertyType propertyType) => _contentBlocksValueConverter.GetPropertyValueType(propertyType); | ||
public bool? IsValue(object? value, PropertyValueLevel level) => _contentBlocksValueConverter.IsValue(value, level); | ||
#endregion | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Perplex.ContentBlocks.DeliveryApi/IApiContentBlockViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Umbraco.Cms.Core.Models.DeliveryApi; | ||
|
||
namespace Perplex.ContentBlocks.DeliveryApi; | ||
|
||
public interface IApiContentBlockViewModel | ||
{ | ||
Guid Id { get; } | ||
|
||
Guid DefinitionId { get; } | ||
|
||
Guid LayoutId { get; } | ||
|
||
IApiElement? Content { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Perplex.ContentBlocks.DeliveryApi; | ||
public interface IApiContentBlocks | ||
{ | ||
IApiContentBlockViewModel Header { get; } | ||
IEnumerable<IApiContentBlockViewModel> Blocks { get; } | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Perplex.ContentBlocks.DeliveryApi/Perplex.ContentBlocks.DeliveryApi.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<GenerateDocumentationFile>True</GenerateDocumentationFile> | ||
<ProduceReferenceAssembly>False</ProduceReferenceAssembly> | ||
<NoWarn>$(NoWarn);NU1902</NoWarn> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Label="NuGet package"> | ||
<PackageId>Perplex.ContentBlocks.DeliveryApi</PackageId> | ||
<Description>Adds support for the Content Delivery API in Umbraco 12+</Description> | ||
<PackageTags>umbraco</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Umbraco.Cms.Web.BackOffice" Version="12.0.0" /> | ||
<ProjectReference Include="..\Perplex.ContentBlocks.Core\Perplex.ContentBlocks.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters