Skip to content

Commit

Permalink
feat(blocks): move block priority and max count out of descriptor and…
Browse files Browse the repository at this point in the history
… into metadata
  • Loading branch information
SonicGD committed Sep 8, 2021
1 parent a1f3ac1 commit 0ff5397
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/Sitko.Blockly.Blazor/Forms/BlocklyForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected override void OnInitialized()
base.OnInitialized();
BlockDescriptors = Blockly.Descriptors
.Where(d => FormOptions.AllowedBlocks.Any() == false || FormOptions.AllowedBlocks.Contains(d.Type))
.OrderBy(d => FormOptions.BlockPriority(d) ?? d.Priority).ThenBy(d => d.Type.FullName).ToArray();
.OrderBy(d => FormOptions.BlockPriority(d)).ThenBy(d => d.Type.FullName).ToArray();
OrderedBlocks.SetItems(CurrentValue?.OrderBy(b => b.Position) ?? new List<ContentBlock>().AsEnumerable());
Blocks.AddRange(CurrentValue ?? new List<ContentBlock>());
BlocklyFormService.AddForm(this);
Expand All @@ -100,7 +100,7 @@ protected bool CanAdd(IBlazorBlockDescriptor blockDescriptor)
return false;
}

var blockMaxCount = FormOptions.MaxBlockCount(blockDescriptor) ?? blockDescriptor.MaxCount;
var blockMaxCount = FormOptions.MaxBlockCount(blockDescriptor);
if (blockMaxCount > 0)
{
var blocksCount = CurrentValue?.Count(b => b.GetType() == blockDescriptor.Type);
Expand Down
4 changes: 0 additions & 4 deletions src/Sitko.Blockly/BlockDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ public interface IBlockDescriptor
{
string Title { get; }
Type Type { get; }
int MaxCount { get; }
int Priority { get; }
string Key { get; }
bool ShouldRender(BlockListContext context, ContentBlock block) => true;
bool ShouldRenderNext(BlockListContext context, ContentBlock block) => true;
Expand All @@ -25,8 +23,6 @@ public abstract record BlockDescriptor : IBlockDescriptor
public abstract string Title { get; }
public abstract Type Type { get; }
public abstract string Key { get; }
public virtual int MaxCount { get; } = 0;
public virtual int Priority { get; } = int.MaxValue;
}

public abstract record BlockDescriptor<TBlock> : BlockDescriptor, IBlockDescriptor<TBlock>
Expand Down
15 changes: 15 additions & 0 deletions src/Sitko.Blockly/Blockly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Sitko.Blockly
{
using System.Collections.Concurrent;
using System.Reflection;

public interface IBlockly<TBlockDescriptor> where TBlockDescriptor : IBlockDescriptor
{
Expand All @@ -23,6 +24,7 @@ public interface IBlockly<TBlockDescriptor> where TBlockDescriptor : IBlockDescr
public class Blockly
{
protected static readonly ConcurrentDictionary<Type, IBlockDescriptor> StaticDescriptors = new();
protected static readonly ConcurrentDictionary<Type, IContentBlockMetadata> StaticMetadata = new();

public static IBlockDescriptor[] GetDescriptors() =>
StaticDescriptors.Values.ToArray();
Expand All @@ -32,12 +34,16 @@ public static IBlockDescriptor[] GetDescriptors() =>

public static IBlockDescriptor? GetDescriptor(Type type) =>
StaticDescriptors.Values.FirstOrDefault(d => d.Type == type);

public static IContentBlockMetadata GetMetadata(IBlockDescriptor descriptor) =>
StaticMetadata.Values.First(d => d.BlockType == descriptor.Type);
}

public class Blockly<TBlockDescriptor> : Blockly, IBlockly<TBlockDescriptor>
where TBlockDescriptor : IBlockDescriptor
{
private readonly List<TBlockDescriptor> blockDescriptors;
private readonly List<IContentBlockMetadata> blocksMetadata = new();
private readonly ILogger<Blockly<TBlockDescriptor>> logger;

public Blockly(IEnumerable<TBlockDescriptor> blockDescriptors, ILogger<Blockly<TBlockDescriptor>> logger)
Expand Down Expand Up @@ -88,6 +94,15 @@ public Task InitAsync()
foreach (var blockDescriptor in blockDescriptors.Cast<IBlockDescriptor>())
{
StaticDescriptors.TryAdd(blockDescriptor.Type, blockDescriptor);
var metadataAttribute = blockDescriptor.Type.GetCustomAttribute<ContentBlockMetadataAttribute>() ??
new ContentBlockMetadataAttribute();
blocksMetadata.Add(new ContentBlockMetadata(blockDescriptor.Type, metadataAttribute.Priority,
metadataAttribute.MaxCount));
}

foreach (var blockMetadata in blocksMetadata)
{
StaticMetadata.TryAdd(blockMetadata.BlockType, blockMetadata);
}

return Task.CompletedTask;
Expand Down
8 changes: 4 additions & 4 deletions src/Sitko.Blockly/BlocklyFormOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ public class BlocklyFormOptions

public IStorage? Storage { get; set; }

public int? MaxBlockCount(IBlockDescriptor descriptor)
public int MaxBlockCount(IBlockDescriptor descriptor)
{
if (maxBlockCounts.ContainsKey(descriptor.Type))
{
return maxBlockCounts[descriptor.Type];
}

return null;
return Blockly.GetMetadata(descriptor).MaxCount;
}

public int? BlockPriority(IBlockDescriptor descriptor)
public int BlockPriority(IBlockDescriptor descriptor)
{
if (blockPriorities.ContainsKey(descriptor.Type))
{
return blockPriorities[descriptor.Type];
}

return null;
return Blockly.GetMetadata(descriptor).Priority;
}

public BlocklyFormOptions ClearBlockMaxCounts()
Expand Down
3 changes: 1 addition & 2 deletions src/Sitko.Blockly/Blocks/CutBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sitko.Blockly.Blocks
{
[ContentBlockMetadata(2, 1)]
public record CutBlock : ContentBlock

{
Expand All @@ -15,7 +16,5 @@ public record CutBlockDescriptor : BlockDescriptor<CutBlock>
public CutBlockDescriptor(ILocalizationProvider<CutBlock> localizationProvider) : base(localizationProvider)
{
}
public override int Priority => 2;
public override int MaxCount => 1;
}
}
3 changes: 1 addition & 2 deletions src/Sitko.Blockly/Blocks/FilesBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Sitko.Blockly.Blocks
{
[ContentBlockMetadata(5)]
public record FilesBlock : ContentBlock
{
public override string ToString() => $"Files: {string.Join(", ", Files.Select(p => p.FileName))}";
Expand All @@ -14,8 +15,6 @@ public record FilesBlock : ContentBlock

public record FilesBlockDescriptor : BlockDescriptor<FilesBlock>
{
public override int Priority => 5;

public FilesBlockDescriptor(ILocalizationProvider<FilesBlock> localizationProvider) : base(localizationProvider)
{
}
Expand Down
3 changes: 1 addition & 2 deletions src/Sitko.Blockly/Blocks/GalleryBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Sitko.Blockly.Blocks
{
[ContentBlockMetadata(3)]
public record GalleryBlock : ContentBlock
{
public override string ToString() => $"Gallery: {string.Join(", ", Pictures.Select(p => p.FileName))}";
Expand All @@ -14,8 +15,6 @@ public record GalleryBlock : ContentBlock

public record GalleryBlockDescriptor : BlockDescriptor<GalleryBlock>
{
public override int Priority => 3;

public GalleryBlockDescriptor(ILocalizationProvider<GalleryBlock> localizationProvider) : base(
localizationProvider)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Sitko.Blockly/Blocks/IframeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sitko.Blockly.Blocks
{
[ContentBlockMetadata(9)]
public record IframeBlock : ContentBlock
{
public override string ToString() => $"Frame: {Src}";
Expand All @@ -11,8 +12,6 @@ public record IframeBlock : ContentBlock

public record IframeBlockDescriptor : BlockDescriptor<IframeBlock>
{
public override int Priority => 9;

public IframeBlockDescriptor(ILocalizationProvider<IframeBlock> localizationProvider) : base(
localizationProvider)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Sitko.Blockly/Blocks/QuoteBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Sitko.Blockly.Blocks
{
[ContentBlockMetadata(4)]
public record QuoteBlock : ContentBlock
{
public override string ToString() => $"{Author}: {Text} ({Link})";
Expand All @@ -15,8 +16,6 @@ public record QuoteBlock : ContentBlock

public record QuoteBlockDescriptor : BlockDescriptor<QuoteBlock>
{
public override int Priority => 4;

public QuoteBlockDescriptor(ILocalizationProvider<QuoteBlock> localizationProvider) : base(localizationProvider)
{
}
Expand Down
3 changes: 1 addition & 2 deletions src/Sitko.Blockly/Blocks/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sitko.Blockly.Blocks
{
[ContentBlockMetadata(1)]
public record TextBlock : ContentBlock
{
public override string ToString() => Text;
Expand All @@ -11,8 +12,6 @@ public record TextBlock : ContentBlock

public record TextBlockDescriptor : BlockDescriptor<TextBlock>
{
public override int Priority => 1;

public TextBlockDescriptor(ILocalizationProvider<TextBlock> localizationProvider) : base(localizationProvider)
{
}
Expand Down
3 changes: 1 addition & 2 deletions src/Sitko.Blockly/Blocks/TwitchBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sitko.Blockly.Blocks
{
[ContentBlockMetadata(8)]
public record TwitchBlock : UrlContentBlock
{
protected override bool IsEmpty => string.IsNullOrEmpty(VideoId) && string.IsNullOrEmpty(ChannelId) &&
Expand Down Expand Up @@ -124,8 +125,6 @@ protected override void ParseUrl(string? url)

public record TwitchBlockDescriptor : BlockDescriptor<TwitchBlock>
{
public override int Priority => 8;

public TwitchBlockDescriptor(ILocalizationProvider<TwitchBlock> localizationProvider) : base(
localizationProvider)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Sitko.Blockly/Blocks/TwitterBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sitko.Blockly.Blocks
{
[ContentBlockMetadata(7)]
public record TwitterBlock : UrlContentBlock
{
protected override bool IsEmpty => string.IsNullOrEmpty(TweetId);
Expand Down Expand Up @@ -41,8 +42,6 @@ protected override void ParseUrl(string? url)

public record TwitterBlockDescriptor : BlockDescriptor<TwitterBlock>
{
public override int Priority => 7;

public TwitterBlockDescriptor(ILocalizationProvider<TwitterBlock> localizationProvider) : base(
localizationProvider)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Sitko.Blockly/Blocks/YoutubeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sitko.Blockly.Blocks
{
[ContentBlockMetadata(6)]
public record YoutubeBlock : UrlContentBlock
{
protected override bool IsEmpty => string.IsNullOrEmpty(YoutubeId);
Expand Down Expand Up @@ -32,8 +33,6 @@ protected override void ParseUrl(string? url)

public record YoutubeBlockDescriptor : BlockDescriptor<YoutubeBlock>
{
public override int Priority => 6;

public YoutubeBlockDescriptor(ILocalizationProvider<YoutubeBlock> localizationProvider) : base(
localizationProvider)
{
Expand Down
38 changes: 38 additions & 0 deletions src/Sitko.Blockly/ContentBlockMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Sitko.Blockly
{
using System;

public interface IContentBlockMetadata
{
int Priority { get; }
int MaxCount { get; }
Type BlockType { get; }
}

public class ContentBlockMetadata : IContentBlockMetadata
{
public int Priority { get; }
public int MaxCount { get; }

public Type BlockType { get; }

public ContentBlockMetadata(Type blockType, int priority = int.MaxValue, int maxCount = 0)
{
Priority = priority;
MaxCount = maxCount;
BlockType = blockType;
}
}

public class ContentBlockMetadataAttribute : Attribute
{
public int Priority { get; }
public int MaxCount { get; }

public ContentBlockMetadataAttribute(int priority = int.MaxValue, int maxCount = 0)
{
Priority = priority;
MaxCount = maxCount;
}
}
}

0 comments on commit 0ff5397

Please sign in to comment.