From a6a41cab306296092365a7cc46506a2511131083 Mon Sep 17 00:00:00 2001 From: George Drak Date: Wed, 15 Sep 2021 12:23:49 +0500 Subject: [PATCH] feat(blocks): add form options to disable specific block type deletion and moving --- .../Forms/AntBlocklyForm.razor | 4 +- src/Sitko.Blockly.Blazor/Forms/BlocklyForm.cs | 8 ++- src/Sitko.Blockly/BlocklyFormOptions.cs | 53 ++++++++++++++++++- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/src/Sitko.Blockly.AntDesign/Forms/AntBlocklyForm.razor b/src/Sitko.Blockly.AntDesign/Forms/AntBlocklyForm.razor index 8cc72f8..ac83111 100644 --- a/src/Sitko.Blockly.AntDesign/Forms/AntBlocklyForm.razor +++ b/src/Sitko.Blockly.AntDesign/Forms/AntBlocklyForm.razor @@ -31,11 +31,11 @@ - - + diff --git a/src/Sitko.Blockly.Blazor/Forms/BlocklyForm.cs b/src/Sitko.Blockly.Blazor/Forms/BlocklyForm.cs index 4db831c..df03a4f 100644 --- a/src/Sitko.Blockly.Blazor/Forms/BlocklyForm.cs +++ b/src/Sitko.Blockly.Blazor/Forms/BlocklyForm.cs @@ -160,9 +160,13 @@ private void ValidateBlock(ContentBlock block) } } - protected bool CanMoveBlockUp(ContentBlock block) => block.Enabled && OrderedBlocks.CanMoveUp(block); + protected bool CanMoveBlockUp(ContentBlock block) => + block.Enabled && FormOptions.CanMoveBlock(block) && OrderedBlocks.CanMoveUp(block); - protected bool CanMoveBlockDown(ContentBlock block) => block.Enabled && OrderedBlocks.CanMoveDown(block); + protected bool CanMoveBlockDown(ContentBlock block) => + block.Enabled && FormOptions.CanMoveBlock(block) && OrderedBlocks.CanMoveDown(block); + + protected bool CanDeleteBlock(ContentBlock block) => block.Enabled && FormOptions.CanDeleteBlock(block); protected Task MoveBlockUpAsync(ContentBlock block) { diff --git a/src/Sitko.Blockly/BlocklyFormOptions.cs b/src/Sitko.Blockly/BlocklyFormOptions.cs index 78fbafe..03aa19a 100644 --- a/src/Sitko.Blockly/BlocklyFormOptions.cs +++ b/src/Sitko.Blockly/BlocklyFormOptions.cs @@ -1,17 +1,20 @@ using System; using System.Collections.Generic; using Sitko.Core.Storage; +using JetBrains.Annotations; namespace Sitko.Blockly { + [PublicAPI] public class BlocklyFormOptions { private readonly Dictionary maxBlockCounts = new(); private readonly Dictionary blockPriorities = new(); public int MaxBlocks { get; set; } private readonly HashSet allowedBlocks = new(); + private readonly HashSet disabledMoveBlocks = new(); + private readonly HashSet disabledDeleteBlocks = new(); public IEnumerable AllowedBlocks => allowedBlocks; - public IStorage? Storage { get; set; } public int MaxBlockCount(IBlockDescriptor descriptor) @@ -86,5 +89,53 @@ public BlocklyFormOptions AddAllowedBlock() where TBlock : ContentBlock allowedBlocks.Add(typeof(TBlock)); return this; } + + public BlocklyFormOptions DisableBlocksMove(IEnumerable blocks) + { + foreach (var disabledMoveBlock in blocks) + { + DisableBlockMove(disabledMoveBlock); + } + + return this; + } + + public BlocklyFormOptions DisableBlockMove(Type allowedBlock) + { + disabledMoveBlocks.Add(allowedBlock); + return this; + } + + public BlocklyFormOptions DisableBlockMove() where TBlock : ContentBlock + { + disabledMoveBlocks.Add(typeof(TBlock)); + return this; + } + + public BlocklyFormOptions DisableBlocksDelete(IEnumerable blocks) + { + foreach (var disabledDeleteBlock in blocks) + { + DisableBlockDelete(disabledDeleteBlock); + } + + return this; + } + + public BlocklyFormOptions DisableBlockDelete(Type allowedBlock) + { + disabledDeleteBlocks.Add(allowedBlock); + return this; + } + + public BlocklyFormOptions DisableBlockDelete() where TBlock : ContentBlock + { + disabledDeleteBlocks.Add(typeof(TBlock)); + return this; + } + + public bool CanDeleteBlock(ContentBlock block) => !disabledDeleteBlocks.Contains(block.GetType()); + + public bool CanMoveBlock(ContentBlock block) => !disabledMoveBlocks.Contains(block.GetType()); } }