张延森 4 年前
父节点
当前提交
c079a537b9
共有 5 个文件被更改,包括 156 次插入9 次删除
  1. 7
    0
      config/config.js
  2. 2
    1
      package.json
  3. 115
    0
      src/components/Wangedit/Wangedit.jsx
  4. 21
    1
      src/pages/UserManage/Editor/User.jsx
  5. 11
    7
      src/pages/UserManage/Editor/index.jsx

+ 7
- 0
config/config.js 查看文件

@@ -3,6 +3,9 @@ import routes from './routes';
3 3
 
4 4
 import slash from 'slash2';
5 5
 import webpackPlugin from './plugin.config';
6
+
7
+const path = require('path');
8
+
6 9
 const { pwa, primaryColor } = defaultSettings; // preview.pro.ant.design only do not use in your production ;
7 10
 // preview.pro.ant.design 专用环境变量,请不要在你的项目中使用它。
8 11
 
@@ -131,6 +134,10 @@ export default {
131 134
   },
132 135
   chainWebpack: webpackPlugin,
133 136
 
137
+  alias: {
138
+    '@/': path.resolve(__dirname, 'src/')
139
+  },
140
+
134 141
   proxy: {
135 142
     '/api': {
136 143
       target: 'http://localhost:8080/',

+ 2
- 1
package.json 查看文件

@@ -61,7 +61,8 @@
61 61
     "umi": "^2.8.7",
62 62
     "umi-plugin-pro-block": "^1.3.2",
63 63
     "umi-plugin-react": "^1.9.5",
64
-    "umi-request": "^1.0.8"
64
+    "umi-request": "^1.0.8",
65
+    "wangeditor": "^4.2.1"
65 66
   },
66 67
   "devDependencies": {
67 68
     "@ant-design/colors": "^3.1.0",

+ 115
- 0
src/components/Wangedit/Wangedit.jsx 查看文件

@@ -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
+

+ 21
- 1
src/pages/UserManage/Editor/User.jsx 查看文件

@@ -7,6 +7,7 @@ import XForm, { FieldTypes } from '../../../components/XForm';
7 7
 import { fetch, apis } from '../../../utils/request';
8 8
 import SelectCity from '../CitySelect';
9 9
 import ImageUpload from '../../../components/uploadImage/ImageUpload';
10
+import Wangedit from '@/components/Wangedit/Wangedit';
10 11
 
11 12
 
12 13
 const defaultPass = 'abc@123';
@@ -160,6 +161,20 @@ export default props => {
160 161
       ],
161 162
       value: user.orgNum,
162 163
     },
164
+
165
+    {
166
+      label: '公司封面',
167
+      name: 'orgThumb',
168
+      type: FieldTypes.ImageUploader,
169
+      value: user.orgThumb,
170
+    },
171
+
172
+    {
173
+      label: '公司简介',
174
+      name: 'orgDesc',
175
+      value: user.orgDesc,
176
+      render: <Wangedit />
177
+    },
163 178
   ]
164 179
 
165 180
   const handleSubmit = val => {
@@ -206,5 +221,10 @@ export default props => {
206 221
   }
207 222
 
208 223
   return (
209
-  <><Spin spinning={ submitting } size="large"><XForm onSubmit={handleSubmit} onCancel={handleCancel} fields={fields} submitting={submitting}></XForm></Spin></>)
224
+    <>
225
+      <Spin spinning={ submitting } size="large">
226
+        <XForm onSubmit={handleSubmit} onCancel={handleCancel} fields={fields} submitting={submitting}></XForm>
227
+      </Spin>
228
+    </>
229
+  )
210 230
 }

+ 11
- 7
src/pages/UserManage/Editor/index.jsx 查看文件

@@ -1,6 +1,6 @@
1 1
 import React, { useState, useEffect } from 'react';
2 2
 import { PageHeaderWrapper } from '@ant-design/pro-layout';
3
-import { Radio, Button } from 'antd';
3
+import { Radio, Button, Spin } from 'antd';
4 4
 import { router } from 'umi';
5 5
 import User from './User';
6 6
 import MiniApp from './Miniapp';
@@ -66,12 +66,16 @@ export default (props) => {
66 66
           <Radio.Button value={4}>特殊配置</Radio.Button>
67 67
         </Radio.Group>
68 68
       </div>
69
-      <div style={{ width: '800px', margin: '0 auto' }}>
70
-        { tab === 1 ? <User data={user} {...props} onChange={handleChangeUser} onCancel={handleCancel(1)}/> : null}
71
-        { tab === 2 ? <MiniApp data={user} tplTyps={tplTyps} {...props} onChange={handleChangeUser} onCancel={handleCancel(2)}/> : null}
72
-        { tab === 3 ? <Menus data={user} {...props} onCancel={handleCancel(3)}/> : null}
73
-        { tab === 4 ? <Advanced data={user} {...props} onCancel={handleCancel(4)}/> : null}
74
-      </div>
69
+      {
70
+        user.userId && (
71
+          <div style={{ width: '800px', margin: '0 auto' }}>
72
+            { tab === 1 ? <User data={user} {...props} onChange={handleChangeUser} onCancel={handleCancel(1)}/> : null}
73
+            { tab === 2 ? <MiniApp data={user} tplTyps={tplTyps} {...props} onChange={handleChangeUser} onCancel={handleCancel(2)}/> : null}
74
+            { tab === 3 ? <Menus data={user} {...props} onCancel={handleCancel(3)}/> : null}
75
+            { tab === 4 ? <Advanced data={user} {...props} onCancel={handleCancel(4)}/> : null}
76
+          </div>
77
+        )
78
+      }
75 79
     </PageHeaderWrapper>
76 80
   );
77 81
 }