Third party integration in EJ2 JavaScript Rich text editor control

16 May 202513 minutes to read

The Rich Text Editor can be integrated with third-party to suite the application scenario.

CodeMirror integration

Rich Text Editor comes with a basic HTML source editor through view-source property. Code mirror plugin can be used to highlight the syntax of HTML. CodeMirror plugin for Rich Text Editor makes editing of HTML source code with a pleasant experience.

Import necessary CSS and JS files of CodeMirror to the HTML page.

Required JS files of code mirror.

 <script src="scripts/CodeMirror/codemirror.js" type="text/javascript"></script>
 <script src="scripts/CodeMirror/javascript.js" type="text/javascript"></script>
 <script src="scripts/CodeMirror/css.js" type="text/javascript"></script>
 <script src="scripts/CodeMirror/htmlmixed.js" type="text/javascript"></script>

Required CSS file of code mirror.

 <link href="scripts/CodeMirror/codemirror.min.css" rel="stylesheet" />

Add a custom icon for HTML source editor in the toolbar of Rich Text Editor using template option of toolbarSettings and define the code mirror plugins, and then pass the Rich Text Editor content as argument in actionComplete event.

var editor = new ej.richtexteditor.RichTextEditor({
    toolbarSettings: {
      items: ['SourceCode'],
    },
  
    actionComplete: actionCompleteHandler,
  });
  editor.appendTo('#editor');
  
  var myCodeMirror;
  function mirrorConversion(e) {
    var id = editor.getID() + 'mirror-view';
    var mirrorView = editor.element.querySelector('#' + id);
    var rteContainer = editor.element.querySelector('.e-rte-container');
    if (e.targetItem === 'Preview') {
      editor.value = myCodeMirror.getValue();
      editor.dataBind();
      rteContainer.classList.remove('e-rte-code-mirror-enabled');
      editor.focusIn();
    } else {
      rteContainer.classList.add('e-rte-code-mirror-enabled');
      rteContainer.classList.remove('e-source-code-enabled');
      if (!mirrorView) {
        mirrorView = ej.base.createElement('div', {
          className: 'rte-code-mirror',
          id: id,
          styles: 'display: none;',
        });
        rteContainer.appendChild(mirrorView);
        renderCodeMirror(
          mirrorView,
          editor.value === null ? '' : editor.value
        );
      } else {
        myCodeMirror.setValue(editor.value === null ? '' : editor.value);
      }
      myCodeMirror.focus();
    }
  }
  function renderCodeMirror(mirrorView, content) {
    myCodeMirror = CodeMirror(mirrorView, {
      value: content,
      lineNumbers: true,
      mode: 'text/html',
      lineWrapping: true,
    });
  }
  
  function actionCompleteHandler(e) {
    if (
      e.targetItem &&
      (e.targetItem === 'SourceCode' || e.targetItem === 'Preview')
    ) {
      mirrorConversion(e);
    }
  }
<!DOCTYPE html><html lang="en"><head>
    <title>Essential JS 2 Rich Text Editor</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-base/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-richtexteditor/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-lists/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-navigations/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-popups/styles/material.css" rel="stylesheet">
     <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-splitbuttons/styles/material.css" rel="stylesheet">
    
    
     <!--CodeMirror references-->
     <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.3.0/codemirror.js" type="text/javascript"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.3.0/mode/css/css.js" type="text/javascript"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.3.0/mode/xml/xml.js" type="text/javascript"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.3.0/mode/htmlmixed/htmlmixed.js" type="text/javascript"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.3.0/mode/javascript/javascript.js" type="text/javascript"></script>
     <link href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.3.0/codemirror.min.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></script>
<style>
      .e-rte-code-mirror-enabled .rte-code-mirror {
            display: block !important;
            /* To show the custom source code view. */
      }

      .e-rte-code-mirror-enabled .e-rte-content {
            display: none !important;
            /* To hide the editor area when custom source code enabled. */
      }
      body[class*='-dark'] .rte-code-mirror .cm-tag,
      body[class*='highcontrast'] .rte-code-mirror .cm-tag {
            color: #00ff00;
      }

      body[class*='-dark'] .rte-code-mirror .cm-string,
      body[class*='highcontrast'] .rte-code-mirror .cm-string {
            color: blue;
      }

      body[class*='-dark'] .rte-code-mirror .cm-attribute,
      body[class*='highcontrast'] .rte-code-mirror .cm-attribute {
            color: #f00;
      }

      body[class*='-dark'] .rte-code-mirror .CodeMirror-gutters,
      body[class*='-dark']
      .rte-code-mirror
      .CodeMirror
      body[class*='highcontrast']
      .rte-code-mirror
      .CodeMirror-gutters,
      body[class*='highcontrast'] .rte-code-mirror .CodeMirror {
            background-color: transparent;
            color: #fff;
      }

      .tailwind3-dark .CodeMirror-scroll {
            background-color: #111827;
            color: #fff;
      }
</style>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
      <div id="editor"></div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Embedly integration

Rich Text Editor easily integrate with Embed.ly which is probably the best service when it comes to embed the rich content such as Twitter, Facebook, Instagram and lots of other publishing platform embeds.

This can be achieved by binding the actionComplete event to the toolbar items in the toolbarSettings property. In the event handler, create an element and add the appropriate class. The below script is have to add in the sample to embed the content.

<script src="https://cdn.embedly.com/widgets/platform.js" charset="UTF-8"></script>

The above script is added to the page.

var editor = new ej.richtexteditor.RichTextEditor({
    placeholder: "Click link icon in toolbar to add the desired link",
    toolbarSettings: {
        items: ['createLink']
    },
    actionComplete: function (args) {
        if (args.requestType === 'Links') {
            if (args.elements[0].parentNode && args.elements[0].parentNode.tagName === 'A') {
                var emberEle = document.createElement('blockquote'); // to add the link
                emberEle.setAttribute('class', 'embedly-card'); // to add the class
                emberEle.appendChild(args.elements[0].parentElement);
                emberEle.appendChild(document.createElement('p'));
                args.range.insertNode(emberEle); // add the link  description to the rte content
            }
        }
    }
});
editor.appendTo('#editor');
<!DOCTYPE html><html lang="en"><head>
    <title>Essential JS 2 Rich Text Editor</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-base/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-richtexteditor/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-inputs/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-lists/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-navigations/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-popups/styles/material.css" rel="stylesheet">
     <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/29.2.4/ej2-splitbuttons/styles/material.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/at.js/1.4.0/css/jquery.atwho.min.css">
    <script src="https://cdn.embedly.com/widgets/platform.js"></script>
    
    

<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    <div id="container">
        <div id="editor"> </div>
    </div>
  
<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>