Example of Slash Commands in ASP.NET MVC Rich Text Editor Control
This example demonstrates how to use the slash menu feature of the Rich Text Editor to apply formats, open dialogs easily.
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "slashEditor",
Name = "Slash",
Height = "300px",
ToolbarSettings = new UiRichTextToolbar { Items = (List<string>)ViewData["Tools"] },
Value = (string)ViewData["RTEValue"]
})
@Html.UiMention(new UiMentionOptions
{
Id = "rteSlash",
Target = "slashEditor_content",
MentionChar = "/",
PopupWidth = "220px",
OnSelect = "RteSlash.run",
Items = new[]
{
new UiListItem("h1", "Heading 1"), new UiListItem("h2", "Heading 2"), new UiListItem("h3", "Heading 3"),
new UiListItem("ul", "Bulleted list"), new UiListItem("ol", "Numbered list"), new UiListItem("quote", "Quote"),
new UiListItem("pre", "Code block"), new UiListItem("table", "Table"), new UiListItem("hr", "Horizontal line")
}
})
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "slashEditor",
Name = "Slash",
Height = "300px",
ToolbarSettings = new UiRichTextToolbar { Items = (List<string>)ViewData["Tools"] },
Value = (string)ViewData["RTEValue"]
})
@Html.UiMention(new UiMentionOptions
{
Id = "rteSlash",
Target = "slashEditor_content",
MentionChar = "/",
PopupWidth = "220px",
OnSelect = "RteSlash.run",
Items = new[]
{
new UiListItem("h1", "Heading 1"), new UiListItem("h2", "Heading 2"), new UiListItem("h3", "Heading 3"),
new UiListItem("ul", "Bulleted list"), new UiListItem("ol", "Numbered list"), new UiListItem("quote", "Quote"),
new UiListItem("pre", "Code block"), new UiListItem("table", "Table"), new UiListItem("hr", "Horizontal line")
}
})
<script>
window.RteSlash = (function () {
'use strict';
var COMMANDS = {
h1: ['Formats', 'h1'], h2: ['Formats', 'h2'], h3: ['Formats', 'h3'], ul: ['UnorderedList'], ol: ['OrderedList'],
quote: ['Blockquote'], pre: ['Formats', 'pre'], hr: ['HorizontalLine'],
table: ['insertHTML', '<table><tbody><tr><td><br></td><td><br></td></tr><tr><td><br></td><td><br></td></tr></tbody></table><p><br></p>']
};
/** The Mention's OnSelect: the command runs instead of its name being written ("/query" is removed). */
function run(args) {
var command = COMMANDS[args.item.value];
if (!command) { return; }
args.cancel = true;
args.clearQuery = true;
setTimeout(function () { window.Lumen.get('slashEditor').executeCommand(command[0], command[1]); }, 0);
}
return { run: run };
})();
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class RichTextEditorController : Controller
{
// The page is Views/RichTextEditor/SlashCommands.cshtml; it draws what ViewData holds.
public ActionResult SlashCommands()
{
ViewData["RTEValue"] = "<p>Put the caret on the empty line below and type /</p><p><br></p>";
ViewData["Tools"] = new List<string> { "Bold", "Italic", "|", "Formats", "|", "Undo", "Redo" };
return View();
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
This sample demonstrates the SlashMenu feature of the Rich Text Editor, which allows users to apply formatting such as headings, lists, quotes, open insert dialogs, and execute custom commands within the editor. The slash menu can be triggered by typing the "/" character in the editor.
In this example, the slash menu is enabled by setting the enable property within the SlashMenuSettings to true.
This example includes two Custom Slash menu items that allow you to easily insert meeting notes and a signature into the content.
The slash menu is configured with the following properties:
-
enable: Enables or disables the slash menu in the editor. The default value isfalse. -
Items: Defines the items displayed in the slash menu popup. Custom items can also be added, and their actions can be handled using theSlashMenuItemSelectevent.
Adding Custom Slash Menu Items
The custom items can be added to the slash menu by defining the Items child property within SlashMenuSettings. The Items property accepts a string of items and also an array of objects, where each object represents a custom slash menu item. These objects can include the following properties:
text- Sets the text displayed for the slash menu item.command- Specifies the action to be executed when the slash menu item is clicked.type- Groups related items within the slash menu.iconCss- Sets the CSS class for the icon associated with the item.description- Provides a description for the slash menu item.