-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy patheditor.component.ts
More file actions
247 lines (214 loc) · 7.57 KB
/
editor.component.ts
File metadata and controls
247 lines (214 loc) · 7.57 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* eslint-disable @typescript-eslint/no-parameter-properties */
import { isPlatformBrowser, CommonModule } from '@angular/common';
import {
AfterViewInit,
Component,
ElementRef,
forwardRef,
Inject,
Input,
NgZone,
OnDestroy,
PLATFORM_ID,
InjectionToken,
Optional,
ChangeDetectorRef,
ChangeDetectionStrategy
} from '@angular/core';
import { FormsModule, ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { Subject, takeUntil } from 'rxjs';
import { getTinymce } from '../TinyMCE';
import { listenTinyMCEEvent, bindHandlers, isTextarea, mergePlugins, uuid, noop, isNullOrUndefined } from '../utils/Utils';
import { EventObj, Events } from './Events';
import { ScriptLoader } from '../utils/ScriptLoader';
import { Editor as TinyMCEEditor, TinyMCE } from 'tinymce';
type EditorOptions = Parameters<TinyMCE['init']>[0];
export const TINYMCE_SCRIPT_SRC = new InjectionToken<string>('TINYMCE_SCRIPT_SRC');
const EDITOR_COMPONENT_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => EditorComponent),
multi: true
};
export type Version = `${'4' | '5' | '6' | '7'}${'' | '-dev' | '-testing' | `.${number}` | `.${number}.${number}`}`;
@Component({
selector: 'editor',
template: '',
styles: [ ':host { display: block; }' ],
providers: [ EDITOR_COMPONENT_VALUE_ACCESSOR ],
standalone: true,
imports: [ CommonModule, FormsModule ],
changeDetection: ChangeDetectionStrategy.OnPush
})
/**
* @see {@link https://www.tiny.cloud/docs/tinymce/7/angular-ref/} for the TinyMCE Angular Technical Reference
*/
export class EditorComponent extends Events implements AfterViewInit, ControlValueAccessor, OnDestroy {
@Input() public cloudChannel: Version = '7';
@Input() public apiKey = 'no-api-key';
@Input() public licenseKey?: string;
@Input() public init?: EditorOptions;
@Input() public id = '';
@Input() public initialValue?: string;
@Input() public outputFormat?: 'html' | 'text';
@Input() public inline?: boolean;
@Input() public tagName?: string;
@Input() public plugins?: string;
@Input() public toolbar?: string | string[];
@Input() public modelEvents = 'change input undo redo';
@Input() public allowedEvents?: string | string[];
@Input() public ignoreEvents?: string | string[];
@Input()
public set disabled(val) {
this._disabled = val;
if (this._editor && this._editor.initialized) {
if (typeof this._editor.mode?.set === 'function') {
this._editor.mode.set(val ? 'readonly' : 'design');
} else {
this._editor.setMode(val ? 'readonly' : 'design');
}
}
}
public get disabled() {
return this._disabled;
}
public get editor() {
return this._editor;
}
public ngZone: NgZone;
private _elementRef: ElementRef;
private _element?: HTMLElement;
private _disabled?: boolean;
private _editor?: TinyMCEEditor;
private onTouchedCallback = noop;
private onChangeCallback: any;
private destroy$ = new Subject<void>();
public constructor(
elementRef: ElementRef,
ngZone: NgZone,
private cdRef: ChangeDetectorRef,
@Inject(PLATFORM_ID) private platformId: Object,
@Optional() @Inject(TINYMCE_SCRIPT_SRC) private tinymceScriptSrc?: string
) {
super();
this._elementRef = elementRef;
this.ngZone = ngZone;
}
public writeValue(value: string | null): void {
if (this._editor && this._editor.initialized) {
const cursor = this._editor.selection.getBookmark(3);
this._editor.setContent(isNullOrUndefined(value) ? '' : value);
try {
this._editor.selection.moveToBookmark(cursor);
} catch (e) {
/* ignore */
}
} else {
this.initialValue = value === null ? undefined : value;
}
}
public registerOnChange(fn: (_: any) => void): void {
this.onChangeCallback = fn;
}
public registerOnTouched(fn: any): void {
this.onTouchedCallback = fn;
}
public setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
}
public ngAfterViewInit() {
if (isPlatformBrowser(this.platformId)) {
this.id = this.id || uuid('tiny-angular');
this.inline = this.inline !== undefined ? this.inline !== false : !!(this.init?.inline);
this.createElement();
if (getTinymce() !== null) {
this.initialise();
} else if (this._element && this._element.ownerDocument) {
// Caretaker note: the component might be destroyed before the script is loaded and its code is executed.
// This will lead to runtime exceptions if `initialise` will be called when the component has been destroyed.
ScriptLoader.load(this._element.ownerDocument, this.getScriptSrc())
.pipe(takeUntil(this.destroy$))
.subscribe(this.initialise);
}
}
}
public ngOnDestroy() {
this.destroy$.next();
if (getTinymce() !== null) {
getTinymce().remove(this._editor);
}
}
public createElement() {
const tagName = typeof this.tagName === 'string' ? this.tagName : 'div';
this._element = document.createElement(this.inline ? tagName : 'textarea');
if (this._element) {
if (document.getElementById(this.id)) {
/* eslint no-console: ["error", { allow: ["warn"] }] */
console.warn(`TinyMCE-Angular: an element with id [${this.id}] already exists. Editors with duplicate Id will not be able to mount`);
}
this._element.id = this.id;
if (isTextarea(this._element)) {
this._element.style.visibility = 'hidden';
}
this._elementRef.nativeElement.appendChild(this._element);
}
}
public initialise = (): void => {
const finalInit: EditorOptions = {
...this.init,
selector: undefined,
target: this._element,
inline: this.inline,
readonly: this.disabled,
license_key: this.licenseKey,
plugins: mergePlugins((this.init && this.init.plugins) as string, this.plugins),
toolbar: this.toolbar || (this.init && this.init.toolbar),
setup: (editor: TinyMCEEditor) => {
this._editor = editor;
listenTinyMCEEvent(editor, 'init', this.destroy$).subscribe(() => {
this.initEditor(editor);
});
bindHandlers(this, editor, this.destroy$);
if (this.init && typeof this.init.setup === 'function') {
this.init.setup(editor);
}
}
};
if (isTextarea(this._element)) {
this._element.style.visibility = '';
}
this.ngZone.runOutsideAngular(() => {
getTinymce().init(finalInit);
});
};
private getScriptSrc() {
return isNullOrUndefined(this.tinymceScriptSrc) ?
`https://cdn.tiny.cloud/1/${this.apiKey}/tinymce/${this.cloudChannel}/tinymce.min.js` :
this.tinymceScriptSrc;
}
private initEditor(editor: TinyMCEEditor) {
listenTinyMCEEvent(editor, 'blur', this.destroy$).subscribe(() => {
this.cdRef.markForCheck();
this.ngZone.run(() => this.onTouchedCallback());
});
listenTinyMCEEvent(editor, this.modelEvents, this.destroy$).subscribe(() => {
this.cdRef.markForCheck();
this.ngZone.run(() => this.emitOnChange(editor));
});
if (typeof this.initialValue === 'string') {
this.ngZone.run(() => {
editor.setContent(this.initialValue as string);
if (editor.getContent() !== this.initialValue) {
this.emitOnChange(editor);
}
if (this.onInitNgModel !== undefined) {
this.onInitNgModel.emit(editor as unknown as EventObj<any>);
}
});
}
}
private emitOnChange(editor: TinyMCEEditor) {
if (this.onChangeCallback) {
this.onChangeCallback(editor.getContent({ format: this.outputFormat }));
}
}
}