Dan*_*ppi 3 quill angular-material angular
我正在尝试将 Quill 编辑器与 Angular Material 集成,在此处实现自定义 MatFormFieldControl: https: //github.com/sermicromegas/learning-management-system/tree/master/frontend/src/app/shared/ui/quill-material
您可以在此处查看工作演示(请参阅“描述”字段):https ://ser-learning-management-system.herokuapp.com/course/edit/5e2089a2c4fea300172bca3e
以下是创建自定义 MatFormFieldControl 的指南:https ://material.angular.io/guide/creating-a-custom-form-field-control
它有点有效,但不是应有的样子。例如(转到空表单:https://ser-learning-management-system.herokuapp.com/course/create):
this.editor.root.innerHTML = contents;将初始内容写入编辑器的操作,但我不知道该怎么做有人可以帮助我改进 quill-material.component.ts 吗?如果你想尝试一下,请随意查看 fork 我的 github 代码。
非常感谢
如果可以帮助某人,这是该组件的源代码,在 Angular 9 上进行了测试。\nI\xe2\x80\x99ve 将其命名为 quill-material,因此您可以像这样使用它:
\n\n<mat-form-field>\n <quill-material\n formControlName="your_control_name"\n placeholder="Type your text here..."\n required\n ></quill-material>\n <mat-error *ngIf="formGroup.get(\'your_control_name\').hasError(\'required\')">\n Field is <strong>required</strong>\n </mat-error>\n</mat-form-field>\nRun Code Online (Sandbox Code Playgroud)\n\nimport {\n Component,\n Input,\n OnInit,\n ElementRef,\n ViewChild,\n forwardRef,\n OnDestroy,\n Injector,\n DoCheck,\n HostBinding\n} from \'@angular/core\';\nimport { NG_VALUE_ACCESSOR, ControlValueAccessor, NgControl } from \'@angular/forms\';\nimport { MatFormFieldControl } from \'@angular/material\';\nimport { Subject } from \'rxjs\';\nimport { FocusMonitor } from \'@angular/cdk/a11y\';\nimport { coerceBooleanProperty } from \'@angular/cdk/coercion\';\nimport Quill from \'quill\';\nimport { QuillDeltaToHtmlConverter } from \'quill-delta-to-html\';\n\nconst SELECTOR = \'quill-material\';\n\n@Component({\n selector: SELECTOR,\n template: `<div class="quill-material-container" #container>\n <div class="editor" (click)="onTouched()" [ngStyle]="{\'height\': \'200px\'}"></div>\n </div>`,\n styles: [`img {\n position: relative;\n }`],\n providers: [{\n provide: NG_VALUE_ACCESSOR,\n useExisting: forwardRef(() => QuillMaterialComponent),\n multi: true\n },\n {\n provide: MatFormFieldControl,\n useExisting: QuillMaterialComponent\n }],\n host: {\n \'[id]\': \'id\',\n \'[attr.aria-describedby]\': \'describedBy\'\n }\n})\nexport class QuillMaterialComponent implements OnInit, DoCheck, OnDestroy, ControlValueAccessor, MatFormFieldControl<any> {\n static nextId = 0;\n @HostBinding() id = `quill-material-${QuillMaterialComponent.nextId++}`;\n\n @ViewChild(\'container\', { read: ElementRef, static: true }) container: ElementRef;\n\n stateChanges = new Subject<void>();\n\n quill: any = Quill;\n editor: any;\n controlType = \'quill-material\';\n errorState = false;\n ngControl: any;\n touched = false;\n focused = false;\n\n _value: any;\n\n get value(): any {\n return this._value;\n }\n set value(value) {\n this._value = value;\n this.editor.setContents(this._value);\n this.onChange(value);\n this.stateChanges.next();\n }\n\n @Input()\n get placeholder() {\n return this._placeholder;\n }\n set placeholder(plh) {\n this._placeholder = plh;\n this.stateChanges.next();\n }\n public _placeholder: string;\n\n @Input()\n get required() {\n return this._required;\n }\n set required(req) {\n this._required = coerceBooleanProperty(req);\n this.stateChanges.next();\n }\n public _required = false;\n\n @Input()\n get disabled() {\n return this._disabled;\n }\n set disabled(disabled) {\n this._disabled = coerceBooleanProperty(disabled);\n this.stateChanges.next();\n }\n public _disabled = false;\n\n get empty() {\n const text = this.editor.getText().trim();\n return text ? false : true;\n }\n\n @HostBinding(\'class.floating\')\n get shouldLabelFloat() {\n return this.focused || !this.empty;\n }\n\n @HostBinding(\'attr.aria-describedby\') describedBy = \'\';\n setDescribedByIds(ids: string[]) {\n this.describedBy = ids.join(\' \');\n }\n\n constructor(public elRef: ElementRef, public injector: Injector, public fm: FocusMonitor) {\n fm.monitor(elRef.nativeElement, true).subscribe(origin => {\n this.focused = !!origin;\n this.stateChanges.next();\n });\n }\n\n ngOnInit(): void {\n // avoid Cyclic Dependency\n this.ngControl = this.injector.get(NgControl);\n if (this.ngControl != null) { this.ngControl.valueAccessor = this; }\n\n const editorRef = this.container.nativeElement.querySelector(\'.editor\');\n this.editor = new Quill(editorRef, { theme: \'snow\' });\n this.editor.on(\'text-change\', () => {\n if (this.ngControl.touched) {\n this.onChange(this.getValue());\n }\n });\n }\n\n ngDoCheck(): void {\n if (this.ngControl) {\n this.errorState = this.ngControl.invalid && this.ngControl.touched && !this.focused;\n this.stateChanges.next();\n }\n }\n\n ngOnDestroy() {\n this.stateChanges.complete();\n this.fm.stopMonitoring(this.elRef.nativeElement);\n }\n\n writeValue(contents: any): void {\n if (this.editor && contents) {\n const delta = this.editor.clipboard.convert(contents); // convert html to delta\n this.editor.setContents(delta);\n this._value = contents;\n }\n }\n\n onChange = (delta: any) => { };\n\n registerOnChange(fn: (v: any) => void): void {\n this.onChange = fn;\n }\n\n onTouched = () => { };\n\n registerOnTouched(fn: () => void): void {\n this.onTouched = fn;\n }\n\n onContainerClick(event: MouseEvent) {\n if (!this.focused) {\n this.editor.focus();\n this.focused = true;\n this.stateChanges.next();\n }\n }\n\n private getValue(): any | undefined {\n if (!this.editor) {\n return undefined;\n }\n\n const delta: any = this.editor.getContents();\n if (this.isEmpty(delta)) {\n return undefined;\n }\n\n const converter = new QuillDeltaToHtmlConverter(delta.ops, {});\n const html = converter.convert();\n\n return html;\n }\n\n private isEmpty(contents: any): boolean {\n if (contents.ops.length > 1) {\n return false;\n }\n\n const opsTypes: Array<string> = Object.keys(contents.ops[0]);\n\n if (opsTypes.length > 1) {\n return false;\n }\n\n if (opsTypes[0] !== \'insert\') {\n return false;\n }\n\n if (contents.ops[0].insert !== \'\\n\') {\n return false;\n }\n\n return true;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
5020 次 |
| 最近记录: |