Angular5整合富文本编辑器TinyMCE的方法(汉化+上传)
- 作者: 如果不爱-请你走开-别让我痴痴的等待
- 来源: 51数据库
- 2021-08-01
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);
}
总结
到此这篇关于angular5整合富文本编辑器tinymce(汉化+上传)的文章就介绍到这了,更多相关angular5整合富文本编辑器tinymce(汉化+上传)内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
热点文章
Angular中响应式表单的三种更新值方法详析
7
Angularjs实现下拉框联动的示例代码
7
详解AngularJS跨页面传值(ui-router)
2
详解AngularJS1.x学习directive 中‘& ’‘=’ ‘@’符号的区别使用
3
angular2路由切换改变页面title的示例代码
4
Angular2 组件间通过@Input @Output通讯示例
5
Angularjs中ng-repeat的简单实例
3
AngularJS 中ui-view传参的实例详解
4
浅谈Angular路由守卫
4
详解基于Angular4+ server render(服务端渲染)开发教程
4
