Handle multiple simultaneous block editor layouts (#16184)
* Handle multiple simultaneous block editor layouts * Cleanup --------- Co-authored-by: Bjarke Berg <mail@bergmania.dk>
This commit is contained in:
@@ -40,7 +40,7 @@ public class BlockEditorData<TValue, TLayout>
|
||||
/// <summary>
|
||||
/// Returns the layout for this specific property editor
|
||||
/// </summary>
|
||||
public IEnumerable<TLayout>? Layout => BlockValue.Layout.TryGetValue(_propertyEditorAlias, out IEnumerable<TLayout>? layout) ? layout : null;
|
||||
public IEnumerable<TLayout>? Layout => BlockValue.GetLayouts(_propertyEditorAlias);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the reference to the original BlockValue
|
||||
|
||||
@@ -15,21 +15,23 @@ public abstract class BlockEditorDataConverter<TValue, TLayout>
|
||||
where TValue : BlockValue<TLayout>, new()
|
||||
where TLayout : class, IBlockLayoutItem, new()
|
||||
{
|
||||
private readonly string _propertyEditorAlias;
|
||||
private readonly IJsonSerializer _jsonSerializer;
|
||||
|
||||
[Obsolete("Use the constructor that takes IJsonSerializer. Will be removed in V15.")]
|
||||
[Obsolete("Use the non-obsolete constructor. Will be removed in V15.")]
|
||||
protected BlockEditorDataConverter(string propertyEditorAlias)
|
||||
: this(propertyEditorAlias, StaticServiceProvider.Instance.GetRequiredService<IJsonSerializer>())
|
||||
{
|
||||
}
|
||||
|
||||
[Obsolete("Use the non-obsolete constructor. Will be removed in V15.")]
|
||||
protected BlockEditorDataConverter(string propertyEditorAlias, IJsonSerializer jsonSerializer)
|
||||
: this(jsonSerializer)
|
||||
{
|
||||
_propertyEditorAlias = propertyEditorAlias;
|
||||
_jsonSerializer = jsonSerializer;
|
||||
}
|
||||
|
||||
protected BlockEditorDataConverter(IJsonSerializer jsonSerializer)
|
||||
=> _jsonSerializer = jsonSerializer;
|
||||
|
||||
public bool TryDeserialize(string json, [MaybeNullWhen(false)] out BlockEditorData<TValue, TLayout> blockEditorData)
|
||||
{
|
||||
try
|
||||
@@ -60,16 +62,15 @@ public abstract class BlockEditorDataConverter<TValue, TLayout>
|
||||
|
||||
public BlockEditorData<TValue, TLayout> Convert(TValue? value)
|
||||
{
|
||||
if (value?.Layout == null)
|
||||
var propertyEditorAlias = new TValue().PropertyEditorAlias;
|
||||
IEnumerable<TLayout>? layouts = value?.GetLayouts(propertyEditorAlias);
|
||||
if (layouts is null)
|
||||
{
|
||||
return BlockEditorData<TValue, TLayout>.Empty;
|
||||
}
|
||||
|
||||
IEnumerable<ContentAndSettingsReference> references =
|
||||
value.Layout.TryGetValue(_propertyEditorAlias, out IEnumerable<TLayout>? layout)
|
||||
? GetBlockReferences(layout)
|
||||
: Enumerable.Empty<ContentAndSettingsReference>();
|
||||
IEnumerable<ContentAndSettingsReference> references = GetBlockReferences(layouts);
|
||||
|
||||
return new BlockEditorData<TValue, TLayout>(_propertyEditorAlias, references, value);
|
||||
return new BlockEditorData<TValue, TLayout>(propertyEditorAlias, references, value!);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ public class BlockGridEditorDataConverter : BlockEditorDataConverter<BlockGridVa
|
||||
}
|
||||
|
||||
public BlockGridEditorDataConverter(IJsonSerializer jsonSerializer)
|
||||
: base(Constants.PropertyEditors.Aliases.BlockGrid, jsonSerializer)
|
||||
: base(jsonSerializer)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -2,4 +2,5 @@
|
||||
|
||||
public class BlockGridValue : BlockValue<BlockGridLayoutItem>
|
||||
{
|
||||
public override string PropertyEditorAlias => Constants.PropertyEditors.Aliases.BlockGrid;
|
||||
}
|
||||
|
||||
@@ -2,4 +2,5 @@
|
||||
|
||||
public class BlockListValue : BlockValue<BlockListLayoutItem>
|
||||
{
|
||||
public override string PropertyEditorAlias => Constants.PropertyEditors.Aliases.BlockList;
|
||||
}
|
||||
|
||||
@@ -3,11 +3,22 @@
|
||||
|
||||
namespace Umbraco.Cms.Core.Models.Blocks;
|
||||
|
||||
public abstract class BlockValue<TLayout> where TLayout : IBlockLayoutItem
|
||||
public abstract class BlockValue<TLayout> : BlockValue
|
||||
where TLayout : IBlockLayoutItem
|
||||
{
|
||||
public IDictionary<string, IEnumerable<TLayout>> Layout { get; set; } = null!;
|
||||
public IEnumerable<TLayout>? GetLayouts(string propertyEditorAlias)
|
||||
=> Layout.TryGetValue(propertyEditorAlias, out IEnumerable<IBlockLayoutItem>? layouts) is true
|
||||
? layouts.OfType<TLayout>()
|
||||
: null;
|
||||
}
|
||||
|
||||
public abstract class BlockValue
|
||||
{
|
||||
public IDictionary<string, IEnumerable<IBlockLayoutItem>> Layout { get; set; } = new Dictionary<string, IEnumerable<IBlockLayoutItem>>();
|
||||
|
||||
public List<BlockItemData> ContentData { get; set; } = new();
|
||||
|
||||
public List<BlockItemData> SettingsData { get; set; } = new();
|
||||
|
||||
public abstract string PropertyEditorAlias { get; }
|
||||
}
|
||||
|
||||
@@ -2,4 +2,5 @@ namespace Umbraco.Cms.Core.Models.Blocks;
|
||||
|
||||
public class RichTextBlockValue : BlockValue<RichTextBlockLayoutItem>
|
||||
{
|
||||
public override string PropertyEditorAlias => Constants.PropertyEditors.Aliases.TinyMce;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,23 @@
|
||||
namespace Umbraco.Cms.Core.Models.Blocks;
|
||||
using Umbraco.Cms.Core.Serialization;
|
||||
|
||||
namespace Umbraco.Cms.Core.Models.Blocks;
|
||||
|
||||
/// <summary>
|
||||
/// Data converter for blocks in the richtext property editor
|
||||
/// </summary>
|
||||
public sealed class RichTextEditorBlockDataConverter : BlockEditorDataConverter<RichTextBlockValue, RichTextBlockLayoutItem>
|
||||
{
|
||||
[Obsolete("Use the constructor that takes IJsonSerializer. Will be removed in V15.")]
|
||||
public RichTextEditorBlockDataConverter()
|
||||
: base(Constants.PropertyEditors.Aliases.TinyMce)
|
||||
{
|
||||
}
|
||||
|
||||
public RichTextEditorBlockDataConverter(IJsonSerializer jsonSerializer)
|
||||
: base(jsonSerializer)
|
||||
{
|
||||
}
|
||||
|
||||
protected override IEnumerable<ContentAndSettingsReference> GetBlockReferences(IEnumerable<RichTextBlockLayoutItem> layout)
|
||||
=> layout.Select(x => new ContentAndSettingsReference(x.ContentUdi, x.SettingsUdi)).ToList();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user