Articles in this section
Category / Section

How to drag and dropTreeview item text into RichTextEditor for Angular?

10 mins read

You can insert the text into RichTextEditor by dragging and dropping the Angular Treeview item text by using the executeCommand method.

Follow the below mentioned steps:

  1. Initialize the Treeview component with Drag and Drop feature by setting the allowDraAndDrop property to true.
  2. Bind the nodeDragStop event to get the drag item text content, which is used to insert the RichTextEditor.
  3. Now, initialize the RichTextEditor component with the created event.

 

App.component.html

 

 

    <div class="control-section">

        <ejs-treeview [fields]='field' [allowDragAndDrop]=true (nodeDragStop)='nodeDragStopHandler($event)'></ejs-treeview>

    

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

    

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

            <ng-template #valueTemplate>

                <p>Snack: </p>

                <p>Drink: </p>

                <p>Sweet: </p>

            </ng-template>

        </ejs-richtexteditor>

    </div>

 

 

 

  1. Add “e-droppable” to RTE editable input element to allow dropping text in the created event callback.
  2. On nodeDragStopHandler function, insert the dropped text into RTE by calling the executeCommand public method.

 

App.component.ts

 

 

import { ComponentViewChild } from "@angular/core";

import { detach } from "@syncfusion/ej2-base";

import { ToolbarServiceLinkServiceImageServiceHtmlEditorServiceRichTextEditorComponent } from "@syncfusion/ej2-angular-richtexteditor";

 

@Component({

  selector: "app-root",

  templateUrl: "app.component.html",

  providers: [ToolbarServiceLinkServiceImageServiceHtmlEditorService]

})

 

export class AppComponent {

  private editAreaHTMLElement;

  @ViewChild("RTE"nullpublic rteObjRichTextEditorComponent;

 

  //Datasource for Treeview

  public hierarchicalData: { [keystring]: Object }[] = [

    {

      "id": "01",

      "text": "Snacks",

      "expanded": true,

      "child": [

        { "id": "11""text": "Popcorn" },

        { "id": "12""text": "Chips" }

      ]

    },

    {

      "id": "02",

      "text": "Drinks",

      "expanded": true,

      "child": [

        { "id": "21""text": "Pepsi" },

        { "id": "22""text": "Coca cola" }

      ]

    },

    {

      "id": "03",

      "text": "Sweets",

      "expanded": true,

      "child": [

        { "id": "31""text": "Candy" },

        { "id": "32""text": "Chocolate" }

      ]

    },

  ];

  

  //Field config for Treeview

  public fieldObject = {

    id: "id",

    text: "text",

    child: "child",

    dataSource: this.hierarchicalData

  };

 

  //RTE created event callback

  onCreate(): void {

    //Target element where the text to be dropped

    this.editArea = this.rteObj.inputElement as HTMLElement;

 

    //Class used to allow drop text from Treeview

    this.editArea.classList.add("e-droppable");

  }

 

  //Treeview nodeDragStop event callback

nodeDragStopHandler(eDragAndDropEventArgs): void {

 

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

      e.cancel = true;

 

      //Create and select range by current mouse pointer position

      let rangeRange = document.caretRangeFromPoint(e.event.clientXe.event.clientY);

      this.rteObj.selectRange(range);

 

      //Update the undo/redo data into stack

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

        this.rteObj.formatter.saveData();

      }

 

      //Insert the text value into the RTE by using the executeCommand method

      this.rteObj.executeCommand("insertHTML"e.draggedNodeData.text);

 

      this.rteObj.formatter.saveData();

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

    }

  }

}

 

 

Sample: https://stackblitz.com/edit/angular-kncnqt-hc2usq?file=app.component.ts


Conclusion

I hope you enjoyed learning about how to drag and dropTreeview item text into RichTextEditor for Angular.

You can refer to our Angular Treeview feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Angular Treeview 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 check out 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 forumsDirect-Trac, 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