7

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.

4

2 回答 2

8

由于这个小提琴和一些玩耍,我在一段时间后设法做到了这一点。见这里

组件.html:

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

组件.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;
  }

输出:

输出截图

希望这可以帮助!

于 2018-04-03T10:19:16.753 回答
4

对于最近的 angular 8 项目,配置选项在这里:https ://github.com/KillerCodeMonkey/ngx-quill#config

这可以通过在 html 中完成

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

和 js 类

export class Component {
  editorOptions= {
      toolbar: [[{ 'list': 'bullet' }]]
  };
}
于 2019-11-22T20:00:02.387 回答