Easily Integrate the React Image Editor into the Rich Text Editor
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (174).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (215)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (100)Streamlit  (1)Succinctly series  (131)Syncfusion  (915)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (147)Chart  (131)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (628)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (507)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (592)What's new  (332)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Easily Integrate the React Image Editor into the Rich Text Editor

Easily Integrate the React Image Editor into the Rich Text Editor

In this blog, we‘ll see how to integrate the Syncfusion React Image Editor with the Rich Text Editor component.

Syncfusion’s React Rich Text Editor is a feature-rich WYSIWYG HTML and Markdown editor. It is widely used to create blogs, forum posts, note sections, support tickets, messaging applications, and more. It allows users to insert images, links, tables, and lists with modular architectures.

The React Image Editor is a UI component for editing and enhancing images, with built-in support for cropping, rotating, flipping, zooming, and filtering. You can annotate images with shapes such as rectangles, ellipses, lines, and text, as well as freehand drawings.

Getting started

Let’s create a React application and configure the Syncfusion React components in it.

Step 1:  Create a new React application.

First, install the create-react-app npm package in the desired location using the following command.

npx create-react-app my-app

Note: Refer to the Getting Started with React Apps documentation to create a React app using the npm and yarn commands.

Step 2: Add the Syncfusion NPM packages.

The NPM public registry contains all the Syncfusion Essential JS 2 packages that are currently available. In this application, we’ll use the React Rich Text Editor, Image Editor, and Dialog (to hold the Image Editor) components.

Use the following commands to install the NuGet packages of these components.

npm install @syncfusion/ej2-react-richtexteditor –save-dev
npm install @syncfusion/ej2-react-image-editor –save-dev
npm install @syncfusion/ej2-react-popups –save-dev	 

Step 3: Add the CSS references for the Syncfusion React components.

After the NuGet package installation, the following CSS files for the Syncfusion React components will be available in the ../node modules/@syncfusion package folder. Add them as references in the src/App.css file.

@import '../node_modules/@syncfusion/ej2-base/styles/bootstrap5.css';
@import '../node_modules/@syncfusion/ej2-icons/styles/bootstrap5.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/bootstrap5.css';
@import '../node_modules/@syncfusion/ej2-splitbuttons/styles/bootstrap5.css';
@import '../node_modules/@syncfusion/ej2-inputs/styles/bootstrap5.css';
@import '../node_modules/@syncfusion/ej2-lists/styles/bootstrap5.css';
@import '../node_modules/@syncfusion/ej2-navigations/styles/bootstrap5.css';
@import '../node_modules/@syncfusion/ej2-react-popups/styles/bootstrap5.css';
@import '../node_modules/@syncfusion/ej2-react-richtexteditor/styles/bootstrap5.css';
@import '../node_modules/@syncfusion/ej2-react-image-editor/styles/bootstrap5.css';

Note: For more details, refer to the getting started documentation for the React Rich Text Editor, ImageEditor, and Dialog components.

Step 4: Add the React Rich Text Editor to the app.

Now, add the following code in the src/App.js file to add the Syncfusion React Rich Text Editor component and inject the required services.

import './App.css';
import { HtmlEditor, Image, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from '@syncfusion/ej2-react-richtexteditor';
import * as React from 'react';

function App() {
  var rteObj;
  return (
    <div>
      <RichTextEditorComponent
        id="rteImageEditor"
        ref={(scope) => {
          rteObj = scope;
        }}>
        <p>
          Integrating an image editor with a React Rich Text Editor can be achieved using a library such as React-Image-Editor.
          <img
            id="img1"
            style={{ height: 335 }}
            src="https://ej2.syncfusion.com/demos/src/image-editor/images/bridge.png"
          ></img>
        </p>
        <p>
          This library provides an easy-to-use API that allows developers to quickly and easily add an image editor to their React Rich Text Editor. It provides a variety of features, including image cropping, resizing, rotation, and more. Additionally, it supports a wide range of image formats, including JPEG, PNG, and GIF. With the help of this library, developers can easily add an image editor to their React Rich Text Editor, allowing users to customize the images they include in their documents.
        </p>
        <Inject services={[Toolbar, Image, Link, HtmlEditor, QuickToolbar]} />
      </RichTextEditorComponent>
    </div>
  );
}

export default App;

Step 5: Add the Image Editor custom toolbar item in the image quick toolbar.

The image quick toolbar feature lets users quickly insert images into the Rich Text Editor. To add a custom Image Editor toolbar item to the image quick toolbar, you will need to do the following:

  1. Configure the required options for the image quick toolbar, such as align and remove.
  2. Include the Image Editor custom toolbar item in the quickToolbarSettings.image property of the Rich Text Editor.

Refer to the following code example.

Import ‘./App.css’;
import { HtmlEditor, Image, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar } from ‘@syncfusion/ej2-react-richtexteditor’;
import * as React from ‘react’;
 
function App() {
  var rteObj;
  const quickToolbarSettings = {
    image: [
      ‘Replace’,
      ‘Align’,
      ‘Caption’,
      ‘Remove’,      
      ‘-‘,
      ‘InsertLink’,
      ‘OpenImageLink’,
      ‘EditImageLink’,
      ‘RemoveImageLink’,
      ‘Display’,
      ‘AltText’,
      {
        tooltipText: ‘Image Editor’,
        template:
          ‘<button class=”e-tbar-btn e-btn” id=”imageEditor”><span class=”e-btn-icon e-icons e-rte-image-editor”></span>’,
      },
    ],
  };
 
  return (
    <div>
      <RichTextEditorComponent
        id=”rteImageEditor”
        ref={(scope) => {
          rteObj = scope;
        }}
        quickToolbarSettings={quickToolbarSettings}
        height={450}>
        <p>
          Integrating an image editor with a React Rich Text Editor can be achieved by using a library such as React-Image-Editor.
          <img
            id=”img1”
            style={{ height: 350 }}
           src=”https://ej2.syncfusion.com/demos/src/image-editor/images/bridge.png”
          ></img>
        </p>
        <p>This library provides an easy-to-use API that allows developers to quickly and easily add an image editor to their React Rich Text Editor. It provides a variety of features, including image cropping, resizing, rotation, and more. Additionally, it supports a wide range of image formats, including JPEG, PNG, and GIF. With the help of this library, developers can easily add an image editor to their React Rich Text Editor, allowing users to customize the images they include in their documents. </p>
        <Inject services={[Toolbar, Image, Link, HtmlEditor, QuickToolbar]} />
      </RichTextEditorComponent>
    </div>
  );
}
 
export default App;

Refer to the following image.

Adding Custom Image Editor Item in the Image Quick Toolbar
Adding Custom Image Editor Item in the Image Quick Toolbar

Step 6: Integrate the React Image Editor with the Rich Text Editor.

Next, add the following code into the src/App.js file to integrate the React Image Editor with the Rich Text Editor component.

Import ‘./App.css’;
import { HtmlEditor, Image, Inject, Link, QuickToolbar, RichTextEditorComponent, Toolbar, NodeSelection  } from ‘@syncfusion/ej2-react-richtexteditor’;
import * as React from ‘react’;
import { DialogComponent } from ‘@syncfusion/ej2-react-popups’;
import { ImageEditorComponent } from ‘@syncfusion/ej2-react-image-editor’;
 
function App() {
  const dlgButtons = [
    {
      buttonModel: { content: ‘Insert’, isPrimary: true },
      click: onInsert.bind(this),
    },
    { buttonModel: { content: ‘Cancel’ }, click: onCancel },
  ];
 
  const selection = new NodeSelection();
  const header = ‘Image Editor’;
  var dialogObj;
  var imageEditorObj;
  var rteObj;
  var range;
  var saveSelection;
  var dataURL;
  var isLoaded = false;
  const quickToolbarSettings = {
    image: [
      ‘Replace’,
      ‘Align’,
      ‘Caption’,
      ‘Remove’,      
      ‘-‘,
      ‘InsertLink’,
      ‘OpenImageLink’,
      ‘EditImageLink’,
      ‘RemoveImageLink’,
      ‘Display’,
      ‘AltText’,
      {
        tooltipText: ‘Image Editor’,
        template:
          ‘<button class=”e-tbar-btn e-btn” id=”imageEditor”><span class=”e-btn-icon e-icons e-rte-image-editor”></span>’,
      },
    ],
  };
 
  function onInsert() {
    if (rteObj.formatter.getUndoRedoStack().length === 0) {
      rteObj.formatter.saveData();
    }
    saveSelection.restore();
    var canvas = document.createElement(
      ‘CANVAS’
    );
    var ctx = canvas.getContext(‘2d’);
    const imgData = imageEditorObj.getImageData();
    canvas.height =imgData.height;
    canvas.width = imgData.width;
    ctx.putImageData(imgData, 0,0);
    isLoaded = true;
    rteObj.executeCommand(“editImage”, { url: canvas.toDataURL(), width:{width: canvas.width}, height:{height:canvas.height}, selection: saveSelection });
    rteObj.formatter.saveData();
    rteObj.formatter.enableUndo(rteObj);
    dialogObj.hide();
  }
 
  function onCancel() {
    dialogObj.hide();
  }
  function onToolbarClick(args) {
    if (args.item.tooltipText === ‘Image Editor’) {
      range = selection.getRange(document);
      saveSelection = selection.save(range, document);
      dialogObj.show();
    }
  }
  function OnBeforeOpen() {
    var imageELement;
    var selectNodes = rteObj.formatter.editorManager.nodeSelection.getNodeCollection(range);
    if(selectNodes.length==1&& selectNodes[0].tagName==”IMG”)
    {
      imageELement = selectNodes[0];
      imageELement.crossOrigin = ‘anonymous’;
      var canvas = document.createElement(‘CANVAS’);
      var ctx = canvas.getContext(‘2d’);
      canvas.height = imageELement.offsetHeight;
      canvas.width = imageELement.offsetWidth;
      imageELement.onload = function () {
        ctx.drawImage(imageELement, 0, 0, canvas.width, canvas.height);
        dataURL = canvas.toDataURL();
        if (!isLoaded) {
          imageEditorObj.open(dataURL);
        }
      }
    }
  }
 
  return (
    <div>
      <RichTextEditorComponent
        id=”rteImageEditor”
        ref={(scope) => {
          rteObj = scope;
        }}
        quickToolbarSettings={quickToolbarSettings}
        toolbarClick= {onToolbarClick}>
        <p>
          Integrating an image editor with a React Rich Text Editor can be achieved by using a library such as React-Image-Editor.
          <img
            id=”img1”
            style={{ height: 335 }}
           src=”https://ej2.syncfusion.com/demos/src/image-editor/images/bridge.png”
          ></img>
        </p>
        <p>This library provides an easy-to-use API that allows developers to quickly and easily add an image editor to their React Rich Text Editor. It provides a variety of features, including image cropping, resizing, rotation, and more. Additionally, it supports a wide range of image formats, including JPEG, PNG, and GIF. With the help of this library, developers can easily add an image editor to their React Rich Text Editor, allowing users to customize the images they include in their documents.</p>
        <Inject services={[Toolbar, Image, Link, HtmlEditor, QuickToolbar]} />
      </RichTextEditorComponent>
 
      <DialogComponent
        id=”ImageEditorDialog”
        ref={(scope) => {
          dialogObj = scope;
        }}
        buttons={dlgButtons}
        beforeOpen={OnBeforeOpen}
        header={header}
        visible={false}
        showCloseIcon={true}
        width=”800px”
        height=”800px”
        isModal={true}
      >
        <div className=”dialogContent”>
          <ImageEditorComponent
            height=”335px”
            ref={(scope) => {
              imageEditorObj = scope;
            }}
          />
        </div>
      </DialogComponent>
    </div>
  );
}
 
export default App;

Then, add the following styles in the src/App.css file for the Image Editor toolbar item icon.

.e-rte-quick-popup .e-rte-quick-toolbar .e-rte-image-editor::before {
  content: “\e730”;
}

Select an image in the Rich Text Editor content; it will show the image quick toolbar. Select the ImageEditor button from the toolbar. A dialog will open with the image editing tools.

The Insert and Cancel buttons will be rendered at the bottom of the dialog. When we choose the Insert button, the edited image will be inserted into the Rich Text Editor content.

Refer to the following image.

Integrating the React Image Editor with the Rich Text Editor Component
Integrating the React Image Editor with the Rich Text Editor Component

GitHub reference

Check out the complete code example for integrating the React Image Editor with the Rich Text Editor on GitHub.

Conclusion

Thanks for reading! In this blog, we’ve seen how to integrate the Syncfusion React Image Editor with the Rich Text Editor component. Try out the steps in this blog and provide your feedback in the comments section below!

If you are new to Syncfusion, try our controls’ features by downloading a free trial. You can also explore our online demos and documentation to explore the other user-friendly features we support.

For questions, you can contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed