How to install a tinymce with angular2 project?

那年仲夏 提交于 2020-01-15 03:39:08

问题


I need to integrate an editor with my project angular2

i choose tinymce but I dont know how to install it

I found something like :

tinymce.init({
    selector: '.tinymce-editor',
    schema: 'html5',
  });

回答1:


Working Plunker

Using ngForm, ControlGroup, NgZone: Implement a Directive something like this

import {ElementRef, Directive, NgZone} from '@angular/core';
import {ControlGroup, Control} from '@angular/common';

@Directive({
  inputs: ['tinyMce'],
  selector: '[tinyMce]'
})
export class TinyEditor {
  public tinyMce: ControlGroup;
  private zone: NgZone;
  private id: string = Math.random().toString(36).substr(2, 5);
  private controlName: string;
  private theControl: Control;

  public constructor(private elRef: ElementRef, private zone: NgZone) {
  }

  public ngOnInit(): void {
    this.zone.runOutsideAngular(() => {
      this.controlName = this.elRef.nativeElement.getAttribute('ngControl');
      this.theControl = this.tinMce.find(this.controlName);
      this.elRef.nativeElement.setAttribute('tiny-id', id);
    });
  }

  public ngAfterViewInit(): void {
    this.zone.runOutsideAngular(() => {
      tinymce.init({
        valid_elements: '*[*]',
        selector: '[tiny-id=' + this.id + ']',
        schema: 'html5',
        height: 400,
        setup: (editor): void => {
          editor.on('keyup change', () => {
            this.zone.run(() => {
              let value: Object = editor.getContent();
              this.theControl.updateValue(value, {emitEvent: true});
              this.theControl.markAsDirty();
              this.theControl.markAsTouched();
              this.theControl.updateValueAndValidity();
            });
          });
        }
      });
    });
  }

  public ngOnDestroy(): void {
    try {
      tinymce.remove('[tiny-id=' + this.id + ']');
    } catch(e) {
      console.error('Error:', e);
    }
  }
}

Usage:

<form #f="ngForm">
  <input [tinyMce]="f" ngControl="valueHolder">
</form>

This way you can also have as many instances as you want, on a single page.



来源:https://stackoverflow.com/questions/37855066/how-to-install-a-tinymce-with-angular2-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!