angular - 如何在 Angular 应用程序中添加自定义 Quill 工具栏

标签 angular quill ngx-quill

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

我的问题是,如何在 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 中这样做会有点困难,特别是当使用 QuillEditorComponent 集成 Quill 时由 ngx-quill 库提供。

最佳答案

多亏了这个 fiddle 和一些玩耍,我在一段时间后成功做到了这一点。 See here

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

输出:

Screenshot of Output

希望这有帮助!

关于angular - 如何在 Angular 应用程序中添加自定义 Quill 工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47796660/

相关文章:

angular - 如何在 Angular 7 中使用 Iframe

node.js - ng2-test-seed 无法统计 - 没有这样的文件或目录

Angular 6 错误 : CSSSyntaxError - Failed to compile

javascript - 如何将 Quill.js 应用于已经内容可编辑的元素?

javascript - 在 Quill 编辑器中嵌入自定义内联印迹后插入未定义的印迹

具有 Windows 身份验证预检问题的 Angular 4 HTTP POST

带有 QuillJS 的 Android Chrome 和 WebView, native 上下文菜单会阻塞工具栏吗?

angular - 无法在 Angular 4+ 中获取 quilljs 编辑器 ENTER 按键事件监听器

angular - Ngx-Quill 和 ImageResize with Angular universal/SSR

angular - 羽毛笔编辑器中的光标位置