Skip to main content
All docs
V24.1

DxHtmlEditor Class

A WYSIWYG text editor.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v24.1.dll

NuGet Package: DevExpress.Blazor

Declaration

public class DxHtmlEditor :
    ClientComponentJSInterop,
    IModelProvider<ClientComponentCollectionModel<HtmlEditorMentionModel>>,
    IModelProvider<HtmlEditorVariablesModel>

Remarks

DevExpress Blazor HTML Editor (<DxHtmlEditor>) is a WYSIWYG (what you see is what you get) text editor that allows users to format text and add graphics. You can export the editor’s resulting markup to HTML or Markdown.

Blazor HTML Editor

Run Demo: HTML Editor - Overview

Add an HTML Editor to a Project

Follow the steps below to add an HTML Editor component to an application:

  1. Use a DevExpress Project Template to create a new Blazor Server or Blazor WebAssembly application. If you use a Microsoft project template or already have a Blazor project, configure your project to incorporate DevExpress Blazor components.
  2. Register the DevExpress.Blazor.Office namespace to access and modify toolbar settings.
  3. Add the following markup to a .razor file: <DxHtmlEditor></DxHtmlEditor>.
  4. Manage the editor markup.
  5. Optional. Configure other options (see the sections below).
Show Nested Component Structure

HTML Editor Markup

Use the Markup property to specify the HTML Editor’s markup.

<DxHtmlEditor Markup="Document content"
              Height="200px"
              Width="100%" />

You can also use the @bind attribute to bind the Markup property to a data field. Refer to the following topic for details: Two-Way Data Binding.

<DxHtmlEditor @bind-Markup="@markup"
              Height="200px"
              Width="100%" />

@code {
    string markup { get; set; } = "Document content";
}

When the Markup property is bound to a field, the editor applies the BindMarkupMode property. This property specifies how the editor updates its markup. The default mode is OnLostFocus.

When the Markup property value changes, the editor raises the MarkupChanged event.

Markup Type

The HTML Editor stores its markup in the Markup property in HTML format.

Html Editor - HTML Markup Type

In Html, the editor can store the following formatting attributes:

  • Bold, italic, strikethrough, underlined, subscript, and superscript text
  • Font, font size, and text color
  • Headings
  • Text alignment
  • Bullet and numbered lists
  • Code blocks and quotes
  • Hyperlinks, images, and tables

Use the MarkupType property to change the markup language to Markdown. Note that in Markdown, the HTML Editor can store a limited number of formatting attributes:

  • Bold and italic text
  • Headings
  • Bullet and numbered lists
  • Quotes
  • Hyperlinks, images, and tables

When the MarkupType property is set to Markdown, the editor disables toolbar commands that correspond to unsupported formats.

Html Editor - Markdown Markup Type

<DxHtmlEditor @bind-Markup="@Markup"
              MarkupType="HtmlEditorMarkupType.Markdown"
              Height="200px"
              Width="100%" />

<p><b>The result markup:</b> @Markup</p>

@code {
    string Markup { get; set; } = "";
}

Input Validation

<DxHtmlEditor> allows you to validate user input. To specify validation rules, use the IsValid property.

When a user enters an invalid value, the focused editor displays a validation message at the specified position. You can also use the ShowValidationMessageOnFocus property to specify whether the editor hides a validation message after the editor’s focus is lost.

The following code snippet validates user input in the MarkupChanged event handler and configures validation settings as follows:

  • Displays a validation message if the editor’s markup is empty.
  • Sets the validation message text.
  • Positions the validation message at the right editor edge.

HTML Editor - Right Validation Message Position

<DxHtmlEditor Markup="@markup"
              IsValid="@isValid"
              ValidationMessage="Empty markup."
              ValidationMessagePosition="HtmlEditorValidationMessagePosition.Right"
              MarkupChanged="@OnMarkupChanged"
              Height="100px"
              Width="80%" />

@code {
    bool isValid;
    string markup { get; set; } = "";
    void OnMarkupChanged(string newValue) {
        markup = newValue;
        isValid = !string.IsNullOrEmpty(newValue);
    }
}

Mentions

<DxHtmlEditor> supports mentions that allow a user to reference other users in text or conversation threads. When a user types a predefined marker, the editor displays a drop-down list of available names. The component allows you to use multiple mention lists. To identify a mention list, use a unique marker.

Follow the steps below to create and configure a mention list:

  1. Add a DxHtmlEditorMention object to the DxHtmlEditorMentions collection.
  2. Use the DxHtmlEditorMention.Data property to specify a data source for mentions.
  3. Specify the DisplayFieldName property to obtain display values for mentions from data source fields.
  4. Assign a unique marker to the Marker property.
  5. Specify the SearchFieldNames property to enable search operations for mentions.
  6. Optional. Use SearchMinLength and SearchDelay properties to configure search settings.

The following code snippet implements mentions to emulate the functionality common to many collaboration tools:

Html Editor - Mentions

<DxHtmlEditor Markup="@Markup" Height="200px">
    <DxHtmlEditorMentions>
        <DxHtmlEditorMention Data="@EmployeesData"
                             DisplayFieldName="@nameof(MentionData.Name)"
                             SearchFieldNames="@SearchFieldNames" />
    </DxHtmlEditorMentions>
</DxHtmlEditor>

@code {
    string Markup = @"<p>
                      <span class='dx-mention' spellcheck='false' data-marker='@' data-mention-value='Kevin Carter'>
                        <span>
                            <span>@</span>
                            Kevin Carter
                        </span>
                      </span>
                      I think John's expertise can be very valuable in our startup.
                    </p>";
    string[] SearchFieldNames = { nameof(MentionData.Name) };

    class MentionData {
        public string Name { get; set; }
        public string Team { get; set; }
    }
    MentionData[] EmployeesData = {
        new MentionData() { Name = "John Heart", Team = "Engineering" },
        new MentionData() { Name = "Kevin Carter", Team = "Engineering" },
        new MentionData() { Name = "Olivia Peyton", Team = "Management" },
        new MentionData() { Name = "Robert Reagan", Team = "Management" },
        new MentionData() { Name = "Cynthia Stanwick", Team = "Engineering" },
        new MentionData() { Name = "Brett Wade", Team = "Analysis" },
        new MentionData() { Name = "Greta Sims", Team = "QA" },
    };
}

Run Demo: HTML Editor - Mentions

Placeholder Variables

<DxHtmlEditor> supports placeholder variables you can use to create templates for document generation. When a user clicks the toolbar’s Variable command, the component displays a drop-down list of available variables. The editor inserts the selected placeholder variable at the caret position in the document and encloses the variable between escape sequences.

Follow the steps below to create and configure variables:

  1. Add a DxHtmlEditorVariables object to component markup.
  2. Use the DxHtmlEditorVariables.Data property to store variables.
  3. Use the EscapeCharacters property to specify escape sequences that enclose placeholder variables in the document.
  4. Add the Variable group to the component’s toolbar in a CustomizeToolbar event handler to display the Variable command.

The following code snippet implements placeholder variables and adds the Variable command to the built-it toolbar:

Html Editor - Variables

@using DevExpress.Blazor.Office

<DxHtmlEditor Height="200px"
              CustomizeToolbar="@OnCustomizeToolbar">
    <DxHtmlEditorVariables Data=@Variables
                           EscapeCharacters="@escapeChar" />
</DxHtmlEditor>

@code {
    string[] Variables = new string[] { "FirstName", "LastName" };
    // Declare one string
    string escapeChar = "$";
    // Declare an array of strings
    string[] escapeChar = new string[] { "$", "$" };

    void OnCustomizeToolbar(IToolbar toolbar) {
        toolbar.Groups.Add(HtmlEditorToolbarGroupNames.Variable);
    }
}

Replace Variables

The following code snippet replaces variables with actual values:

HTML Editor - Replace Variables

@using DevExpress.Blazor.Office
@using HtmlAgilityPack

<DxHtmlEditor @bind-Markup="@simpleMarkup"
              CustomizeToolbar="@OnCustomizeToolbar" ... />
<DxButton Text="Replace variables" Click="@onButtonClick" />

@code{
    void OnCustomizeToolbar(IToolbar toolbar) {
        // ...
        toolbar.Groups.Add(HtmlEditorToolbarGroupNames.Variable);
        // ...
    }
    void onButtonClick() {
        simpleMarkup = ReplaceVariables("John", "Smith");
    }

    string ReplaceVariables(string firstName, string lastName) {
        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(simpleMarkup);

        var nodes = doc.DocumentNode.SelectNodes("//span[@class='dx-variable']");

        if (nodes != null) {
            foreach (var node in nodes) {
                var varValue = node.GetAttributeValue("data-var-value", "");
                if (varValue == "FirstName") {
                    node.ParentNode.ReplaceChild(HtmlNode.CreateNode(firstName), node);
                }
                else if (varValue == "LastName") {
                    node.ParentNode.ReplaceChild(HtmlNode.CreateNode(lastName), node);
                }
            }
        }
        return doc.DocumentNode.OuterHtml;
    }

    string simpleMarkup = @"<p>Hello <span class='dx-variable' data-var-start-esc-char='{' data-var-end-esc-char='}' data-var-value='FirstName'>
                            <span contenteditable='false'>{FirstName}</span></span>
                            <span class='dx-variable' data-var-start-esc-char='{' data-var-end-esc-char='}' data-var-value='LastName'>
                            <span contenteditable='false'>{LastName}</span></span>! Nice to meet you!</p>";
}

<DxHtmlEditor> allows users to add hyperlinks to the document. When a user clicks the Hyperlink command in the toolbar’s Insert Element group, the editor invokes the Hyperlink dialog:

HTML Editor - Hyperlink Dialog

This dialog allows users to edit existing or create new links to web pages.

Images

<DxHtmlEditor> allows users to add pictures to the document. Once a user clicks the Picture command in the toolbar’s Insert Element group, the editor invokes the Insert Image or Update Image dialog:

HTML Editor - Insert Image Dialog

Users can upload a picture from the local file system or specify a URL.

<DxHtmlEditor> allows users to resize images. To disable this functionality, set the MediaResizeEnabled property to false.

Tables

<DxHtmlEditor> supports table functionality: users can select commands in the toolbar’s Table group to add or delete tables or individual rows/columns.

Html Editor - Toolbar Table Group

Insert Table Dialog

Once a user clicks the toolbar’s Table command, the editor invokes the Insert Table dialog:

Html Editor - Insert Table Dialog

This dialog allows users to create a table with a specified number of rows and columns.

Table Resize

<DxHtmlEditor> allows you to specify whether users can resize tables. To enable resize operations, set the TableResizeEnabled property to true. You can also use TableColumnMinWidth and TableRowMinHeight properties to specify minimum column width and row height.

The following code snippet allows users to resize tables and configures the minimum column width and row height:

HTML Editor - Table Resize

<DxHtmlEditor Markup="@markup"
              TableResizeEnabled="true"
              TableColumnMinWidth="100"
              TableRowMinHeight="50"
              Height="500px"
              Width="100%" />

@code {
    string markup = "";
    protected override void OnInitialized() {
        markup = GetData();
    }
}

Toolbar

The HTML Editor displays a built-in toolbar that consists of multiple groups with various items within a group. The toolbar supports adaptivity and customization.

Html Editor - Toolbar

Adaptivity

The HTML Editor’s toolbar supports adaptive mode. When you resize the browser window, the toolbar hides grouped items in drop-down menus starting from the right-most item. You can use the AdaptivePriority property to specify an item’s hiding order.

When the component width changes, the toolbar also hides a group’s text and displays an icon.

Html Editor - Adaptive Toolbar

Customization

<DxHtmlEditor> allows you to access and modify the built-in toolbar. Handle the CustomizeToolbar event to perform the following operations:

Note

To access and modify toolbar settings, register the DevExpress.Blazor.Office namespace:

@using DevExpress.Blazor.Office

Keyboard Navigation

DevExpress Blazor HTML Editor allows users to access every UI element and run all commands with a keyboard. Keyboard navigation is implemented both on the client and server.

HTML Editor - Keyboard Navigation

Run Demo: HTML Editor - Overview

Keyboard Shortcuts

The image below shows navigation areas available in the HTML Editor component:

HTML Editor - Navigation Areas

Use the following shortcuts to navigate between these areas and elements within an area:

Shortcut Keys

Description

Arrow keys

Move focus between elements within the toolbar.

Tab
Shift + Alt + Arrow Down

Moves focus to the next navigation area.

Shift + Tab
Shift + Alt + Arrow Up

Moves focus to the previous navigation area.

You can use common shortcuts to work with document content. The built-in toolbar component includes its own navigation shortcuts. Refer to the following help topic for more information on keyboard support in various DevExpress Blazor components: Accessibility - Keyboard Support.

Troubleshooting

If a Blazor application throws unexpected exceptions, refer to the following help topic: Troubleshooting.

Inheritance

Object
ComponentBase
DevExpress.Blazor.Base.DxAsyncDisposableComponent
DevExpress.Blazor.Base.DxDecoratedComponent
DxComponentBase
DevExpress.Blazor.ClientComponents.Internal.ClientComponentBase
DevExpress.Blazor.ClientComponents.Internal.ClientComponentJSInterop
DxHtmlEditor
See Also