New to Telerik UI for BlazorStart a free 30-day trial

Editor Built-in Tools

This article explains which are the built-in tools and commands of the Editor, how to invoke them programmatically and what functionality they offer.

How to Use this Article

This article describes the Editor tools and commands.

An Editor tool is the visible interface for a given action. For example, the button that bolds text is a tool. Built-in tools can render as buttons, color pickers or dropdown lists. The Editor also supports custom tools with custom rendering.

An Editor command is the action, which modifies the HTML value in some way. Each built-in Editor tool has an associated command. Custom tools can execute business logic or invoke built-in commands.

The information about the Editor tools and commands is organized in tables below. Here is what the table headers mean:

Here is a simple example that demonstrates how to use class names, command names and ExecuteAsync arguments.

Use tool class names and command names with the Blazor Editor

@using Telerik.Blazor.Components.Editor
@* Avoid ambiguous reference with SVG icons *@
@using EditorNS = Telerik.Blazor.Components.Editor;

<TelerikButton OnClick="@InsertParagraph">Insert Paragraph in the Editor</TelerikButton>

<TelerikEditor @ref="EditorRef"
               Tools="@EditorTools"
               @bind-Value="@EditorValue">
</TelerikEditor>

@code {
    private TelerikEditor EditorRef { get; set; }

    private string EditorValue { get; set; } = @"<p>foo</p><p>bar</p>";

    // "Bold", "Italic" and "Underline" are class names
    private List<IEditorTool> EditorTools { get; set; } = new List<IEditorTool>() {
        new EditorNS.Bold(),
        new EditorNS.Italic(),
        new EditorNS.Underline()
    };

    private async Task InsertParagraph()
    {
        // "insertHtml" is a command name
        // HtmlCommandArgs is the ExecuteAsync argument
        await EditorRef.ExecuteAsync(new HtmlCommandArgs("insertHtml", $"<p>baz {DateTime.Now.Millisecond}</p>"));
    }
}

Built-in Tools and Commands

When choosing which Editor tools to render, it is possible to create a tools collection from scratch or append additional tools to a preset collection.

Inline Tools

The inline tools add or work with inline HTML elements. For example, such elements are <a>, <img>, <strong> and <em>.

Table 1: Inline Tools of the Editor

Class NameCommand NameTool TypeDescriptionArgument for ExecuteAsync
BoldboldbuttonApplies bold stylenew ToolCommandArgs(string commandName)
BackgroundColorbackColorcolorChanges the background colornew FormatCommandArgs(string commandName, string Value)
ForeColorforeColorcolorChanges the text colornew FormatCommandArgs(string commandName, string Value)
CreateLinkcreateLinkbuttonCreates a hyperlinknew LinkCommandArgs(string href, string text, string target, string title, null)
FontFamilyfontFamilydropdownSets the font typefacenew FormatCommandArgs(string commandName, string Value)
FontSizefontSizedropdownSets the text font sizenew FormatCommandArgs(string commandName, string Value)
ItalicitalicbuttonApplies italic stylenew ToolCommandArgs(string commandName)
StrikethroughstrikethroughbuttonApplies strikethrough formattingnew ToolCommandArgs(string commandName)
SubScriptsubscriptbuttonMakes the selected text subscriptnew ToolCommandArgs(string commandName)
SuperScriptsuperscriptbuttonMakes the selected text superscriptnew ToolCommandArgs(string commandName)
UnderlineunderlinebuttonApplies underline stylenew ToolCommandArgs(string commandName)
UnlinkunlinkbuttonRemoves a hyperlinknew ToolCommandArgs(string commandName)

Color Tool Customization

The ForeColor and BackgroundColor tools expose a few customization properties:

PropertyType and Default ValueDescription
ColorsIEnumerable<string>.The list of available colors to set from the Color tool. You can provide a member of ColorPalettePresets, or a custom list of RGB(A) or HEX colors in different supported formats.
TitlestringThe tooltip content that shows on tool mouse over.
ValueFormatColorFormat enum
(Rgb)
The format, which the Color tool will set in the generated HTML markup. Use Rgb or Hex.

Customizing the Editor Color Tools

@using Telerik.Blazor.Components.Editor

<TelerikEditor Tools="@EditorTools"
               @bind-Value="@EditorValue">
</TelerikEditor>

@code {
    private string EditorValue { get; set; }

    private List<IEditorTool> EditorTools { get; set; } = new List<IEditorTool>()
    {
        new ForeColor()
        {
            Title = "Text Color",
            Colors = new List<string> { "#f00", "#ff9900", "rgb(0, 128, 0)", "rgba(0, 0, 255, .8)" },
            ValueFormat = ColorFormat.Hex
        },
        new BackgroundColor()
        {
            Title = "Background Color",
            Colors = ColorPalettePresets.Basic,
            ValueFormat = ColorFormat.Hex
        }
    };
}

Font Tool Customization

The FontFamily and FontSize tools have a Data property that accepts a List<EditorDropDownListItem>. Use it to customize the available options in these dropdowns. You can also change the dropdown label via DefaultText.

@using Telerik.Blazor.Components.Editor
@* Avoid ambiguous reference with SVG icons *@
@using EditorNS = Telerik.Blazor.Components.Editor;

<TelerikEditor @bind-Value="@EditorValue"
               Tools="@EditorTools">
</TelerikEditor>

@code {
    private string EditorValue { get; set; }

    private List<IEditorTool> EditorTools { get; set; } = new List<IEditorTool>()
    {
        new EditorNS.FontFamily()
        {
            DefaultText = "Font Family",
            Data = new List<EditorDropDownListItem>()
            {
                new EditorDropDownListItem("Georgia", "georgia"),
                new EditorDropDownListItem("Lucida Console", "'lucida console'")
            }
        },
        new EditorNS.FontSize()
        {
            DefaultText = "Text Size",
            Data = new List<EditorDropDownListItem>()
            {
                new EditorDropDownListItem("Small", "12px"),
                new EditorDropDownListItem("Medium", "16px"),
                new EditorDropDownListItem("Large", "24px")
            }
        }
    };
}

Block Tools

The block tools add or work with block HTML elements, like <h1>, <h2>, <p> and <ul>.

All tools in the table below are buttons, except Format, which is a dropdown.

Table 2: Block Tools of the Editor

Class NameCommand NameDescriptionArgument for ExecuteAsync
AlignCenteralignCenterAligns the selected paragraph to the centernew ToolCommandArgs(string commandName)
AlignJustifyalignJustifyJustifies the selected paragraphnew ToolCommandArgs(string commandName)
AlignLeftalignLeftAligns the selected paragraph to the leftnew ToolCommandArgs(string commandName)
AlignRightalignRightAligns the selected paragraph to the rightnew ToolCommandArgs(string commandName)
FormatformatApplies standard formatting to the selected text like Heading 1, Paragraph and so on. Unlike the other tools in this table, this one is a dropdown.new FormatCommandArgs(string commandName, string Value)
IndentindentAdds indent to the selected textnew ToolCommandArgs(string commandName)
InsertImageinsertImageInserts image from a desired URLnew ImageCommandArgs(string src, string alt, string width, string height)
OrderedListinsertOrderedListCreates a list of numeric bulletsnew ToolCommandArgs(string commandName)
OutdentoutdentRemoves indent from the selected textnew ToolCommandArgs(string commandName)
RedoredoRepeats the last undone actionnew ToolCommandArgs(string commandName)
ViewHtmlsetHtmlShows and updates the raw HTML of the Editor contentnew HtmlCommandArgs(string commandName, string value)
UndoundoReverts the last actionnew ToolCommandArgs(string commandName)
UnorderedListinsertUnorderedListCreates a list of bulletsnew ToolCommandArgs(string commandName)

Format Tool Customization

The Format tool exposes a Data property that accepts a List<EditorDropDownListItem>. Use it to reduce or reorder the items in the dropdown list.

@using Telerik.Blazor.Components.Editor

<TelerikEditor @bind-Value="@EditorValue"
               Tools="@EditorTools">
</TelerikEditor>

@code {
    private string EditorValue { get; set; }

    private List<IEditorTool> EditorTools { get; set; } = new List<IEditorTool>()
    {
        new Format()
        {
            Data = new List<EditorDropDownListItem>()
            {
                new EditorDropDownListItem("Paragraph", "p"),
                new EditorDropDownListItem("Heading 1", "h1")
            }
        }
    };
}

Table Tools

The table tools create and manipulate HTML <table> elements. These tools can add or remove columns and rows, and merge or split cells.

All tools in the table below are buttons.

Table 3: Editor Table Tools

Class NameCommand NameDescriptionArgument for ExecuteAsync
InsertTableinsertTableInserts a table with the desired number of columns and rowsnew TableCommandArgs(int rows, int cols)
DeleteTabledeleteTableDeletes the selected HTML tablenew ToolCommandArgs(string commandName)
AddColumnBeforeaddColumnBeforeInserts a column before the selected onenew ToolCommandArgs(string commandName)
AddColumnAfteraddColumnAfterInserts a column after the selected onenew ToolCommandArgs(string commandName)
AddRowBeforeaddRowBeforeInserts a row before the selected onenew ToolCommandArgs(string commandName)
AddRowAfteraddRowAfterInserts a row after the selected onenew ToolCommandArgs(string commandName)
DeleteRowdeleteRowDeletes the selected rownew ToolCommandArgs(string commandName)
DeleteColumndeleteColumnDeletes the selected columnnew ToolCommandArgs(string commandName)
MergeCellsmergeCellsMerges the selected cellsnew ToolCommandArgs(string commandName)
SplitCellsplitCellSplits already merged cellsnew ToolCommandArgs(string commandName)

Commands Without Built-in Tools

Some Editor commands have no built-in tools. These commands can only be invoked programmatically.

Table 4: Editor Commands Without Tools

Command NameDescriptionArguments for ExecuteAsync
cleanFormattingCleans the inline formatting of a selected textnew ToolCommandArgs(string commandName)
insertHtmlInserts HTML at the cursor position. To insert multiple nodes, wrap them in a single element.
By default this is a block command that will wrap passed inline content in a p or div, depending on the context. To insert inline content, set the third argument to true.
Note there are separate commands for inserting links and images.
new HtmlCommandArgs(string commandName, string value, bool inline)

Programmatic Execution

You can invoke the built-in Editor commands from outside the component or from custom tools.

In order to do so, you need to use the Editor reference and to call the ExecuteAsync method.

Use the reference tables above to find the command name and its arguments for the command you want to invoke.

Execute commands from buttons outside the Editor

@* Click on the buttons to execute the Editor tools *@

@using Telerik.Blazor.Components.Editor

<TelerikButton OnClick="@InsertHr">Insert hr</TelerikButton>
<TelerikButton OnClick="@BoldText">Create bold text</TelerikButton>
<TelerikButton OnClick="@InsertTable">Insert Table</TelerikButton>
<TelerikButton OnClick="@InsertImage">Insert Image</TelerikButton>
<TelerikButton OnClick="@InsertInlineText">Insert Inline Text</TelerikButton>

<TelerikEditor @ref="@TheEditor" @bind-Value="@TheContent"></TelerikEditor>

@code{
    TelerikEditor TheEditor { get; set; }

    string TheContent { get; set; } = "<p>Lorem ipsum.</p><p>Dolor sit amet.</p>";

    async Task InsertHr()
    {
        await TheEditor.ExecuteAsync(new HtmlCommandArgs("insertHtml", "<hr />"));
    }

    async Task InsertInlineText()
    {
        await TheEditor.ExecuteAsync(new HtmlCommandArgs("insertHtml", "John Doe", true));
    }

    async Task InsertImage()
    {
        await TheEditor.ExecuteAsync(new ImageCommandArgs("https://demos.telerik.com/blazor-ui/images/articles/1-220x140.png", "the alt text", "220px", "140px"));
    }

    async Task BoldText()
    {
        await TheEditor.ExecuteAsync(new ToolCommandArgs("bold"));
    }

    async Task InsertTable()
    {
        await TheEditor.ExecuteAsync(new TableCommandArgs(4, 4));
    }
}

See Also