搭建环境:
npm install jquery --save-dev
npm install simditor --save-dev
jsx页面引入:必要的包(一个form表单里的富文本,修改保存)
import React from "react";
import {Form, Input } from "antd";
const createForm = Form.create;
const FormItem = Form.Item;
import $ from "jquery" ;
import Simditor from "simditor";
this.editor.on是编辑器监听内容改变事件,用于将该值赋值给下面真是的desc字段
componentDidMount() {
this.editor = new Simditor({
textarea: this.refs.simditor,
placeholder: '请输入告警处理说明',
//toolbarFloat: false,
//toolbarFloatOffset:10,
toolbar: [
'title',
'bold',
'italic',
'underline',
'strikethrough',
'fontScale',
'color',
'ol',
'ul',
'blockquote',
'code',
'table',
'alignment',
'hr',
],
codeLanguages:[
{ name: 'CSS', value: 'css' },
{ name: 'Erlang', value: 'erlang' },
{ name: 'Less', value: 'less' },
{ name: 'Sass', value: 'sass' },
{ name: 'Diff', value: 'diff' },
{ name: 'HTML,XML', value: 'html' },
{ name: 'JSON', value: 'json' },
{ name: 'Java', value: 'java' },
{ name: 'JavaScript', value: 'js' },
{ name: 'PHP', value: 'php' },
{ name: 'SQL', value: 'sql'}
]
});
this.editor.setValue(this.state.data.monitorDescription);
this.editor.on("valuechanged", (e, src) => {
//console.log("valuechanged");
let desc = this.editor.getValue();
if(this.state.data.monitorDescription == desc ){
// console.log("valuechanged-1");
}else{
// console.log("valuechanged-2");
let data= this.state.data;
data.monitorDescription = desc;
this.setState({
data:data
});
}
// console.log(desc);
});
}
render() {
const {getFieldDecorator, getFieldError, isFieldValidating} = this.props.form;
// 初始化form表单的值
this.state.data = this.props.init.data;
// 判断 改编后赋值与改变前是否一致。一致则跳出
if(this.editor !==undefined ){
let desc = this.editor.getValue();
if(this.state.data.Description !== desc){
this.editor.setValue(this.state.data.Description);
}
}
return (
);
}