Example of Format Painter in ASP.NET MVC Rich Text Editor Control
This demo demonstrates the Format Painter feature of the Rich Text Editor control. With Format Painter, copy and apply styles from one content to another.
Properties
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "painterEditor",
ToolbarSettings = new UiRichTextToolbar { Items = (List<string>)ViewData["Tools"] },
Value = (string)ViewData["RTEValue"]
})
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "painterEditor",
ToolbarSettings = new UiRichTextToolbar { Items = (List<string>)ViewData["Tools"] },
Value = (string)ViewData["RTEValue"]
})
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row"><label for="allowedFormatInput">Allowed Formats</label><input id="allowedFormatInput" type="text" placeholder="span; strong; em; sup; sub; code;" /></div>
<div class="demo-property-row"><label for="deniedFormatInput">Denied Formats</label><input id="deniedFormatInput" type="text" placeholder="span(important)[title]{background-color,color};" /></div>
</div>
<script>
document.getElementById('allowedFormatInput').addEventListener('blur', function (e) {
window.Lumen.get('painterEditor').setSettings({ formatPainterSettings: { allowedFormats: e.target.value } });
});
document.getElementById('deniedFormatInput').addEventListener('blur', function (e) {
window.Lumen.get('painterEditor').setSettings({ formatPainterSettings: { deniedFormats: e.target.value } });
});
</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/FormatPainter.cshtml; it draws what ViewData holds.
public ActionResult FormatPainter()
{
ViewData["Tools"] = new List<string>
{
"FormatPainter", "Bold", "Italic", "Underline", "StrikeThrough", "SuperScript", "SubScript", "|", "FontName", "FontSize", "FontColor", "BackgroundColor", "LowerCase", "UpperCase", "|",
"Formats", "Alignments", "Blockquote", "|", "OrderedList", "UnorderedList", "|", "Outdent", "Indent", "|", "CreateLink", "Image", "Video", "Audio", "CreateTable", "|", "SourceCode", "Undo", "Redo"
};
ViewData["RTEValue"] = "<h3>Format Painter in Rich Text Editor</h3><p>The <strong>Format Painter</strong> allows you to quickly copy and apply text formatting within the editor, saving time and ensuring consistency.</p>" +
"<h5>How to Use Format Painter?</h5><ul><li><strong>Select the text</strong> with the formatting you want to copy.</li>" +
"<li>Click the <strong>Format Painter</strong> button (paintbrush icon) in the toolbar.</li><li>The cursor changes to a <strong>paintbrush</strong> icon.</li>" +
"<li><strong>Click and drag</strong> over the text where you want to apply the copied format.</li><li>Release the mouse button, and the formatting will be applied.</li></ul>" +
"<h5>Why Use Format Painter?</h5><ul><li><strong>Saves time</strong> when formatting large documents.</li><li><strong>Ensures consistency</strong> in text styles. </li>" +
"<li><strong>Easy to use</strong> for writers, editors, and content creators.</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>
The Format Painter feature allows you to copy the formats and apply them to content without formatting thus saving time to reformat the content.
- Format painter can be accessed via the toolbar or the keyboard shortcuts.
- The sticky mode can be enabled by double-clicking the toolbar button, and it can be utilized to apply a format to multiple locations.
Keyboard Shortcut
- ALT + SHIFT + C - Copy the selection format or current range.
- ALT + SHIFT + V - Paint the copied format.
- ESC - Remove the previously copied format and disable the sticky mode.
The following settings are available to customize the format painter in the FormatPainterSettings property.
- Fill the
Allowed Formatsinput with selectors only whose format styles will be allowed. For example:span; strong; em;as the input allows only the span, strong, and em format styles to be copied. - Fill the
Denied Formatsinput with selectors only whose format styles will be explicitly prohibited. For example:code; sup;does not copy the code and superscript formats. All other format styles will be copied.span(important)[title]{background-color,color};names a class, an attribute and styles of the span element. The editor copies no class, attribute or style of an element, so this one denies nothing.