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