|
|
@@ -2,6 +2,14 @@ import React, { Component } from 'react';
|
|
|
import E from 'wangeditor'
|
|
|
|
|
|
class RichTextEditor extends Component {
|
|
|
+
|
|
|
+ constructor(props) {
|
|
|
+ super(props);
|
|
|
+ this.state = ({
|
|
|
+ _html: props.content
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
render() {
|
|
|
return (
|
|
|
<div className='richtexteditor' ref="editorElem"></div>
|
|
|
@@ -11,8 +19,16 @@ class RichTextEditor extends Component {
|
|
|
const { content, onContentChange } = this.props;
|
|
|
const elem = this.refs.editorElem;
|
|
|
const editor = new E(elem);
|
|
|
- editor.customConfig.onchange = onContentChange; // 使用 onchange 函数监听内容的变化
|
|
|
+ editor.customConfig.onchange = (html) => {
|
|
|
+ const { _html } = this.state;
|
|
|
+ if(_html !== html) { // 只有实际内容发生改变才触发model的修改
|
|
|
+ onContentChange(html);
|
|
|
+ }
|
|
|
+ }; // 使用 onchange 函数监听内容的变化
|
|
|
editor.customConfig.onfocus = () => {
|
|
|
+ this.setState({
|
|
|
+ _html: editor.txt.html()
|
|
|
+ });
|
|
|
let toolbar = elem.getElementsByClassName('w-e-toolbar')[0];
|
|
|
toolbar.style.display = 'flex';
|
|
|
}; // 获得焦点时显示工具栏(需要计算位置)
|
|
|
@@ -22,6 +38,7 @@ class RichTextEditor extends Component {
|
|
|
}
|
|
|
editor.customConfig.uploadImgShowBase64 = true; // 上传图片(base64)
|
|
|
editor.create()
|
|
|
+ // editor.blur();
|
|
|
editor.txt.html(content);
|
|
|
}
|
|
|
}
|