|
@@ -1,5 +1,7 @@
|
1
|
1
|
import React from 'react';
|
2
|
2
|
import E from 'wangeditor';
|
|
3
|
+import PreviewMenu from './PreviewMenu'
|
|
4
|
+import Preview from './Preview'
|
3
|
5
|
import { fetch, apis } from '../../utils/request';
|
4
|
6
|
|
5
|
7
|
/**
|
|
@@ -10,6 +12,7 @@ class Wangedit extends React.Component {
|
10
|
12
|
constructor(props, context) {
|
11
|
13
|
super(props, context);
|
12
|
14
|
this.state = {
|
|
15
|
+ preview: false,
|
13
|
16
|
html: undefined,
|
14
|
17
|
contenteditable: props.contenteditable == false ? false : true
|
15
|
18
|
}
|
|
@@ -18,8 +21,10 @@ class Wangedit extends React.Component {
|
18
|
21
|
|
19
|
22
|
render() {
|
20
|
23
|
return (
|
21
|
|
- <div ref="editorElem" style={{ textAlign: 'left' }}>
|
22
|
|
- </div>
|
|
24
|
+ <>
|
|
25
|
+ <div ref="editorElem" style={{ textAlign: 'left' }} />
|
|
26
|
+ <Preview width={426} style={{width: '426px', height: '863px', margin: 0, padding: 0}} visible={this.state.preview} html={this.state.html} onCancel={() => this.setState({preview: false})} />
|
|
27
|
+ </>
|
23
|
28
|
);
|
24
|
29
|
}
|
25
|
30
|
|
|
@@ -27,24 +32,31 @@ class Wangedit extends React.Component {
|
27
|
32
|
const elem = this.refs.editorElem
|
28
|
33
|
this.editor = new E(elem)
|
29
|
34
|
// 使用 onchange 函数监听内容的变化
|
30
|
|
- this.editor.customConfig.onchange = html => {
|
|
35
|
+ this.editor.config.onchange = html => {
|
31
|
36
|
this.setState({ html })
|
32
|
37
|
|
33
|
38
|
if (typeof this.props.onChange === 'function') {
|
34
|
39
|
this.props.onChange(html)
|
35
|
40
|
}
|
36
|
41
|
}
|
37
|
|
- this.editor.customConfig.zIndex = 100
|
38
|
|
- this.editor.customConfig.uploadImgMaxLength = 1
|
39
|
|
- this.editor.customConfig.customUploadImg = function (files, insert) {
|
|
42
|
+ this.editor.config.zIndex = 100
|
|
43
|
+ this.editor.config.uploadImgMaxLength = 1
|
|
44
|
+ this.editor.config.customUploadImg = function (files, insert) {
|
40
|
45
|
if (!files.length) return
|
41
|
46
|
|
42
|
47
|
const data = new FormData()
|
43
|
48
|
data.append('file', files[0])
|
44
|
49
|
|
45
|
50
|
fetch(apis.image.upload)({data}).then(insert)
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ // 扩展按钮
|
|
54
|
+ this.editor.menus.extend('previewMenu', PreviewMenu)
|
|
55
|
+ PreviewMenu.preview = (html) => {
|
|
56
|
+ this.setState({preview: true})
|
46
|
57
|
}
|
47
|
|
- this.editor.customConfig.menus = [
|
|
58
|
+
|
|
59
|
+ this.editor.config.menus = [
|
48
|
60
|
'head', // 标题
|
49
|
61
|
'bold', // 粗体
|
50
|
62
|
'fontSize', // 字号
|
|
@@ -59,12 +71,13 @@ class Wangedit extends React.Component {
|
59
|
71
|
'quote', // 引用
|
60
|
72
|
'image', // 插入图片
|
61
|
73
|
'undo', // 撤销
|
62
|
|
- 'redo' // 重复
|
|
74
|
+ 'redo', // 重复
|
|
75
|
+ 'previewMenu'
|
63
|
76
|
]
|
64
|
77
|
|
65
|
78
|
// 过滤 word 字符
|
66
|
|
- this.editor.customConfig.pasteFilterStyle = false
|
67
|
|
- this.editor.customConfig.pasteTextHandle = function(content) {
|
|
79
|
+ this.editor.config.pasteFilterStyle = false
|
|
80
|
+ this.editor.config.pasteTextHandle = function(content) {
|
68
|
81
|
const regs = [
|
69
|
82
|
/<!--\[if [\s\S]*?endif\]-->/ig,
|
70
|
83
|
/<[a-zA-Z0-9]+\:[^>]+>[^>]*<\/[a-zA-Z0-9]+\:[^>]+>/ig,
|
|
@@ -80,7 +93,7 @@ class Wangedit extends React.Component {
|
80
|
93
|
|
81
|
94
|
this.editor.create()
|
82
|
95
|
this.editor.$textElem.attr('contenteditable',this.state.contenteditable);
|
83
|
|
- this.editor.customConfig.uploadImgShowBase64 = true
|
|
96
|
+ this.editor.config.uploadImgShowBase64 = true
|
84
|
97
|
this.editor.txt.html(this.props.value)
|
85
|
98
|
}
|
86
|
99
|
|
|
@@ -101,7 +114,7 @@ class Wangedit extends React.Component {
|
101
|
114
|
* @memberof Wangedit
|
102
|
115
|
*/
|
103
|
116
|
shouldComponentUpdate(nextProps) {
|
104
|
|
- return nextProps.value !== this.editor.txt.html()
|
|
117
|
+ return nextProps.value !== this.editor.txt.html() || nextProps.preview !== this.state.preview
|
105
|
118
|
}
|
106
|
119
|
}
|
107
|
120
|
|