Hi I'm trying to implement tinymce into an angular 2 component for a small forum to create a new thread. I want the content of the textarea (tinymce) be 2-way-binded to a variable inside the component. So far the submit button works but the keyup event doesn't.
export class ForumNewThreadComponent implements OnInit{
constructor(){}
ngOnInit():any {
tinymce.init(
{
selector: ".tinyMCE",
})
}
text:string;
submit(){
this.text = tinymce.activeEditor.getContent();
}
getTinymceContent(){
this.text = tinymce.activeEditor.getContent();
}
}
and view
<div class="thread-body">
{{getValue}}
<textarea class="tinyMCE" style="height:300px" (keyup)="getTinymceContent()">
</textarea>
<button class="btn btn-primary" (click)="submit()">Submit</button>
</div>
I would implement a custom value accessor for this:
const TINY_MCE_VALUE_ACCESSOR = new Provider(
NG_VALUE_ACCESSOR, {useExisting: forwardRef(() => TinyMceValueAccessor), multi: true});
@Directive({
selector: 'textarea[tinymce]',
host: { '(keyup)': 'doOnChange($event.target)' },
providers: [ TINY_MCE_VALUE_ACCESSOR ]
})
export class DateValueAccessor extends DefaultValueAccessor {
@Input()
tinymce:any;
onChange = (_) => {};
onTouched = () => {};
writeValue(value:any):void {
if (value!=null) {
super.writeValue(value.toString());
}
}
doOnChange(elt) {
this.onChange(this.tinymce.activeEditor.getContent());
}
}
I would use it this way:
<textarea [tinymce]="tinymce" style="height:300px" [(ngModel)]="text">
</textarea>
and in your component class:
@Component({
(...)
directives: [ DateValueAccessor ]
})
export class ForumNewThreadComponent implements OnInit{
constructor(){}
ngOnInit():any {
tinymce.init({
selector: "[tinymce]"
})
}
text:string;
}