Angular5 整合富文本編輯器TinyMCE(漢化+上傳)

1. TinyMCE簡介

TinyMCE是一個輕量級的富文本編輯器,相對於CKEditor更加精簡,但足以知足絕大部分場景的須要。

2. 安裝和配置TinyMCE

  • 2.1 安裝TinyMCE

    npm install --save tinymce
  • 2.2 設置tinymce全局訪問(.angular-cli.json)

    "scripts": [
         "../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"
     ],
  • 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...css

    下載下來的壓縮文件中會有一個langs目錄,裏面有zh_CN.js,把這個目錄拷貝到src/assets目錄下,而後在全局中添加引用(.angular-cli.json):
    "scripts": [html

    "../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",
       "../src/assets/langs/zh_CN.js"

    ],node

3. 建立TinyMCE組件

ng g c myeditor
import {
    Component,
    AfterViewInit,
    EventEmitter,
    OnDestroy,
    Input,
    Output
} from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Component({
    selector: 'tiny-editor',
    templateUrl: './tiny-editor.component.html',
    styleUrls: ['./tiny-editor.component.css']
})
export class TinyEditorComponent implements AfterViewInit, OnDestroy {
    @Input() elementId: String;
    @Output() onEditorContentChange = new EventEmitter();

    editor;

    constructor() { }

    ngAfterViewInit() {
        let self = this;
        tinymce.init({
            selector: '#' + this.elementId,
            height: 600,
            plugins: ['link', 'table', 'image'],
            skin_url: 'assets/skins/lightgray',
            setup: editor => {
                this.editor = editor;
                editor.on('keyup change', () => {
                    const content = editor.getContent();
                    this.onEditorContentChange.emit(content);
                });
            }
        });
    }

    ngOnDestroy() {
        tinymce.remove(this.editor);
    }

}
// tiny-editor.component.html
<textarea id="{{elementId}}"></textarea>

4. 使用自定義TinyMCE組件

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

5. 增長圖片上傳功能

import {
    Component,
    AfterViewInit,
    EventEmitter,
    OnDestroy,
    Input,
    Output
} from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Component({
    selector: 'tiny-editor',
    templateUrl: './tiny-editor.component.html',
    styleUrls: ['./tiny-editor.component.css']
})
export class TinyEditorComponent implements AfterViewInit, OnDestroy {
    @Input() elementId: String;
    @Output() onEditorContentChange = new EventEmitter();

    editor;

    constructor(private http: HttpClient) { }

    ngAfterViewInit() {
        let self = this;
        tinymce.init({
            selector: '#' + this.elementId,
            height: 600,
            plugins: ['link', 'table', 'image'],
            skin_url: 'assets/skins/lightgray',
            setup: editor => {
                this.editor = editor;
                editor.on('keyup change', () => {
                    const content = editor.getContent();
                    this.onEditorContentChange.emit(content);
                });
            },
            // 圖片上傳功能
            images_upload_handler: function(blobInfo, success, failure) {
                var formData;
                formData = new FormData();
                console.log(blobInfo);
                formData.append("file", blobInfo.blob(), blobInfo.filename());
                console.log(formData);
                self.uploadFile('http://www.seenode.com/index/player/upload', formData).subscribe( response => {
                    let url = response['data']['imagePath'];
                    success(url);
                });
            }
        });
    }

    // 上傳圖片
    private uploadFile(url: string, formData: any) {
        var self = this;
        var headers = new HttpHeaders();
        headers.set("Accept", "application/json");
        return self.http.post(url, formData, { headers: headers });
    }

    ngOnDestroy() {
        tinymce.remove(this.editor);
    }

}

6. 獲取和設置編輯器內容

<tiny-editor 
    [elementId]="'defined-tinymce-editor'"
    (onEditorContentChange)="keyupHandler($event)"></tiny-editor>
// 監聽onEditorKeyup事件
private keyupHandler(event) {
    console.log('編輯器的內容:', event);
}
相關文章
相關標籤/搜索