Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom Quill toolbar in Angular application

My web application is based on Angular and I try to integrate Quill into it using ngx-quill library. I have created my custom embed blot containing just unmodifiable text block (retrieved from the database) and now I try to create a custom toolbar allowing users to insert instances of this blot into the text.

My question is, how is it possible to create a custom drop-down in Quill toolbar where I can supply my own content that will be shown to the user and inserted to the text?

I try to do it this way:

<quill-editor>
  <div quill-editor-toolbar>
    <select class="ql-attribute">
      <option *ngFor="let attribute of documentAttributes()"
              [title]="document.data[attribute.id]"
              (click)="onAddAttribute(attribute.id)">
        {{attribute.name}}
      </option>
    </select>
  </div>
</quill-editor>

...but there are no values shown in the drop-down menu.

It seems that this problem has already been solved for React and plain JS. But it looks like it will be a little bit harder doing so in Angular, especially when Quill is integrated using QuillEditorComponent provided by ngx-quill library.

like image 908
livthomas Avatar asked Dec 13 '17 15:12

livthomas


2 Answers

I managed to do this after some time thanks to this fiddle and a bit of playing around.See here.

component.html:

<quill-editor [(ngModel)]="editorContent"
              [options]="editorOptions" 
              (ready)="onEditorCreated($event)"
              (change)="onContentChanged($event)"></quill-editor>

component.ts:

public editor;
  public editorContent = '<h3>Type Something...</h3>';
  public editorOptions = {
     theme: 'snow',
    modules: {
        toolbar: {
        container:
        [
            [{ 'placeholder': ['[GuestName]', '[HotelName]'] }], // my custom dropdown
            ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
            ['blockquote', 'code-block'],

            [{ 'header': 1 }, { 'header': 2 }],               // custom button values
            [{ 'list': 'ordered' }, { 'list': 'bullet' }],
            [{ 'script': 'sub' }, { 'script': 'super' }],      // superscript/subscript
            [{ 'indent': '-1' }, { 'indent': '+1' }],          // outdent/indent
            [{ 'direction': 'rtl' }],                         // text direction

            [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
            [{ 'header': [1, 2, 3, 4, 5, 6, false] }],

            [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
            [{ 'font': [] }],
            [{ 'align': [] }],

            ['clean']                                    // remove formatting button

        ],
        handlers: {
            "placeholder": function (value) { 
                if (value) {
                    const cursorPosition = this.quill.getSelection().index;
                    this.quill.insertText(cursorPosition, value);
                    this.quill.setSelection(cursorPosition + value.length);
                }
            }
        }
      }
    }
  };

  constructor(private elem: ElementRef) {

  }

  onEditorCreated(quill) {
    this.editor = quill;
    console.log('quill is ready! this is current quill instance object', quill);
  }

  onContentChanged({ quill, html, text }) {
    console.log('quill content is changed!', quill, html, text);
  }
  ngAfterViewInit() {
    // Update your dropdown with labels
    let placeholderPickerItems = this.elem.nativeElement.querySelectorAll('.ql-placeholder .ql-picker-item');
    placeholderPickerItems.forEach(item => item.textContent = item.dataset.value);
    this.elem.nativeElement.querySelector('.ql-placeholder .ql-picker-label').innerHTML
      = 'Insert Data Field &nbsp; &nbsp; &nbsp;' + this.elem.nativeElement.querySelector('.ql-placeholder .ql-picker-label').innerHTML;
  }

Output:

Screenshot of Output

Hope this helps!

like image 132
A.S.K Stringer Avatar answered Nov 02 '22 04:11

A.S.K Stringer


For more recent angular 8 projects, the configuration options are here: https://github.com/KillerCodeMonkey/ngx-quill#config

And this can be done in html via

<quill-editor [modules]="editorOptions" ></quill-editor>

and the js class

export class Component {
  editorOptions= {
      toolbar: [[{ 'list': 'bullet' }]]
  };
}
like image 33
James L. Avatar answered Nov 02 '22 04:11

James L.