Skip to content

Commit cbf9781

Browse files
ronaldbarendsekjac
andauthored
DX: Add BlockValue constructors to force correct property editor alias and layout item type (#16266)
* Improve getting and initializing new block value layouts * Remove unnecessary generic type constraints * Add and use new block value/layout item constructors in tests * Removed GetLayouts that did not make sense * Added constructor to BlockItemData to simplify explicit usages --------- Co-authored-by: kjac <[email protected]>
1 parent c7328bc commit cbf9781

File tree

14 files changed

+279
-284
lines changed

14 files changed

+279
-284
lines changed

src/Umbraco.Core/Models/Blocks/BlockEditorData.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,25 @@ namespace Umbraco.Cms.Core.Models.Blocks;
88
/// </summary>
99
public class BlockEditorData<TValue, TLayout>
1010
where TValue : BlockValue<TLayout>, new()
11-
where TLayout : class, IBlockLayoutItem, new()
11+
where TLayout : IBlockLayoutItem
1212
{
13-
private readonly string _propertyEditorAlias;
14-
15-
public BlockEditorData(
16-
string propertyEditorAlias,
17-
IEnumerable<ContentAndSettingsReference> references,
18-
TValue blockValue)
13+
public BlockEditorData(IEnumerable<ContentAndSettingsReference> references, TValue blockValue)
1914
{
20-
if (string.IsNullOrWhiteSpace(propertyEditorAlias))
21-
{
22-
throw new ArgumentException($"'{nameof(propertyEditorAlias)}' cannot be null or whitespace", nameof(propertyEditorAlias));
23-
}
24-
25-
_propertyEditorAlias = propertyEditorAlias;
2615
BlockValue = blockValue ?? throw new ArgumentNullException(nameof(blockValue));
2716
References = references != null
2817
? new List<ContentAndSettingsReference>(references)
2918
: throw new ArgumentNullException(nameof(references));
3019
}
3120

3221
private BlockEditorData()
33-
{
34-
_propertyEditorAlias = string.Empty;
35-
BlockValue = new TValue();
36-
}
22+
=> BlockValue = new TValue();
3723

3824
public static BlockEditorData<TValue, TLayout> Empty { get; } = new();
3925

4026
/// <summary>
4127
/// Returns the layout for this specific property editor
4228
/// </summary>
43-
public IEnumerable<TLayout>? Layout => BlockValue.GetLayouts(_propertyEditorAlias);
29+
public IEnumerable<TLayout>? Layout => BlockValue.GetLayouts();
4430

4531
/// <summary>
4632
/// Returns the reference to the original BlockValue

src/Umbraco.Core/Models/Blocks/BlockEditorDataConverter.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Umbraco.Cms.Core.Models.Blocks;
1313
/// </summary>
1414
public abstract class BlockEditorDataConverter<TValue, TLayout>
1515
where TValue : BlockValue<TLayout>, new()
16-
where TLayout : class, IBlockLayoutItem, new()
16+
where TLayout : IBlockLayoutItem
1717
{
1818
private readonly IJsonSerializer _jsonSerializer;
1919

@@ -42,7 +42,7 @@ public bool TryDeserialize(string json, [MaybeNullWhen(false)] out BlockEditorDa
4242
}
4343
catch (Exception)
4444
{
45-
blockEditorData = null;
45+
blockEditorData = default;
4646
return false;
4747
}
4848
}
@@ -62,15 +62,13 @@ public BlockEditorData<TValue, TLayout> Deserialize(string json)
6262

6363
public BlockEditorData<TValue, TLayout> Convert(TValue? value)
6464
{
65-
var propertyEditorAlias = new TValue().PropertyEditorAlias;
66-
IEnumerable<TLayout>? layouts = value?.GetLayouts(propertyEditorAlias);
67-
if (layouts is null)
65+
if (value?.GetLayouts() is not IEnumerable<TLayout> layouts)
6866
{
6967
return BlockEditorData<TValue, TLayout>.Empty;
7068
}
7169

7270
IEnumerable<ContentAndSettingsReference> references = GetBlockReferences(layouts);
7371

74-
return new BlockEditorData<TValue, TLayout>(propertyEditorAlias, references, value!);
72+
return new BlockEditorData<TValue, TLayout>(references, value);
7573
}
7674
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
namespace Umbraco.Cms.Core.Models.Blocks;
1+
namespace Umbraco.Cms.Core.Models.Blocks;
22

33
public class BlockGridLayoutAreaItem
44
{
55
public Guid Key { get; set; } = Guid.Empty;
66

77
public BlockGridLayoutItem[] Items { get; set; } = Array.Empty<BlockGridLayoutItem>();
8+
9+
public BlockGridLayoutAreaItem()
10+
{ }
11+
12+
public BlockGridLayoutAreaItem(Guid key)
13+
=> Key = key;
814
}

src/Umbraco.Core/Models/Blocks/BlockGridLayoutItem.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,14 @@ public class BlockGridLayoutItem : IBlockLayoutItem
1717
public int? RowSpan { get; set; }
1818

1919
public BlockGridLayoutAreaItem[] Areas { get; set; } = Array.Empty<BlockGridLayoutAreaItem>();
20+
21+
public BlockGridLayoutItem()
22+
{ }
23+
24+
public BlockGridLayoutItem(Udi contentUdi)
25+
=> ContentUdi = contentUdi;
26+
27+
public BlockGridLayoutItem(Udi contentUdi, Udi settingsUdi)
28+
: this(contentUdi)
29+
=> SettingsUdi = settingsUdi;
2030
}
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1-
namespace Umbraco.Cms.Core.Models.Blocks;
1+
namespace Umbraco.Cms.Core.Models.Blocks;
22

3+
/// <summary>
4+
/// Represents a block grid value.
5+
/// </summary>
36
public class BlockGridValue : BlockValue<BlockGridLayoutItem>
47
{
8+
/// <summary>
9+
/// Initializes a new instance of the <see cref="BlockGridValue" /> class.
10+
/// </summary>
11+
public BlockGridValue()
12+
{ }
13+
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="BlockGridValue" /> class.
16+
/// </summary>
17+
/// <param name="layouts">The layouts.</param>
18+
public BlockGridValue(IEnumerable<BlockGridLayoutItem> layouts)
19+
=> Layout[PropertyEditorAlias] = layouts;
20+
21+
/// <inheritdoc />
522
public override string PropertyEditorAlias => Constants.PropertyEditors.Aliases.BlockGrid;
623
}

src/Umbraco.Core/Models/Blocks/BlockItemData.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ namespace Umbraco.Cms.Core.Models.Blocks;
1010
/// </summary>
1111
public class BlockItemData
1212
{
13+
public BlockItemData()
14+
{
15+
}
16+
17+
public BlockItemData(Udi udi, Guid contentTypeKey, string contentTypeAlias)
18+
{
19+
ContentTypeAlias = contentTypeAlias;
20+
Udi = udi;
21+
ContentTypeKey = contentTypeKey;
22+
}
23+
1324
public Guid ContentTypeKey { get; set; }
1425

1526
/// <summary>

src/Umbraco.Core/Models/Blocks/BlockListLayoutItem.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,14 @@ public class BlockListLayoutItem : IBlockLayoutItem
1111
public Udi? ContentUdi { get; set; }
1212

1313
public Udi? SettingsUdi { get; set; }
14+
15+
public BlockListLayoutItem()
16+
{ }
17+
18+
public BlockListLayoutItem(Udi contentUdi)
19+
=> ContentUdi = contentUdi;
20+
21+
public BlockListLayoutItem(Udi contentUdi, Udi settingsUdi)
22+
: this(contentUdi)
23+
=> SettingsUdi = settingsUdi;
1424
}
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1-
namespace Umbraco.Cms.Core.Models.Blocks;
1+
namespace Umbraco.Cms.Core.Models.Blocks;
22

3+
/// <summary>
4+
/// Represents a block list value.
5+
/// </summary>
36
public class BlockListValue : BlockValue<BlockListLayoutItem>
47
{
8+
/// <summary>
9+
/// Initializes a new instance of the <see cref="BlockListValue" /> class.
10+
/// </summary>
11+
public BlockListValue()
12+
{ }
13+
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="BlockListValue" /> class.
16+
/// </summary>
17+
/// <param name="layouts">The layouts.</param>
18+
public BlockListValue(IEnumerable<BlockListLayoutItem> layouts)
19+
=> Layout[PropertyEditorAlias] = layouts;
20+
21+
/// <inheritdoc />
522
public override string PropertyEditorAlias => Constants.PropertyEditors.Aliases.BlockList;
623
}

src/Umbraco.Core/Models/Blocks/BlockValue.cs

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,56 @@
33

44
namespace Umbraco.Cms.Core.Models.Blocks;
55

6-
public abstract class BlockValue<TLayout> : BlockValue
7-
where TLayout : IBlockLayoutItem
8-
{
9-
public IEnumerable<TLayout>? GetLayouts(string propertyEditorAlias)
10-
=> Layout.TryGetValue(propertyEditorAlias, out IEnumerable<IBlockLayoutItem>? layouts) is true
11-
? layouts.OfType<TLayout>()
12-
: null;
13-
}
14-
6+
/// <summary>
7+
/// Represents a block value.
8+
/// </summary>
159
public abstract class BlockValue
1610
{
11+
/// <summary>
12+
/// Gets or sets the layout for specific property editors.
13+
/// </summary>
14+
/// <value>
15+
/// The layout.
16+
/// </value>
1717
public IDictionary<string, IEnumerable<IBlockLayoutItem>> Layout { get; set; } = new Dictionary<string, IEnumerable<IBlockLayoutItem>>();
1818

19-
public List<BlockItemData> ContentData { get; set; } = new();
19+
/// <summary>
20+
/// Gets or sets the content data.
21+
/// </summary>
22+
/// <value>
23+
/// The content data.
24+
/// </value>
25+
public List<BlockItemData> ContentData { get; set; } = [];
2026

21-
public List<BlockItemData> SettingsData { get; set; } = new();
27+
/// <summary>
28+
/// Gets or sets the settings data.
29+
/// </summary>
30+
/// <value>
31+
/// The settings data.
32+
/// </value>
33+
public List<BlockItemData> SettingsData { get; set; } = [];
2234

35+
/// <summary>
36+
/// Gets the property editor alias of the current layout.
37+
/// </summary>
38+
/// <value>
39+
/// The property editor alias of the current layout.
40+
/// </value>
2341
public abstract string PropertyEditorAlias { get; }
2442
}
43+
44+
/// <inheritdoc />
45+
public abstract class BlockValue<TLayout> : BlockValue
46+
where TLayout : IBlockLayoutItem
47+
{
48+
/// <summary>
49+
/// Gets the layouts of the current property editor.
50+
/// </summary>
51+
/// <returns>
52+
/// The layouts.
53+
/// </returns>
54+
public IEnumerable<TLayout>? GetLayouts()
55+
=> Layout.TryGetValue(PropertyEditorAlias, out IEnumerable<IBlockLayoutItem>? layouts)
56+
? layouts.OfType<TLayout>()
57+
: null;
58+
}

src/Umbraco.Core/Models/Blocks/RichTextBlockLayoutItem.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Umbraco.
1+
// Copyright (c) Umbraco.
22
// See LICENSE for more details.
33

44
namespace Umbraco.Cms.Core.Models.Blocks;
@@ -11,4 +11,14 @@ public class RichTextBlockLayoutItem : IBlockLayoutItem
1111
public Udi? ContentUdi { get; set; }
1212

1313
public Udi? SettingsUdi { get; set; }
14+
15+
public RichTextBlockLayoutItem()
16+
{ }
17+
18+
public RichTextBlockLayoutItem(Udi contentUdi)
19+
=> ContentUdi = contentUdi;
20+
21+
public RichTextBlockLayoutItem(Udi contentUdi, Udi settingsUdi)
22+
: this(contentUdi)
23+
=> SettingsUdi = settingsUdi;
1424
}

0 commit comments

Comments
 (0)