Example of Enter Key Configuration in ASP.NET MVC Rich Text Editor Control
This sample demonstrates the API usage to customize the enter key and shift + enter key action in the Rich Text Editor content. Code view represents the current rich text editor value when typing or pressing the enter key or shift + enter key.
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "enterEditor",
SaveInterval = 1,
Height = "200px",
Value = (string)ViewData["RTEValue"]
})
<div class="demo-property-panel">
<div class="demo-property-row">
<label for="enterList">When pressing the enter key</label>
<select id="enterList"><option value="P">P</option><option value="DIV">DIV</option><option value="BR">BR</option></select>
</div>
<div class="demo-property-row">
<label for="shiftEnterList">When pressing the shift + enter key</label>
<select id="shiftEnterList"><option value="BR">BR</option><option value="P">P</option><option value="DIV">DIV</option></select>
</div>
</div>
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "enterEditor",
SaveInterval = 1,
Height = "200px",
Value = (string)ViewData["RTEValue"]
})
<label for="codeView" class="mt-3">Code View</label>
<pre id="codeView" class="small p-2 border rounded" aria-live="polite"></pre>
<script>
var rteObj = function () { return window.Lumen.get('enterEditor'); };
// The code view shows what the editor holds, as it is typed.
function showHtml() { document.getElementById('codeView').textContent = rteObj().getHtml(); }
// The text of each choice says what the key does there.
var TEXTS = {
P: '<p>In Rich text Editor, the enter key and shift + enter key actions can be customized using the enterKey and shiftEnterKey APIs. And the possible values are as follows:</p><ul><li>P - When "P" is configured, pressing enter or shift + enter will create a "p" tag</li><li>DIV - When "DIV" is configured, pressing enter or shift + enter will create a "div" tag</li><li>BR - When "BR" is configured, pressing enter or shift + enter will create a "br" tag</li></ul>',
DIV: '<div>In Rich text Editor, the enter key and shift + enter key action can be customized using the enterKey and shiftEnterKey APIs. And the possible values are as follows</div><ul><li>P - When "P" is configured pressing enter will create a "p" tag</li><li>DIV - When "DIV" is configured pressing enter will create a "div" tag</li><li>BR - When "BR" is configured pressing enter will create a "br" tag</li></ul>',
BR: 'In Rich text Editor, the enter key and shift + enter key action can be customized using the enterKey and shiftEnterKey APIs. And the possible values are as follows<ul><li>P - When "P" is configured pressing enter will create a "p" tag</li><li>DIV - When "DIV" is configured pressing enter will create a "div" tag</li><li>BR - When "BR" is configured pressing enter will create a "br" tag</li></ul>'
};
document.getElementById('enterList').addEventListener('change', function (e) {
rteObj().setEnterKey(e.target.value);
rteObj().value = TEXTS[e.target.value];
showHtml();
});
document.getElementById('shiftEnterList').addEventListener('change', function (e) { rteObj().setShiftEnterKey(e.target.value); });
// The editor starts when the page's scripts have run; its change event then keeps the code view up to date.
window.addEventListener('load', function () { showHtml(); rteObj().addEventListener('change', showHtml); });
</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/EnterKeyConfiguration.cshtml; it draws what ViewData holds.
public ActionResult EnterKeyConfiguration()
{
ViewData["RTEValue"] = "<p>In Rich text Editor, the enter key and shift + enter key actions can be customized using the enterKey and shiftEnterKey APIs. And the possible values are as follows:</p>" +
"<ul><li>P - When \"P\" is configured, pressing enter or shift + enter will create a \"p\" tag</li><li>DIV - When \"DIV\" is configured, pressing enter or shift + enter will create a \"div\" tag</li>" +
"<li>BR - When \"BR\" is configured, pressing enter or shift + enter will create a \"br\" tag</li></ul>";
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>
In these demos, ensuring the API’s behaviors by doing
- Change the value of the
EnterKeydrop down to customize the action when the enter key is pressed. - Change the value of the
ShiftEnterKeydrop down to customize the action when shift + enter is pressed.
The possible values are P (a paragraph), DIV (a div) and BR (a line break). The code view is the editor’s HTML, shown as it changes.