Skip to content

Commit

Permalink
feat(blocks): add form options to disable specific block type deletio…
Browse files Browse the repository at this point in the history
…n and moving
  • Loading branch information
SonicGD committed Sep 15, 2021
1 parent 2e75260 commit a6a41ca
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Sitko.Blockly.AntDesign/Forms/AntBlocklyForm.razor
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
</AntDesign.Button>
</span>
<span title="@LocalizationProvider["Delete block"]">
<AntDesign.Popconfirm Placement="@Placement.Left" Title="@LocalizationProvider["Delete block?"]"
<AntDesign.Popconfirm Disabled="!CanDeleteBlock(block)" Placement="@Placement.Left" Title="@LocalizationProvider["Delete block?"]"
OnConfirm="@(() => DeleteBlock(block))"
OkText="@LocalizationProvider["Delete block"]"
CancelText="@LocalizationProvider["Cancel"]">
<AntDesign.Button Size="Default">
<AntDesign.Button Disabled="!CanDeleteBlock(block)" Size="Default">
<Icon Type="delete"></Icon>
</AntDesign.Button>
</AntDesign.Popconfirm>
Expand Down
8 changes: 6 additions & 2 deletions src/Sitko.Blockly.Blazor/Forms/BlocklyForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
53 changes: 52 additions & 1 deletion src/Sitko.Blockly/BlocklyFormOptions.cs
Original file line number Diff line number Diff line change
@@ -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<Type, int> maxBlockCounts = new();
private readonly Dictionary<Type, int> blockPriorities = new();
public int MaxBlocks { get; set; }
private readonly HashSet<Type> allowedBlocks = new();
private readonly HashSet<Type> disabledMoveBlocks = new();
private readonly HashSet<Type> disabledDeleteBlocks = new();
public IEnumerable<Type> AllowedBlocks => allowedBlocks;

public IStorage? Storage { get; set; }

public int MaxBlockCount(IBlockDescriptor descriptor)
Expand Down Expand Up @@ -86,5 +89,53 @@ public BlocklyFormOptions AddAllowedBlock<TBlock>() where TBlock : ContentBlock
allowedBlocks.Add(typeof(TBlock));
return this;
}

public BlocklyFormOptions DisableBlocksMove(IEnumerable<Type> blocks)
{
foreach (var disabledMoveBlock in blocks)
{
DisableBlockMove(disabledMoveBlock);
}

return this;
}

public BlocklyFormOptions DisableBlockMove(Type allowedBlock)
{
disabledMoveBlocks.Add(allowedBlock);
return this;
}

public BlocklyFormOptions DisableBlockMove<TBlock>() where TBlock : ContentBlock
{
disabledMoveBlocks.Add(typeof(TBlock));
return this;
}

public BlocklyFormOptions DisableBlocksDelete(IEnumerable<Type> blocks)
{
foreach (var disabledDeleteBlock in blocks)
{
DisableBlockDelete(disabledDeleteBlock);
}

return this;
}

public BlocklyFormOptions DisableBlockDelete(Type allowedBlock)
{
disabledDeleteBlocks.Add(allowedBlock);
return this;
}

public BlocklyFormOptions DisableBlockDelete<TBlock>() 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());
}
}

0 comments on commit a6a41ca

Please sign in to comment.