在Angular2项目中使用Tinymce

Yin*_*Yin 3 tinymce angular

我正在尝试将tinymce嵌入到使用Angular2构建的网站中.以下是我的组件:

export class myComponent implements OnInit {
    //some code

    constructor(private af: AngularFire) {
    // some code
    }

    ngOnInit():any {
    tinymce.init(
        {
            selector: ".tinymce",
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的HTML旁边,有:

<textarea class="tinymce" rows="15"></textarea>
Run Code Online (Sandbox Code Playgroud)

但是有错误说" Cannot find name 'tinymce'"但我已经包括了

<script src='//cdn.tinymce.com/4/tinymce.min.js'></script>
Run Code Online (Sandbox Code Playgroud)

在html的头部.我做错什么了吗?我的初始化不正确吗?

Avi*_* P. 5

这是我在RC6上用于自己的东西.首先,我将展示用法:

<h1>The Editor</h1>
<textarea htmlEditor [(ngModel)]="txt"></textarea>
<h1>The HTML Source</h1>
<textarea [(ngModel)]="txt"></textarea>
<h1>The Rendered HTML</h1>
<div [innerHTML]="txt"></div>
Run Code Online (Sandbox Code Playgroud)

因此使用非常简单直接,编辑器的HTML结果被移动到textarea(我触发blur事件更新)的值

这是指令定义(Typescript):

import {
    Directive,
    OnDestroy,
    AfterViewInit,
    Provider,
    forwardRef,
    HostBinding
} from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
import { DomSanitizer } from '@angular/platform-browser';

declare var tinymce: any;

export const TinyMceValueAccessor: Provider = {
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => TinyMceDirective2),
    multi: true
};

// Tinymce directive
@Directive({
    selector: '[htmlEditor]',
    providers: [TinyMceValueAccessor]
})

export class TinyMceDirective2 implements OnDestroy, AfterViewInit, ControlValueAccessor {
    static nextUniqueId = 0;
    @HostBinding('attr.data-tinymce-uniqueid') uniqueId;

    onTouchedCallback: () => void = () => { };
    onChangeCallback: (_: any) => void = () => { };
    innerValue;
    init = false;

    constructor(private sanitizer: DomSanitizer) {
        this.uniqueId = `tinymce-host-${TinyMceDirective2.nextUniqueId++}`;
    }

    //get accessor
    get value(): any {
        return this.innerValue;
    };

    //set accessor including call the onchange callback
    set value(v: any) {
        if (v !== this.innerValue) {
            this.innerValue = v;
            this.onChangeCallback(v);
        }
    }

    ngAfterViewInit(): void {
        console.log('tinymce');
        tinymce.init({
            selector: `[data-tinymce-uniqueid=${this.uniqueId}]`,
            schema: 'html5',
            setup: ed => {
                ed.on('init', ed2 => {
                    if (this.innerValue) ed2.target.setContent(this.innerValue);
                    this.init = true;
                });
            }
        });

        // I chose to send an update on blur, you may choose otherwise
        tinymce.activeEditor.on('blur', () => this.updateValue());
    }

    updateValue() {
        const content = tinymce.activeEditor.getContent();
        this.value = this.sanitizer.bypassSecurityTrustHtml(content);
    }

    writeValue(value): void {
        if (value !== this.innerValue) {
            this.innerValue = value;
            if (this.init && value) tinymce.activeEditor.setContent(value);
        }
    }

    registerOnChange(fn): void {
        this.onChangeCallback = fn;
    }

    registerOnTouched(fn): void {
        this.onTouchedCallback = fn;
    }

    ngOnDestroy(): void {
        if (this.init) tinymce.remove(`[data-tinymce-uniqueid=${this.uniqueId}]`);
    }
}
Run Code Online (Sandbox Code Playgroud)

一些亮点:

  • 我正在使用NG_VALUE_ACCESSOR提供双向绑定ngModel
  • 我正在为host元素上的自定义属性分配一个唯一的id,这样tinymce只会初始化该特定元素而不会初始化其他元素.
  • 我只在blur事件上发送价值更新,但您可以使用不同的策略,例如使用去抖时间.
  • 我正在使用DomSanitizer绕过消毒,因为tinymce有时输出html,触发Angular 2消毒.