Search code examples
angulareditorquillngx-quill

append data to quill editors existing data angular


I am working on a requirement where I have to append data to a container that is quill-editor container in angular. I tried different things but neither they worked.

I tried below:

Try-1

this.mailTemplateForm.controls['body'].patchValue(value)

Try-2

this.mailTemplateForm.controls.body.setValue(value);

Then the existing data is getting replaced by the new data. What is the solution for this.

component.ts:

import { QuillEditorComponent } from 'ngx-quill';
export class EditMailTemplateComponent implements OnInit {
 @ViewChild('description') description: QuillEditorComponent; 
 mailTemplateForm: FormGroup;

 ngOnInit() {
  this.getFormData();
 }

 editForm(){
//console.log('test',this.data);
this.mailTemplateForm = this.fb.group({
  id: 0,
  name: [''],
  slug: [''],
  status: [''],
  subject: [''],
  body: [],
  body_parameters: [''],
 });
}

getFormData(){
  -----------
  -----------
  this.editForm();
  this.mailTemplateForm.patchValue(this.data.form_data);
}
appendTagTo(value: any){
  console.log('called - ',value);
  this.mailTemplateForm.controls.body.setValue(value); // Tried here
}

component.html

<ul class="list-style-none mt-0">
   <li *ngFor="let field of fieldList" class="py-4 text-uppercase">
     <a color='accent' class='cursor-pointer' (click)="appendTagTo(field.field_name)"> {{ field.label_name }}
     </a>
   </li>
</ul>

<div fxFlex="75" class="mt-12">
  <quill-editor [style.display]="'block'" [style.height]="'400px'" formControlName="body" #description>
  </quill-editor>
</div>

I want the data to be added wherever the cursor presents inside the quill-editor (or) if the cursor not there in the editor at the starting point. Any help would be appreciated. Thanks.


Solution

  • Hope this helps someone. I have solved it by using registering quill-editor's 'onEditorCreated()' method like below:

    <quill-editor [style.display]="'block'" [modules]="editorOptions.editor"
          (onEditorCreated)="onEditorCreated($event)" [style.height]="'300px'" formControlName="body" #description>
        </quill-editor>
    

    component.ts:

    public editor;
    
    onEditorCreated(event) {
     this.editor = event;
    }
    
    appendTagTo(textTOInsert: string) {
     if(textTOInsert){
      textTOInsert = '{{'+textTOInsert+'}}';
      const selection = this.editor.getSelection(true);
      this.editor.insertText(selection.index, textTOInsert);
      //this.editor.insertHTML(selection.index, textTOInsert);
     }
    }