UNPKG

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