tui.editor.blazor is a Blazor component that provides a Markdown editor based on the tui.editor library.
Install the package using following command:
Install-Package ToastUIEditor
or
dotnet add package ToastUIEditoror just use nuget package manager.
- Add the following import statement to your
_Imports.razorfile
@using ToastUI- Use the
Editorcomponent in your Blazor page or component
<Editor @bind-Value="content" Options="@options" />@bind-Value: Binds the editor's content to a string property in your Blazor component.Options: Sets the configuration options for the editor. Refer to theEditorOptionsclass for available options.
- Use the
Viewercomponent in your Blazor page or component
<Viewer Value="@content" />Value: Sets the content to be displayed in the viewer. It will update automatically whencontentchanges.
- Handle the available events by specifying event callbacks
<Editor @bind-Value="content"
Load="HandleLoad"
Change="HandleChange"
CaretChange="HandleCaretChange"
Focus="HandleFocus"
Blur="HandleBlur"
KeyDown="HandleKeyDown"
KeyUp="HandleKeyUp"
BeforePreviewRender="HandleBeforePreviewRender"
BeforeConvertWYSIWYGToMarkdown="HandleBeforeConvertWYSIWYGToMarkdown" />
<Viewer Value="content"
Load="HandleLoad"
UpdatePreview="HandleUpdatePreview" />These events are the same as the native public events, and the parameters are detailed in the code comments.
- Access the
EditororViewerinstance to invoke methods
<Editor @ref="editorRef" @bind-Value="markdown" Options="@options" />
<Viewer @ref="viewerRef" Value="markdown" />
<button @onclick="HandlePreview">Preview</button>
<Viewer @ref="viewerRef2"/>
@code {
Editor editorRef = default!;
Viewer viewerRef = default!;
Viewer viewerRef2 = default!;
string markdown = string.Empty;
async Task HandlePreview()
{
var markdown = await editorRef.GetMarkdown();
await viewerRef2.SetMarkdown(markdown);
}
}Most of all native methods have been implemented. Refer to the Editor class for available methods.
- Add custom language
- Use
Editor.SetLanguagestatic method to add custom language. - Use
Editor.SetDefaultLanguagestatic method to set default language, it will be used when no language is set inEditorOptions.
Note: Please make sure Editor.SetLanguage and Editor.SetDefaultLanguage are called before
Editorcomponent is rendered.
- Widget rules
Due to BlazorServer mode not supporting JavaScript call .NET method synchronously, the widget rules only support in BlazorWebAssembly mode.
-
EditorandViewerbasic usage -
EditorandViewerevents - Language setting and custom language
-
EditorandViewerinstance methods - Toolbar with custom button
- Add command and execute command
- Add widget and set widget rules (only support in
BlazorWebAssemblymode) - Link attributes
- Custom markdown renderer
- Custom HTML renderer
- Custom HTML Sanitizer
This software is licensed under the MIT License