【问题标题】:How to add custom Quill toolbar in Angular application如何在 Angular 应用程序中添加自定义 Quill 工具栏
【发布时间】:2017-12-13 15:25:14
【问题描述】:

我的 Web 应用程序基于 Angular,我尝试使用 ngx-quill 库将 Quill 集成到其中。我已经创建了我的自定义嵌入印迹,其中只包含不可修改的文本块(从数据库中检索),现在我尝试创建一个自定义工具栏,允许用户将此印迹的实例插入到文本中。

我的问题是,如何在 Quill 工具栏中创建一个自定义下拉菜单,我可以在其中提供我自己的内容,这些内容将显示给用户并插入到文本中?

我尝试这样做:

<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>

...但下拉菜单中没有显示任何值。

Reactplain JS 似乎已经解决了这个问题。但看起来在 Angular 中这样做会有点困难,尤其是当使用 ngx-quill 库提供的 QuillEditorComponent 集成 Quill 时。

【问题讨论】:

    标签: angular quill ngx-quill


    【解决方案1】:

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

    这可以通过 html 来完成

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

    还有js类

    export class Component {
      editorOptions= {
          toolbar: [[{ 'list': 'bullet' }]]
      };
    }
    

    【讨论】:

      【解决方案2】:

      多亏了这个小提琴和一些玩耍,我在一段时间后设法做到了这一点。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;
        }
      

      输出:

      Screenshot of Output

      希望这会有所帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-05
        • 1970-01-01
        • 2016-12-04
        • 2014-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多