How To Section

Add Google fonts

To use web fonts in RTE, it is not needed for the web fonts to be present in local machine. To add the web fonts to RTE, we need to refer the web font links and add the font names in the fontFamily property.

tagHelper
google-webfonts.cs
<ejs-richtexteditor id="image" value="@ViewBag.value" fontFamily="@ViewBag.font">
    <e-richtexteditor-toolbarsettings items="@ViewBag.tools">
</ejs-richtexteditor>
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Great+Vibes">
public IActionResult Image()
{
    object item1 = new
{
    text= "Segoe UI", value= "Segoe UI"
};
object item2 = new
{
    text= "Roboto", value= "Roboto"
};
object item3 = new
{
    text= "Great vibes", value= "Great Vibes,cursive"
};
object item4 = new
{
    text= "Noto Sans", value= "Noto Sans"
};
object item5 = new
{
    text= "Impact", value= "Impact,Charcoal,sans-serif"
};
object item6 = new
{
   text= "Tahoma", value= "Tahoma,Geneva,sans-serif"
};
    ViewBag.font = new[] {
        items:new[]{ item1, item2, item3, item4, item5, item6 }
    };
    ViewBag.tools = new[] { 
        "Bold", "Italic", "Underline", "StrikeThrough",
        "FontName", "FontSize", "FontColor", "BackgroundColor",
        "LowerCase", "UpperCase", "|",
        "Formats", "Alignments", "OrderedList", "UnorderedList",
        "Outdent", "Indent", "|",
        "CreateLink", "Image", "|", "ClearFormat", "Print",
        "SourceCode", "FullScreen", "|", "Undo", "Redo"
    };
    
    ViewBag.value = @"<p>The rich text editor component is WYSIWYG ('what you see is what you get') editor that provides the best user experience to create and update the content. 
                        Users can format their content using standard toolbar commands.</p>
    <p><b> Key features:</b></p>

    <ul>
        <li><p> Provides & lt; IFRAME & gt; and & lt; DIV & gt; modes </p></li>

        <li><p> Capable of handling markdown editing.</p></li>

        <li><p> Contains a modular library to load the necessary functionality on demand.</p></li>

        <li><p> Provides a fully customizable toolbar.</p></li>

        <li><p> Provides HTML view to edit the source directly for developers.</p></li>
    
        <li><p> Supports third - party library integration.</p></li>
    
        <li><p> Allows preview of modified content before saving it.</p></li>
    
        <li><p> Handles images, hyperlinks, video, hyperlinks, uploads, etc.</p></li>
    
        <li><p> Contains undo / redo manager.</p></li>
    
        <li><p> Creates bulleted and numbered lists.</p></li>
                                        
    </ul>";
    return View();
}

The below font style links are referred in the page.

<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Great+Vibes">

In the above sample, you can see that we have added two Google web fonts (Roboto and Great vibes) to RTE.

Capture ctrl+s to update the value

To achieve this, we need to bind the keydown event to the RTE content and capture the ctrl + s key press using its keyCode. In the keydown event handler, the updateValue method is called to update the value property and then we can save the content in the required database using the same.

tagHelper
save.cs
<ejs-richtexteditor id="iframe" value="@ViewBag.value" Created="onCreate">
</ejs-richtexteditor>
<script>
function onCreate(){
document.getElementById("default").ej2_instances[0].contentModule.getDocument().addEventListener("keydown",function(e: any):void{
if(e.key === 's' && e.ctrlKey===true){
      e.preventDefault(); // to prevent default ctrl+s action
      document.getElementById("default").ej2_instances[0].updateValue(); // to update the value after editing
      let value: any= document.getElementById("default").ej2_instances[0].value; // you can get the RTE content to save in the desired database
}

});
}
</script>
public IActionResult Iframe()
{
    ViewBag.value = @"<p>The rich text editor component is WYSIWYG ('what you see is what you get') editor that provides the best user experience to create and update the content. 
                        Users can format their content using standard toolbar commands.</p>
    <p><b> Key features:</b></p>

    <ul>
        <li><p> Provides & lt; IFRAME & gt; and & lt; DIV & gt; modes </p></li>

        <li><p> Capable of handling markdown editing.</p></li>

        <li><p> Contains a modular library to load the necessary functionality on demand.</p></li>

        <li><p> Provides a fully customizable toolbar.</p></li>

        <li><p> Provides HTML view to edit the source directly for developers.</p></li>
    
        <li><p> Supports third - party library integration.</p></li>
    
        <li><p> Allows preview of modified content before saving it.</p></li>
    
        <li><p> Handles images, hyperlinks, video, hyperlinks, uploads, etc.</p></li>
    
        <li><p> Contains undo / redo manager.</p></li>
    
        <li><p> Creates bulleted and numbered lists.</p></li>
                                        
    </ul>";
    return View();
}

Customize the placeholder style

By using rte-placeholder class, you can customize the placeholder style.

tagHelper
placeholder.cs
<ejs-richtexteditor id="default" showCharCount="true" placeholder="Type Something"></ejs-richtexteditor>
<style>
    .e-richtexteditor .rte-placeholder {
        font-family: monospace;
        color: deeppink;
    }
</style>
public IActionResult Placeholder()
{
    ViewBag.value = @"<p>The rich text editor component is WYSIWYG ('what you see is what you get') editor that provides the best user experience to create and update the content. 
                    Users can format their content using standard toolbar commands.</p>
                <p><b> Key features:</b></p>
                <ul><li><p> Provides &lt; IFRAME &gt; and &lt; DIV &gt; modes </p></li>
                    <li><p> Capable of handling markdown editing.</p></li>
                    <li><p> Contains a modular library to load the necessary functionality on demand.</p></li>
                    <li><p> Provides a fully customizable toolbar.</p></li>
                    <li><p> Provides HTML view to edit the source directly for developers.</p></li>
                    <li><p> Supports third - party library integration.</p></li>
                    <li><p> Allows preview of modified content before saving it.</p></li>
                    <li><p> Handles images, hyperlinks, video, hyperlinks, uploads, etc.</p></li>
                    <li><p> Contains undo / redo manager.</p></li>
                    <li><p> Creates bulleted and numbered lists.</p></li>
                </ul>";
    return View();
}

Set the cursor at the specific range

This can be achieved by using setRange method in the RTE using NodeSelection instance. In this below sample, we have passed the text node (specific location in RTE content) in setStart method and passed the range in setRange method of RTE.

tagHelper
cursor.cs
<ejs-richtexteditor id="iframe" value="@ViewBag.value">
</ejs-richtexteditor>
<input id="btn" type="button" value="Set cursor point" onclick="btnClick(this)"/>
<script>
<script>
var selectioncursor = new ej.richtexteditor.NodeSelection();
 function btnClick(){
      var element = document.getElementById("default").ej2_instances[0].contentModule.getDocument().getElementsById("key");
      var range = document.createRange();
      range.setStart(element, 1); // to set the range
      selectioncursor.setRange(document, range); // to set the cursor
    }
</script>
public IActionResult Iframe()
{
    ViewBag.value = @"<p>The rich text editor component is WYSIWYG ('what you see is what you get') editor that provides the best user experience to create and update the content. 
                        Users can format their content using standard toolbar commands.</p>
    <p id='key'><b> Key features:</b></p>

    <ul>
        <li><p> Provides & lt; IFRAME & gt; and & lt; DIV & gt; modes </p></li>

        <li><p> Capable of handling markdown editing.</p></li>

        <li><p> Contains a modular library to load the necessary functionality on demand.</p></li>

        <li><p> Provides a fully customizable toolbar.</p></li>

        <li><p> Provides HTML view to edit the source directly for developers.</p></li>
    
        <li><p> Supports third - party library integration.</p></li>
    
        <li><p> Allows preview of modified content before saving it.</p></li>
    
        <li><p> Handles images, hyperlinks, video, hyperlinks, uploads, etc.</p></li>
    
        <li><p> Contains undo / redo manager.</p></li>
    
        <li><p> Creates bulleted and numbered lists.</p></li>
                                        
    </ul>";
    return View();
}

Save RichTextEditor content in a file in the server

RichTextEditor content can be passed from view to controller through XMLHttpRequest post. Content will be sent to the corresponding method into the controller and this value can be saved in a text file or any other format using streamWriter. Refer to the following given code.

tagHelper
savefile.cs
<ejs-richtexteditor id="default" value="@ViewBag.value" ></ejs-richtexteditor>
<div> <button id="btn">save content</button></div>

<script>
document.getElementById('btn').addEventListener('click', function () {
  var obj = document.getElementById('default').ej2_instances[0];
  var value = JSON.stringify({ text: obj.value });
  const Http = new XMLHttpRequest();
  const url = '@Url.Action("Save")';
    Http.open("POST", url);
    Http.setRequestHeader('Content-Type', 'application/json');
    Http.send(value);
 });
</script>
[System.Web.Http.HttpPost]
public ActionResult Save([FromBody]string text)
 {
   string RootPath = Server.MapPath("~/data.txt");
   StreamWriter wirteFile = new StreamWriter(RootPath, true);
   wirteFile.WriteLine(text);
   wirteFile.Close();
   wirteFile.Dispose();
   return View();
 }