UNPKG

16.5 kBJavaScriptView Raw
1import { EventEmitter, Output, InjectionToken, forwardRef, ElementRef, NgZone, Inject, PLATFORM_ID, Optional, Component, Input, NgModule } from '@angular/core';
2import { isPlatformBrowser, CommonModule } from '@angular/common';
3import { NG_VALUE_ACCESSOR, FormsModule } from '@angular/forms';
4
5/**
6 * Copyright (c) 2017-present, Ephox, Inc.
7 *
8 * This source code is licensed under the Apache 2 license found in the
9 * LICENSE file in the root directory of this source tree.
10 *
11 */
12const getTinymce = () => {
13 const w = typeof window !== 'undefined' ? window : undefined;
14 return w && w.tinymce ? w.tinymce : null;
15};
16const ɵ0 = getTinymce;
17
18class Events {
19 constructor() {
20 this.onBeforePaste = new EventEmitter();
21 this.onBlur = new EventEmitter();
22 this.onClick = new EventEmitter();
23 this.onContextMenu = new EventEmitter();
24 this.onCopy = new EventEmitter();
25 this.onCut = new EventEmitter();
26 this.onDblclick = new EventEmitter();
27 this.onDrag = new EventEmitter();
28 this.onDragDrop = new EventEmitter();
29 this.onDragEnd = new EventEmitter();
30 this.onDragGesture = new EventEmitter();
31 this.onDragOver = new EventEmitter();
32 this.onDrop = new EventEmitter();
33 this.onFocus = new EventEmitter();
34 this.onFocusIn = new EventEmitter();
35 this.onFocusOut = new EventEmitter();
36 this.onKeyDown = new EventEmitter();
37 this.onKeyPress = new EventEmitter();
38 this.onKeyUp = new EventEmitter();
39 this.onMouseDown = new EventEmitter();
40 this.onMouseEnter = new EventEmitter();
41 this.onMouseLeave = new EventEmitter();
42 this.onMouseMove = new EventEmitter();
43 this.onMouseOut = new EventEmitter();
44 this.onMouseOver = new EventEmitter();
45 this.onMouseUp = new EventEmitter();
46 this.onPaste = new EventEmitter();
47 this.onSelectionChange = new EventEmitter();
48 this.onActivate = new EventEmitter();
49 this.onAddUndo = new EventEmitter();
50 this.onBeforeAddUndo = new EventEmitter();
51 this.onBeforeExecCommand = new EventEmitter();
52 this.onBeforeGetContent = new EventEmitter();
53 this.onBeforeRenderUI = new EventEmitter();
54 this.onBeforeSetContent = new EventEmitter();
55 this.onChange = new EventEmitter();
56 this.onClearUndos = new EventEmitter();
57 this.onDeactivate = new EventEmitter();
58 this.onDirty = new EventEmitter();
59 this.onExecCommand = new EventEmitter();
60 this.onGetContent = new EventEmitter();
61 this.onHide = new EventEmitter();
62 this.onInit = new EventEmitter();
63 this.onLoadContent = new EventEmitter();
64 this.onNodeChange = new EventEmitter();
65 this.onPostProcess = new EventEmitter();
66 this.onPostRender = new EventEmitter();
67 this.onPreInit = new EventEmitter();
68 this.onPreProcess = new EventEmitter();
69 this.onProgressState = new EventEmitter();
70 this.onRedo = new EventEmitter();
71 this.onRemove = new EventEmitter();
72 this.onReset = new EventEmitter();
73 this.onSaveContent = new EventEmitter();
74 this.onSetAttrib = new EventEmitter();
75 this.onObjectResizeStart = new EventEmitter();
76 this.onObjectResized = new EventEmitter();
77 this.onObjectSelected = new EventEmitter();
78 this.onSetContent = new EventEmitter();
79 this.onShow = new EventEmitter();
80 this.onSubmit = new EventEmitter();
81 this.onUndo = new EventEmitter();
82 this.onVisualAid = new EventEmitter();
83 }
84}
85Events.propDecorators = {
86 onBeforePaste: [{ type: Output }],
87 onBlur: [{ type: Output }],
88 onClick: [{ type: Output }],
89 onContextMenu: [{ type: Output }],
90 onCopy: [{ type: Output }],
91 onCut: [{ type: Output }],
92 onDblclick: [{ type: Output }],
93 onDrag: [{ type: Output }],
94 onDragDrop: [{ type: Output }],
95 onDragEnd: [{ type: Output }],
96 onDragGesture: [{ type: Output }],
97 onDragOver: [{ type: Output }],
98 onDrop: [{ type: Output }],
99 onFocus: [{ type: Output }],
100 onFocusIn: [{ type: Output }],
101 onFocusOut: [{ type: Output }],
102 onKeyDown: [{ type: Output }],
103 onKeyPress: [{ type: Output }],
104 onKeyUp: [{ type: Output }],
105 onMouseDown: [{ type: Output }],
106 onMouseEnter: [{ type: Output }],
107 onMouseLeave: [{ type: Output }],
108 onMouseMove: [{ type: Output }],
109 onMouseOut: [{ type: Output }],
110 onMouseOver: [{ type: Output }],
111 onMouseUp: [{ type: Output }],
112 onPaste: [{ type: Output }],
113 onSelectionChange: [{ type: Output }],
114 onActivate: [{ type: Output }],
115 onAddUndo: [{ type: Output }],
116 onBeforeAddUndo: [{ type: Output }],
117 onBeforeExecCommand: [{ type: Output }],
118 onBeforeGetContent: [{ type: Output }],
119 onBeforeRenderUI: [{ type: Output }],
120 onBeforeSetContent: [{ type: Output }],
121 onChange: [{ type: Output }],
122 onClearUndos: [{ type: Output }],
123 onDeactivate: [{ type: Output }],
124 onDirty: [{ type: Output }],
125 onExecCommand: [{ type: Output }],
126 onGetContent: [{ type: Output }],
127 onHide: [{ type: Output }],
128 onInit: [{ type: Output }],
129 onLoadContent: [{ type: Output }],
130 onNodeChange: [{ type: Output }],
131 onPostProcess: [{ type: Output }],
132 onPostRender: [{ type: Output }],
133 onPreInit: [{ type: Output }],
134 onPreProcess: [{ type: Output }],
135 onProgressState: [{ type: Output }],
136 onRedo: [{ type: Output }],
137 onRemove: [{ type: Output }],
138 onReset: [{ type: Output }],
139 onSaveContent: [{ type: Output }],
140 onSetAttrib: [{ type: Output }],
141 onObjectResizeStart: [{ type: Output }],
142 onObjectResized: [{ type: Output }],
143 onObjectSelected: [{ type: Output }],
144 onSetContent: [{ type: Output }],
145 onShow: [{ type: Output }],
146 onSubmit: [{ type: Output }],
147 onUndo: [{ type: Output }],
148 onVisualAid: [{ type: Output }]
149};
150const validEvents = [
151 'onActivate',
152 'onAddUndo',
153 'onBeforeAddUndo',
154 'onBeforeExecCommand',
155 'onBeforeGetContent',
156 'onBeforeRenderUI',
157 'onBeforeSetContent',
158 'onBeforePaste',
159 'onBlur',
160 'onChange',
161 'onClearUndos',
162 'onClick',
163 'onContextMenu',
164 'onCopy',
165 'onCut',
166 'onDblclick',
167 'onDeactivate',
168 'onDirty',
169 'onDrag',
170 'onDragDrop',
171 'onDragEnd',
172 'onDragGesture',
173 'onDragOver',
174 'onDrop',
175 'onExecCommand',
176 'onFocus',
177 'onFocusIn',
178 'onFocusOut',
179 'onGetContent',
180 'onHide',
181 'onInit',
182 'onKeyDown',
183 'onKeyPress',
184 'onKeyUp',
185 'onLoadContent',
186 'onMouseDown',
187 'onMouseEnter',
188 'onMouseLeave',
189 'onMouseMove',
190 'onMouseOut',
191 'onMouseOver',
192 'onMouseUp',
193 'onNodeChange',
194 'onObjectResizeStart',
195 'onObjectResized',
196 'onObjectSelected',
197 'onPaste',
198 'onPostProcess',
199 'onPostRender',
200 'onPreProcess',
201 'onProgressState',
202 'onRedo',
203 'onRemove',
204 'onReset',
205 'onSaveContent',
206 'onSelectionChange',
207 'onSetAttrib',
208 'onSetContent',
209 'onShow',
210 'onSubmit',
211 'onUndo',
212 'onVisualAid'
213];
214
215/**
216 * Copyright (c) 2017-present, Ephox, Inc.
217 *
218 * This source code is licensed under the Apache 2 license found in the
219 * LICENSE file in the root directory of this source tree.
220 *
221 */
222const bindHandlers = (ctx, editor) => {
223 validEvents.forEach((eventName) => {
224 const eventEmitter = ctx[eventName];
225 editor.on(eventName.substring(2), (event) => ctx.ngZone.run(() => eventEmitter.emit({ event, editor })));
226 });
227};
228const ɵ0$1 = bindHandlers;
229let unique = 0;
230const uuid = (prefix) => {
231 const date = new Date();
232 const time = date.getTime();
233 const random = Math.floor(Math.random() * 1000000000);
234 unique++;
235 return prefix + '_' + random + unique + String(time);
236};
237const ɵ1 = uuid;
238const isTextarea = (element) => {
239 return typeof element !== 'undefined' && element.tagName.toLowerCase() === 'textarea';
240};
241const ɵ2 = isTextarea;
242const normalizePluginArray = (plugins) => {
243 if (typeof plugins === 'undefined' || plugins === '') {
244 return [];
245 }
246 return Array.isArray(plugins) ? plugins : plugins.split(' ');
247};
248const ɵ3 = normalizePluginArray;
249const mergePlugins = (initPlugins, inputPlugins) => normalizePluginArray(initPlugins).concat(normalizePluginArray(inputPlugins));
250const ɵ4 = mergePlugins;
251// tslint:disable-next-line:no-empty
252const noop = () => { };
253const ɵ5 = noop;
254const isNullOrUndefined = (value) => value === null || value === undefined;
255const ɵ6 = isNullOrUndefined;
256
257/**
258 * Copyright (c) 2017-present, Ephox, Inc.
259 *
260 * This source code is licensed under the Apache 2 license found in the
261 * LICENSE file in the root directory of this source tree.
262 *
263 */
264const createState = () => {
265 return {
266 listeners: [],
267 scriptId: uuid('tiny-script'),
268 scriptLoaded: false
269 };
270};
271const ɵ0$2 = createState;
272const CreateScriptLoader = () => {
273 let state = createState();
274 const injectScriptTag = (scriptId, doc, url, callback) => {
275 const scriptTag = doc.createElement('script');
276 scriptTag.referrerPolicy = 'origin';
277 scriptTag.type = 'application/javascript';
278 scriptTag.id = scriptId;
279 scriptTag.src = url;
280 const handler = () => {
281 scriptTag.removeEventListener('load', handler);
282 callback();
283 };
284 scriptTag.addEventListener('load', handler);
285 if (doc.head) {
286 doc.head.appendChild(scriptTag);
287 }
288 };
289 const load = (doc, url, callback) => {
290 if (state.scriptLoaded) {
291 callback();
292 }
293 else {
294 state.listeners.push(callback);
295 if (!doc.getElementById(state.scriptId)) {
296 injectScriptTag(state.scriptId, doc, url, () => {
297 state.listeners.forEach((fn) => fn());
298 state.scriptLoaded = true;
299 });
300 }
301 }
302 };
303 // Only to be used by tests.
304 const reinitialize = () => {
305 state = createState();
306 };
307 return {
308 load,
309 reinitialize
310 };
311};
312const ɵ1$1 = CreateScriptLoader;
313const ScriptLoader = CreateScriptLoader();
314
315const TINYMCE_SCRIPT_SRC = new InjectionToken('TINYMCE_SCRIPT_SRC');
316const EDITOR_COMPONENT_VALUE_ACCESSOR = {
317 provide: NG_VALUE_ACCESSOR,
318 useExisting: forwardRef(() => EditorComponent),
319 multi: true
320};
321class EditorComponent extends Events {
322 constructor(elementRef, ngZone, platformId, tinymceScriptSrc) {
323 super();
324 this.platformId = platformId;
325 this.tinymceScriptSrc = tinymceScriptSrc;
326 this.cloudChannel = '5';
327 this.apiKey = 'no-api-key';
328 this.id = '';
329 this.modelEvents = 'change keyup undo redo';
330 this.onTouchedCallback = noop;
331 this.onChangeCallback = noop;
332 this._elementRef = elementRef;
333 this.ngZone = ngZone;
334 this.initialise = this.initialise.bind(this);
335 }
336 set disabled(val) {
337 this._disabled = val;
338 if (this._editor && this._editor.initialized) {
339 this._editor.setMode(val ? 'readonly' : 'design');
340 }
341 }
342 get disabled() {
343 return this._disabled;
344 }
345 get editor() {
346 return this._editor;
347 }
348 writeValue(value) {
349 if (this._editor && this._editor.initialized) {
350 this._editor.setContent(isNullOrUndefined(value) ? '' : value);
351 }
352 else {
353 this.initialValue = value === null ? undefined : value;
354 }
355 }
356 registerOnChange(fn) {
357 this.onChangeCallback = fn;
358 }
359 registerOnTouched(fn) {
360 this.onTouchedCallback = fn;
361 }
362 setDisabledState(isDisabled) {
363 if (this._editor) {
364 this._editor.setMode(isDisabled ? 'readonly' : 'design');
365 }
366 else if (isDisabled) {
367 this.init = Object.assign(Object.assign({}, this.init), { readonly: true });
368 }
369 }
370 ngAfterViewInit() {
371 if (isPlatformBrowser(this.platformId)) {
372 this.id = this.id || uuid('tiny-angular');
373 this.inline =
374 typeof this.inline !== 'undefined' ? (typeof this.inline === 'boolean' ? this.inline : true) : this.init && this.init.inline;
375 this.createElement();
376 if (getTinymce() !== null) {
377 this.initialise();
378 }
379 else if (this._element && this._element.ownerDocument) {
380 ScriptLoader.load(this._element.ownerDocument, this.getScriptSrc(), this.initialise);
381 }
382 }
383 }
384 ngOnDestroy() {
385 if (getTinymce() !== null) {
386 getTinymce().remove(this._editor);
387 }
388 }
389 createElement() {
390 const tagName = typeof this.tagName === 'string' ? this.tagName : 'div';
391 this._element = document.createElement(this.inline ? tagName : 'textarea');
392 if (this._element) {
393 this._element.id = this.id;
394 if (isTextarea(this._element)) {
395 this._element.style.visibility = 'hidden';
396 }
397 this._elementRef.nativeElement.appendChild(this._element);
398 }
399 }
400 initialise() {
401 const finalInit = Object.assign(Object.assign({}, this.init), { target: this._element, inline: this.inline, readonly: this.disabled, plugins: mergePlugins(this.init && this.init.plugins, this.plugins), toolbar: this.toolbar || (this.init && this.init.toolbar), setup: (editor) => {
402 this._editor = editor;
403 editor.on('init', (e) => {
404 this.initEditor(editor);
405 });
406 bindHandlers(this, editor);
407 if (this.init && typeof this.init.setup === 'function') {
408 this.init.setup(editor);
409 }
410 } });
411 if (isTextarea(this._element)) {
412 this._element.style.visibility = '';
413 }
414 this.ngZone.runOutsideAngular(() => {
415 getTinymce().init(finalInit);
416 });
417 }
418 getScriptSrc() {
419 return isNullOrUndefined(this.tinymceScriptSrc) ?
420 `https://cdn.tiny.cloud/1/${this.apiKey}/tinymce/${this.cloudChannel}/tinymce.min.js` :
421 this.tinymceScriptSrc;
422 }
423 initEditor(editor) {
424 editor.on('blur', () => this.ngZone.run(() => this.onTouchedCallback()));
425 editor.on(this.modelEvents, () => {
426 this.ngZone.run(() => this.onChangeCallback(editor.getContent({ format: this.outputFormat })));
427 });
428 if (typeof this.initialValue === 'string') {
429 this.ngZone.run(() => {
430 editor.setContent(this.initialValue);
431 this.onChangeCallback(editor.getContent({ format: this.outputFormat }));
432 });
433 }
434 }
435}
436EditorComponent.ctorParameters = () => [
437 { type: ElementRef },
438 { type: NgZone },
439 { type: Object, decorators: [{ type: Inject, args: [PLATFORM_ID,] }] },
440 { type: String, decorators: [{ type: Optional }, { type: Inject, args: [TINYMCE_SCRIPT_SRC,] }] }
441];
442EditorComponent.decorators = [
443 { type: Component, args: [{
444 selector: 'editor',
445 template: '<ng-template></ng-template>',
446 providers: [EDITOR_COMPONENT_VALUE_ACCESSOR],
447 styles: [':host { display: block; }']
448 },] }
449];
450EditorComponent.ctorParameters = () => [
451 { type: ElementRef },
452 { type: NgZone },
453 { type: Object, decorators: [{ type: Inject, args: [PLATFORM_ID,] }] },
454 { type: String, decorators: [{ type: Optional }, { type: Inject, args: [TINYMCE_SCRIPT_SRC,] }] }
455];
456EditorComponent.propDecorators = {
457 disabled: [{ type: Input }],
458 cloudChannel: [{ type: Input }],
459 apiKey: [{ type: Input }],
460 init: [{ type: Input }],
461 id: [{ type: Input }],
462 initialValue: [{ type: Input }],
463 outputFormat: [{ type: Input }],
464 inline: [{ type: Input }],
465 tagName: [{ type: Input }],
466 plugins: [{ type: Input }],
467 toolbar: [{ type: Input }],
468 modelEvents: [{ type: Input }]
469};
470
471class EditorModule {
472}
473EditorModule.decorators = [
474 { type: NgModule, args: [{
475 imports: [CommonModule, FormsModule],
476 declarations: [EditorComponent],
477 exports: [EditorComponent]
478 },] }
479];
480
481/**
482 * Generated bundle index. Do not edit.
483 */
484
485export { EditorComponent, EditorModule, TINYMCE_SCRIPT_SRC, Events as ɵa };
486//# sourceMappingURL=tinymce-tinymce-angular.js.map