Files
radzen-blazor/Radzen.Blazor/Spreadsheet/HyperlinkCommand.cs
Atanas Korchev ce0eae2037 Add RadzenSpreadsheet feature toggles, async command pipeline and custom ribbon
ReadOnly, ShowToolbar, ShowFormulaBar, ShowSheetTabs, and 15 Allow* parameters
gate workbook mutations and grey out the matching tools. Each ICommand declares
its SpreadsheetFeature so ExecuteAsync routes through one chokepoint.

Execute is now async (Task<bool>) so CommandExecuting handlers can await before
calling PreventDefault to veto a command. Tool bindings move from setter-style
dispatch to Value=/ValueChanged= async methods.

ChildContent replaces the built-in ribbon; the host cascades the active
Worksheet so tools in custom content don't need to track sheet-tab changes.
TableDesignTab is a new public component for the contextual tab.

Tests: 76 new (parameter rendering, ExecuteAsync gating per Allow* flag,
CommandExecuting + PreventDefault, ICommand.Feature mapping, ChildContent).
Also fixes two pre-existing SheetViewTests that hardcoded the old 24px row
default after it changed to 22px in 2e79dd7d.
2026-06-15 18:45:14 +03:00

58 lines
1.6 KiB
C#

using Radzen.Documents.Spreadsheet;
namespace Radzen.Blazor.Spreadsheet;
#nullable enable
/// <summary>
/// Command to set or remove a hyperlink on a cell.
/// </summary>
public class HyperlinkCommand(Worksheet sheet, CellRef address, Hyperlink? hyperlink) : ICommand, IProtectedCommand
{
/// <inheritdoc/>
public SheetAction RequiredAction => SheetAction.InsertHyperlinks;
/// <inheritdoc/>
public SpreadsheetFeature? Feature => SpreadsheetFeature.Hyperlinks;
private readonly Worksheet sheet = sheet;
private readonly CellRef address = address;
private readonly Hyperlink? hyperlink = hyperlink;
private Hyperlink? previousHyperlink;
private string? previousDisplayText;
/// <inheritdoc/>
public bool Execute()
{
var cell = sheet.Cells[address.Row, address.Column];
previousHyperlink = cell.Hyperlink?.Clone();
previousDisplayText = cell.Value?.ToString();
cell.Hyperlink = hyperlink?.Clone();
if (hyperlink is not null && !string.IsNullOrEmpty(hyperlink.Text))
{
cell.Value = hyperlink.Text;
}
else if (hyperlink is not null && (cell.Value is null || string.IsNullOrEmpty(cell.Value.ToString())))
{
cell.Value = hyperlink.Url;
}
return true;
}
/// <inheritdoc/>
public void Unexecute()
{
if (sheet.Cells.TryGet(address.Row, address.Column, out var cell))
{
cell.Hyperlink = previousHyperlink?.Clone();
if (previousDisplayText is not null)
{
cell.Value = previousDisplayText;
}
}
}
}