|
@@ -11,82 +11,74 @@ class Wangedit extends React.Component {
|
11
|
11
|
super(props, context);
|
12
|
12
|
this.state = {
|
13
|
13
|
html: undefined,
|
14
|
|
- contenteditable: props.contenteditable == false ? false : true
|
15
|
|
- }
|
|
14
|
+ contenteditable: props.contenteditable == false ? false : true,
|
|
15
|
+ };
|
16
|
16
|
this.editor = undefined;
|
17
|
17
|
}
|
18
|
18
|
|
19
|
|
- render() {
|
20
|
|
- return (
|
21
|
|
- <div ref="editorElem" style={{ textAlign: 'left' }}>
|
22
|
|
- </div>
|
23
|
|
- );
|
24
|
|
- }
|
25
|
|
-
|
26
|
19
|
componentDidMount() {
|
27
|
|
- const elem = this.refs.editorElem
|
28
|
|
- this.editor = new E(elem)
|
|
20
|
+ const elem = this.refs.editorElem;
|
|
21
|
+ this.editor = new E(elem);
|
29
|
22
|
// 使用 onchange 函数监听内容的变化
|
30
|
23
|
this.editor.customConfig.onchange = html => {
|
31
|
|
- this.setState({ html })
|
|
24
|
+ this.setState({ html });
|
32
|
25
|
|
33
|
26
|
if (typeof this.props.onChange === 'function') {
|
34
|
|
- this.props.onChange(html)
|
|
27
|
+ this.props.onChange(html);
|
35
|
28
|
}
|
36
|
|
- }
|
37
|
|
- this.editor.customConfig.zIndex = 100
|
38
|
|
- this.editor.customConfig.uploadImgMaxLength = 1
|
39
|
|
- this.editor.customConfig.customUploadImg = function (files, insert) {
|
40
|
|
- if (!files.length) return
|
41
|
|
-
|
42
|
|
- const data = new FormData()
|
43
|
|
- data.append('file', files[0])
|
|
29
|
+ };
|
|
30
|
+ this.editor.customConfig.zIndex = 100;
|
|
31
|
+ this.editor.customConfig.uploadImgMaxLength = 1;
|
|
32
|
+ this.editor.customConfig.customUploadImg = function(files, insert) {
|
|
33
|
+ if (!files.length) return;
|
44
|
34
|
|
45
|
|
- fetch(apis.image.upload)({data}).then(insert)
|
46
|
|
- }
|
|
35
|
+ const data = new FormData();
|
|
36
|
+ data.append('file', files[0]);
|
|
37
|
+
|
|
38
|
+ fetch(apis.image.upload)({ data }).then(insert);
|
|
39
|
+ };
|
47
|
40
|
this.editor.customConfig.menus = [
|
48
|
|
- 'head', // 标题
|
49
|
|
- 'bold', // 粗体
|
50
|
|
- 'fontSize', // 字号
|
51
|
|
- 'fontName', // 字体
|
52
|
|
- 'italic', // 斜体
|
53
|
|
- 'underline', // 下划线
|
54
|
|
- 'strikeThrough', // 删除线
|
55
|
|
- 'foreColor', // 文字颜色
|
56
|
|
- 'backColor', // 背景颜色
|
57
|
|
- 'list', // 列表
|
58
|
|
- 'justify', // 对齐方式
|
59
|
|
- 'quote', // 引用
|
60
|
|
- 'image', // 插入图片
|
61
|
|
- 'undo', // 撤销
|
62
|
|
- 'redo' // 重复
|
63
|
|
- ]
|
64
|
|
-
|
|
41
|
+ 'head', // 标题
|
|
42
|
+ 'bold', // 粗体
|
|
43
|
+ 'fontSize', // 字号
|
|
44
|
+ 'fontName', // 字体
|
|
45
|
+ 'italic', // 斜体
|
|
46
|
+ 'underline', // 下划线
|
|
47
|
+ 'strikeThrough', // 删除线
|
|
48
|
+ 'foreColor', // 文字颜色
|
|
49
|
+ 'backColor', // 背景颜色
|
|
50
|
+ 'list', // 列表
|
|
51
|
+ 'justify', // 对齐方式
|
|
52
|
+ 'quote', // 引用
|
|
53
|
+ 'image', // 插入图片
|
|
54
|
+ 'undo', // 撤销
|
|
55
|
+ 'redo', // 重复
|
|
56
|
+ ];
|
|
57
|
+
|
65
|
58
|
// 过滤 word 字符
|
66
|
|
- this.editor.customConfig.pasteFilterStyle = false
|
|
59
|
+ this.editor.customConfig.pasteFilterStyle = false;
|
67
|
60
|
this.editor.customConfig.pasteTextHandle = function(content) {
|
68
|
61
|
const regs = [
|
69
|
|
- /<!--\[if [\s\S]*?endif\]-->/ig,
|
70
|
|
- /<[a-zA-Z0-9]+\:[^>]+>[^>]*<\/[a-zA-Z0-9]+\:[^>]+>/ig,
|
71
|
|
- /<[a-zA-Z0-9]+\:[^>]+\/>/ig,
|
72
|
|
- /<style>[\s\S]*?<\/style>/ig,
|
73
|
|
- ]
|
|
62
|
+ /<!--\[if [\s\S]*?endif\]-->/gi,
|
|
63
|
+ /<[a-zA-Z0-9]+\:[^>]+>[^>]*<\/[a-zA-Z0-9]+\:[^>]+>/gi,
|
|
64
|
+ /<[a-zA-Z0-9]+\:[^>]+\/>/gi,
|
|
65
|
+ /<style>[\s\S]*?<\/style>/gi,
|
|
66
|
+ new RegExp('\u2029', 'ig'), // 替换word分隔符 序号 8233
|
|
67
|
+ ];
|
74
|
68
|
|
75
|
|
- return regs.reduce((acc, reg) => {
|
76
|
|
- return acc.replace(reg, '')
|
77
|
|
- }, content)
|
78
|
|
- }
|
|
69
|
+ return regs.reduce((acc, reg) => acc.replace(reg, ''), content);
|
|
70
|
+ };
|
79
|
71
|
|
80
|
|
- this.editor.create()
|
81
|
|
- this.editor.$textElem.attr('contenteditable',this.state.contenteditable);
|
82
|
|
- this.editor.customConfig.uploadImgShowBase64 = true
|
83
|
|
- this.editor.txt.html(this.props.value)
|
|
72
|
+ this.editor.create();
|
|
73
|
+ this.editor.$textElem.attr('contenteditable', this.state.contenteditable);
|
|
74
|
+ this.editor.customConfig.uploadImgShowBase64 = true;
|
|
75
|
+ this.editor.txt.html(this.props.value);
|
84
|
76
|
}
|
85
|
77
|
|
86
|
78
|
componentDidUpdate(props, state) {
|
87
|
79
|
if (this.props.value && !state.html) {
|
88
|
80
|
if (this.editor) {
|
89
|
|
- this.editor.txt.html(this.props.value)
|
|
81
|
+ this.editor.txt.html(this.props.value);
|
90
|
82
|
}
|
91
|
83
|
}
|
92
|
84
|
}
|
|
@@ -100,9 +92,12 @@ class Wangedit extends React.Component {
|
100
|
92
|
* @memberof Wangedit
|
101
|
93
|
*/
|
102
|
94
|
shouldComponentUpdate(nextProps) {
|
103
|
|
- return nextProps.value !== this.editor.txt.html()
|
|
95
|
+ return nextProps.value !== this.editor.txt.html();
|
104
|
96
|
}
|
105
|
|
-}
|
106
|
97
|
|
107
|
|
-export default Wangedit
|
|
98
|
+ render() {
|
|
99
|
+ return <div ref="editorElem" style={{ textAlign: 'left' }}></div>;
|
|
100
|
+ }
|
|
101
|
+}
|
108
|
102
|
|
|
103
|
+export default Wangedit;
|