Angular5 整合富文本编辑器TinyMCE(汉化+上传)

前端之家收集整理的这篇文章主要介绍了Angular5 整合富文本编辑器TinyMCE(汉化+上传)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. TinyMCE简介

TinyMCE是一个轻量级的富文本编辑器,相对于CKEditor更加精简,但足以满足绝大部分场景的需要。

2. 安装和配置TinyMCE

  • 2.1 安装TinyMCE

    npm install --save tinymce
  • 2.2 设置tinymce全局访问(.angular-cli.json)

    1. "scripts": [
    2. "../node_modules/_tinymce@4.7.4/tinymce.js","../node_modules/_tinymce@4.7.4/themes/modern/theme.js","../node_modules/_tinymce@4.7.4/plugins/link/plugin.js","../node_modules/_tinymce@4.7.4/plugins/paste/plugin.js","../node_modules/_tinymce@4.7.4/plugins/table/plugin.js"
    3. ],
  • 2.3 定义全局变量

    在项目中的typing.d.ts中声明tinymce全局变量,不然会提示找不到tinymce
    declare var tinymce: any;
  • 2.4 拷贝皮肤文件到assets目录下

    Linux and MacOS
    cp -r node_modules/tinymce/skins src/assets/skins
  • 2.5 安装中文支持

    中文语言包可以从这个地址下载:https://www.tinymce.com/downl...

    下载下来的压缩文件中会有一个langs目录,里面有zh_CN.js,把这个目录拷贝到src/assets目录下,然后在全局中添加引用(.angular-cli.json):
    "scripts": [

    1. "../node_modules/_tinymce@4.7.4/tinymce.js","../node_modules/_tinymce@4.7.4/plugins/table/plugin.js","../src/assets/langs/zh_CN.js"

    ],

3. 创建TinyMCE组件

ng g c myeditor
  1. import {
  2. Component,AfterViewInit,EventEmitter,OnDestroy,Input,Output
  3. } from '@angular/core';
  4. import { HttpClient,HttpHeaders } from '@angular/common/http';
  5.  
  6. @Component({
  7. selector: 'tiny-editor',templateUrl: './tiny-editor.component.html',styleUrls: ['./tiny-editor.component.css']
  8. })
  9. export class TinyEditorComponent implements AfterViewInit,OnDestroy {
  10. @Input() elementId: String;
  11. @Output() onEditorContentChange = new EventEmitter();
  12.  
  13. editor;
  14.  
  15. constructor() { }
  16.  
  17. ngAfterViewInit() {
  18. let self = this;
  19. tinymce.init({
  20. selector: '#' + this.elementId,height: 600,plugins: ['link','table','image'],skin_url: 'assets/skins/lightgray',setup: editor => {
  21. this.editor = editor;
  22. editor.on('keyup change',() => {
  23. const content = editor.getContent();
  24. this.onEditorContentChange.emit(content);
  25. });
  26. }
  27. });
  28. }
  29.  
  30. ngOnDestroy() {
  31. tinymce.remove(this.editor);
  32. }
  33.  
  34. }
  1. // tiny-editor.component.html
  2. <textarea id="{{elementId}}"></textarea>

4. 使用自定义TinyMCE组件

  1. <tiny-editor [elementId]="'defined-tinymce-editor'"></tiny-editor>

5. 增加图片上传功能

  1. import {
  2. Component,OnDestroy {
  3. @Input() elementId: String;
  4. @Output() onEditorContentChange = new EventEmitter();
  5.  
  6. editor;
  7.  
  8. constructor(private http: HttpClient) { }
  9.  
  10. ngAfterViewInit() {
  11. let self = this;
  12. tinymce.init({
  13. selector: '#' + this.elementId,() => {
  14. const content = editor.getContent();
  15. this.onEditorContentChange.emit(content);
  16. });
  17. },// 图片上传功能
  18. images_upload_handler: function(blobInfo,success,failure) {
  19. var formData;
  20. formData = new FormData();
  21. console.log(blobInfo);
  22. formData.append("file",blobInfo.blob(),blobInfo.filename());
  23. console.log(formData);
  24. self.uploadFile('http://www.seenode.com/index/player/upload',formData).subscribe( response => {
  25. let url = response['data']['imagePath'];
  26. success(url);
  27. });
  28. }
  29. });
  30. }
  31.  
  32. // 上传图片
  33. private uploadFile(url: string,formData: any) {
  34. var self = this;
  35. var headers = new HttpHeaders();
  36. headers.set("Accept","application/json");
  37. return self.http.post(url,formData,{ headers: headers });
  38. }
  39.  
  40. ngOnDestroy() {
  41. tinymce.remove(this.editor);
  42. }
  43.  
  44. }

6. 获取和设置编辑器内容

  1. <tiny-editor
  2. [elementId]="'defined-tinymce-editor'"
  3. (onEditorContentChange)="keyupHandler($event)"></tiny-editor>
  1. // 监听onEditorKeyup事件
  2. private keyupHandler(event) {
  3. console.log('编辑器的内容:',event);
  4. }

猜你在找的Angularjs相关文章