This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
quillEditor.component.ts
155 lines (130 loc) · 4.16 KB
/
quillEditor.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import {
AfterViewInit,
Component,
ElementRef,
EventEmitter,
forwardRef,
Input,
OnChanges,
Output,
SimpleChanges,
ViewEncapsulation
} from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
declare var require: any;
const Quill = require('quill');
@Component({
selector: 'quill-editor',
template: `<div class="quill-editor"></div>`,
styleUrls: [
'./quillEditor.component.css',
'../quill/dist/quill.core.css',
'../quill/dist/quill.snow.css',
'../quill/dist/quill.bubble.css'
],
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => QuillEditorComponent),
multi: true
}],
encapsulation: ViewEncapsulation.None
})
export class QuillEditorComponent implements AfterViewInit, ControlValueAccessor, OnChanges {
// 基本数据
quillEditor: any;
editorElem: HTMLElement;
content: any;
defaultModules = {
toolbar: [
['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': new Array<any>() }, { 'background': new Array<any>() }], // dropdown with defaults from theme
[{ 'font': new Array<any>() }],
[{ 'align': new Array<any>() }],
['clean'], // remove formatting button
['link', 'image', 'video'] // link and image, video
]
};
// 传入配置
@Input() options: Object;
// 派发事件
@Output() blur: EventEmitter<any> = new EventEmitter();
@Output() focus: EventEmitter<any> = new EventEmitter();
@Output() ready: EventEmitter<any> = new EventEmitter();
@Output() change: EventEmitter<any> = new EventEmitter();
// ...
onModelChange: Function = () => {};
onModelTouched: Function = () => {};
// 注入Dom
constructor(private elementRef: ElementRef) { }
// 视图加载完成后执行初始化
ngAfterViewInit() {
this.editorElem = this.elementRef.nativeElement.children[0];
this.quillEditor = new Quill(this.editorElem, Object.assign({
modules: this.defaultModules,
placeholder: 'Insert text here ...',
readOnly: false,
theme: 'snow',
boundary: document.body
}, this.options || {}));
// 写入内容
if (this.content) {
this.quillEditor.pasteHTML(this.content);
}
// 广播事件
this.ready.emit(this.quillEditor);
// mark model as touched if editor lost focus
this.quillEditor.on('selection-change', (range: any) => {
if (!range) {
this.onModelTouched();
this.blur.emit(this.quillEditor);
} else {
this.focus.emit(this.quillEditor);
}
});
// update model if text changes
this.quillEditor.on('text-change', (delta: any, oldDelta: any, source: any) => {
let html = this.editorElem.children[0].innerHTML;
const text = this.quillEditor.getText();
if (html === '<p><br></p>') html = null;
this.onModelChange(html);
this.change.emit({
editor: this.quillEditor,
html: html,
text: text
});
});
}
// 数据变更时
ngOnChanges(changes: SimpleChanges) {
if (changes['readOnly'] && this.quillEditor) {
this.quillEditor.enable(!changes['readOnly'].currentValue);
}
}
// 写数据
writeValue(currentValue: any) {
this.content = currentValue;
if (this.quillEditor) {
if (currentValue) {
this.quillEditor.pasteHTML(currentValue);
return;
}
this.quillEditor.setText('');
}
}
// 注册事件
registerOnChange(fn: Function): void {
this.onModelChange = fn;
}
// 注册事件
registerOnTouched(fn: Function): void {
this.onModelTouched = fn;
}
}