gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:14:12 25 4
gpt4 key购买 nike

我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com