Articles in this section
Category / Section

How to drag and drop the ListView item to the Angular Rich Text Editor as text content

1 min read
This article explains how to drag and drop the ListView item to the Angular RichTextEditor as text content. You can insert the ListView item as text content into the Rich Text Editor by drag and drop using the ‘executeCommand’ method.

Follow the steps mentioned below to achieve this functionality:


1. Initialize the ListView and RichTextEditor components.

<div class="control-section">

        <ejs-listview id='listview' [dataSource]='data'></ejs-listview>

        <br /> <br /> <br />

      <ejs-richtexteditor #RTE id='defaultRTE' (created)='onCreate()'>

      </ejs-richtexteditor>

    </div>

 

2. Define data source by configuring draggable as true in htmlAttributes to enable dragging the items in ListView.


public data: { [key: string]: Object }[] = [

  {

    "text": "Hennessey Venom",

    "id": "list-01",

    "htmlAttributes": { draggable: true }

  },

  {

    "text": "Bugatti Chiron",

    "id": "list-02",

    "htmlAttributes": { draggable: true }

  },

  {

    "text": "Bugatti Veyron Super Sport",

    "id": "list-03",

    "htmlAttributes": { draggable: true }

  }

];
3. Bind the drop event to the edit element of the Rich Text Editor, which is used to retrieve and insert the dropped text into the editor.


this.editArea = document.querySelector("#defaultRTE .e-content") as HTMLElement;

// Drop handler
this.editArea.addEventListener("drop", this.dropHandler.bind(this));

4. Bind the dragStart event to the ListView element to get the dragged item’s text content.

this.listviewEle = document.getElementById('listview') as HTMLElement;

 

// DragStart event binding

this.listviewEle.addEventListener("dragstart", function (e) {

  e.dataTransfer.setData("Text", (e.target as HTMLElement).innerText);

});

5. Now, include the following code in the dropHandler function to insert the text into the Rich Text Editor.


dropHandler(e: any): void {

  //Prevent browser default dragop and drop action

  e.preventDefault();

 

  // Do drop action if the target is inside RTE edit area

  if (this.rteObj.inputElement.contains(e.target)) {

    let range: Range;

 

    if (this.rteObj.contentModule.getDocument().caretRangeFromPoint) {

      range = this.rteObj.contentModule.getDocument().caretRangeFromPoint(e.clientX, e.clientY);

    } else if (e.rangeParent) {

      range = this.rteObj.contentModule.getDocument().createRange();

      range.setStart(e.rangeParent, e.rangeOffset);

    }

 

    this.rteObj.selectRange(range);


    if (this.rteObj.formatter.getUndoRedoStack().length === 0) {

      this.rteObj.formatter.saveData();

    }

    var text = e.dataTransfer.getData('Text').replace(/\n/g, '').replace(/\r/g, '').replace(/\r\n/g, '');


    this.rteObj.executeCommand("insertHTML", text);

    this.rteObj.formatter.saveData();

    this.rteObj.formatter.enableUndo(this.rteObj);

  }

}
Refer to the working sample for additional details and implementation: Sample


Conclusion


We hope you enjoyed learning about how to drag and drop the ListView item to the Angular Rich Text Editor as text content. 

You can refer to our Angular feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our Angular RichTextEditor example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to explore our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!

 



Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied