Angular5整合富文本編輯器TinyMCE的方法(漢化+上傳)
1. TinyMCE簡(jiǎn)介
TinyMCE是一個(gè)輕量級(jí)的富文本編輯器,相對(duì)于CK編輯器更加精簡(jiǎn),但必須滿足絕大部分場(chǎng)景的需要。
2.安裝和配置TinyMCE
2.1安裝TinyMCE
npm install-保存tinymce
2.2設(shè)置tinymce局部訪問(wèn)(.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定義變量
在項(xiàng)目中的typing.d.ts中
聲明tinymce
變量,不然會(huì)提示發(fā)現(xiàn)tinymce
聲明var tinymce:任何;
2.4拷貝皮膚文件到資產(chǎn)目錄下
Linux和MacOS
cp -r node_modules / tinymce / skins src / assets / skins
2.5安裝中文支持
中文語(yǔ)言包可以從這個(gè)地址下載:
https://www.tinymce.com/downl ...
下載下來(lái)的壓縮文件中會(huì)有一個(gè)langs目錄,里面有zh_CN.js,把這個(gè)目錄復(fù)制到src / assets目錄下,然后在上下中添加引用(.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", "../src/assets/langs/zh_CN.js"復(fù)制代碼 ],
3.創(chuàng)建TinyMCE組件
ng gc 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.獲取和設(shè)置編輯器內(nèi)容
<tiny-editor [elementId]="'defined-tinymce-editor'" (onEditorContentChange)="keyupHandler($event)"></tiny-editor>復(fù)制代碼 // 監(jiān)聽onEditorKeyup事件 private keyupHandler(event) { console.log('編輯器的內(nèi)容:', event); }
總結(jié)
到此這篇關(guān)于Angular5整合富文本編輯器TinyMCE(漢化+上傳)的文章就介紹到這了,更多相關(guān)Angular5整合富文本編輯器TinyMCE(漢化+上傳)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于?angular?material?theming?機(jī)制修改?mat-toolbar?的背景色(示例詳解
最近在學(xué)習(xí)?angular,記錄一下昨天的進(jìn)展,解決的問(wèn)題是通過(guò)?theme?的配置修改?mat-toolbar?的背景色,避免對(duì)色彩的硬編碼,這篇文章主要介紹了基于?angular?material?theming?機(jī)制修改?mat-toolbar?的背景色,需要的朋友可以參考下2022-10-10Angular通過(guò)angular-cli來(lái)搭建web前端項(xiàng)目的方法
這篇文章主要介紹了Angular通過(guò)angular-cli來(lái)搭建web前端項(xiàng)目的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07解決angular2在雙向數(shù)據(jù)綁定時(shí)[(ngModel)]無(wú)法使用的問(wèn)題
今天小編就為大家分享一篇解決angular2在雙向數(shù)據(jù)綁定時(shí)[(ngModel)]無(wú)法使用的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09AngularJS實(shí)現(xiàn)的select二級(jí)聯(lián)動(dòng)下拉菜單功能示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的select二級(jí)聯(lián)動(dòng)下拉菜單功能,結(jié)合完整實(shí)例形式分析了AngularJS實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)菜單的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10Angular搜索 過(guò)濾 批量刪除 添加 表單驗(yàn)證功能集錦(實(shí)例代碼)
這篇文章主要介紹了Angular搜索 過(guò)濾 批量刪除 添加 表單驗(yàn)證功能集錦(實(shí)例代碼),需要的朋友可以參考下2017-10-10Angularjs使用ng-repeat中$even和$odd屬性的注意事項(xiàng)
無(wú)可否認(rèn)angularjs的崛起成為前端很大的福利,最近接到項(xiàng)目,框架便選中了angularjs。angularjs最吸引人的地方就是數(shù)據(jù)的雙向綁定和指令了,這篇文章主要介紹了Angularjs中使用ng-repeat的$even和$odd屬性的注意事項(xiàng),需要的朋友可以參考下2016-12-12Angular 與 Component store實(shí)踐示例
這篇文章主要為大家介紹了Angular 與 Component store實(shí)踐示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02