using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
namespace Radzen.Blazor
{
///
/// A tool which changes the style of a the selected text by making it a heading or paragraph.
///
///
///
/// <RadzenHtmlEditor @bind-Value=@html>
/// <RadzenHtmlEditorFormatBlock />
/// </RadzenHtmlEdito>
/// @code {
/// string html = "@lt;strong>Hello</strong> world!";
/// }
///
///
public partial class RadzenHtmlEditorFormatBlock : ComponentBase, IDisposable
{
///
/// The RadzenHtmlEditor component which this tool is part of.
///
[CascadingParameter]
public RadzenHtmlEditor? Editor { get; set; }
///
/// Specifies the placeholder displayed to the user. Set to "Format block" by default.
///
[Parameter]
public string? Placeholder { get; set; } = "Format block";
///
/// Specifies the title (tooltip) displayed when the user hovers the tool. Set to "Text style" by default.
///
[Parameter]
public string? Title { get; set; } = "Text style";
///
/// Specifies the text displayed for the normal text example. Set to "Normal" by default.
///
[Parameter]
public string? NormalText { get; set; } = "Normal";
///
/// Specifies the text displayed for the h1 example. Set to "Heading 1" by default.
///
[Parameter]
public string? Heading1Text { get; set; } = "Heading 1";
///
/// Specifies the text displayed for the h2 example. Set to "Heading 2" by default.
///
[Parameter]
public string? Heading2Text { get; set; } = "Heading 2";
///
/// Specifies the text displayed for the h3 example. Set to "Heading 3" by default.
///
[Parameter]
public string? Heading3Text { get; set; } = "Heading 3";
///
/// Specifies the text displayed for the h4 example. Set to "Heading 4" by default.
///
[Parameter]
public string? Heading4Text { get; set; } = "Heading 4";
///
/// Specifies the text displayed for the h5 example. Set to "Heading 5" by default.
///
[Parameter]
public string? Heading5Text { get; set; } = "Heading 5";
///
/// Specifies the text displayed for the h6 example. Set to "Heading 6" by default.
///
[Parameter]
public string? Heading6Text { get; set; } = "Heading 6";
async Task OnChange(string value)
{
if (Editor != null)
{
await Editor.ExecuteCommandAsync("formatBlock", value);
}
}
///
override protected void OnInitialized()
{
Editor?.RegisterShortcut("Alt+Shift+1", () => OnChange("h1"));
Editor?.RegisterShortcut("Alt+Shift+2", () => OnChange("h2"));
Editor?.RegisterShortcut("Alt+Shift+3", () => OnChange("h3"));
Editor?.RegisterShortcut("Alt+Shift+4", () => OnChange("h4"));
Editor?.RegisterShortcut("Alt+Shift+5", () => OnChange("h5"));
Editor?.RegisterShortcut("Alt+Shift+6", () => OnChange("h6"));
Editor?.RegisterShortcut("Alt+Shift+7", () => OnChange("p"));
}
///
/// IDisposable implementation.
///
public void Dispose()
{
Editor?.UnregisterShortcut("Alt+Shift+1");
Editor?.UnregisterShortcut("Alt+Shift+2");
Editor?.UnregisterShortcut("Alt+Shift+3");
Editor?.UnregisterShortcut("Alt+Shift+4");
Editor?.UnregisterShortcut("Alt+Shift+5");
Editor?.UnregisterShortcut("Alt+Shift+6");
Editor?.UnregisterShortcut("Alt+Shift+7");
GC.SuppressFinalize(this);
}
}
}