1. TinyMCE简介
TinyMCE是一个轻量级的富文本编辑器,相对于CK编辑器更加精简,但必须满足绝大部分场景的需要。
2.安装和配置TinyMCE
2.1安装TinyMCE
npm install-保存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
声明var tinymce:任何;
2.4拷贝皮肤文件到资产目录下
Linux和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”:[ "../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"复制代码 ],
3.创建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.获取和设置编辑器内容
<tiny-editor [elementId]="'defined-tinymce-editor'" (onEditorContentChange)="keyupHandler($event)"></tiny-editor>复制代码 // 监听onEditorKeyup事件 private keyupHandler(event) { console.log('编辑器的内容:', event); }
总结
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]