先引入wangeditor写评论功能
import React, { useEffect, useState, useRef, forwardRef, useImperativeHandle } from 'react';
import '@wangeditor/editor/dist/css/style.css';
import { Editor, Toolbar } from '@wangeditor/editor-for-react';
import { Button, Card, Col, Form, List, Row, Select, Tag, message, Mentions } from 'antd';
import { wsPost, wsGet } from '@models/BaseModel';
import { ListItemDataType, fakeList } from '../../List';
import { LikeOutlined, LoadingOutlined, MessageOutlined, StarOutlined } from '@ant-design/icons';
import ArticleListContent from '../../ArticleListContent/index';
import './style.less';
import closeImg from '../../../../image/close.svg';
// import { createToolbar } from '@wangeditor/editor/dist/editor/src';
import { position, offset } from 'caret-pos';
import { IDomEditor, DomEditor, IModalMenu, SlateNode, Boot } from '@wangeditor/editor';
import mentionModule, { MentionElement } from '@wangeditor/plugin-mention';
import PersonModal from './personModal';
// Extend menu
const IconText = ({ type, text }) => {
switch (type) {
case 'star-o':
return (
{ marginRight: 8 }} />
{text}
);
case 'like-o':
return (
{ marginRight: 8 }} />
{text}
);
case 'message':
return (
{ marginRight: 8 }} />
{text}
);
default:
return null;
}
};
const Comments = forwardRef((props, CommentRef) => {
const [editor, setEditor] = useState(null); // 存储 editor 实例
const [html, setHtml] = useState();
const [loading, setLoading] = useState(true);
const [commentVis, setCommentVis] = useState(false);
const [commentParentId, setCommentParentId] = useState('0'); //父级id
const [messageApi, contextHolder] = message.useMessage();
const [buttonLoading, setButtonLoading] = useState(false);
const [personList, setPersonList] = useState([]); //人员
const [isModalVisible, setIsModalVisible] = useState(false);
const formRef = useRef();
// const toolbar = DomEditor.getToolbar(editor)
// const curToolbarConfig = toolbar.getConfig()
// console.log( curToolbarConfig.toolbarKeys )
useImperativeHandle(CommentRef, () => ({
closeHand: () => {
if (editor == null) return;
setCommentVis(false);
editor.clear();
setEditor(null);
},
}));
const withAttachment = editor => {
const { isInline, isVoid } = editor;
const newEditor = editor;
newEditor.isInline = elem => {
const type = DomEditor.getNodeType(elem);
if (type === 'attachment') return true; // 针对 type: attachment ,设置为 inline
return isInline(elem);
};
return newEditor;
};
useEffect(() => {
console.log(props.dataList, props.type, 'nbsp');
}, [props.dataList]);
useEffect(() => {
Boot.registerPlugin(withAttachment);
Boot.registerModule(mentionModule);
wsGet({
url: '/api/problem/getUsers',
handler: res => {
const { code, data, msg } = res;
switch (code) {
case 20000: {
setPersonList(data);
break;
}
default:
message.error(msg);
break;
}
},
});
}, []);
const toolbarConfig = {
toolbarKeys: [
'bold',
'underline',
'italic',
// 'emotion',
{
key: 'group-image', // 必填,要以 group 开头
title: '图片', // 必填
iconSvg:
'', // 可选
menuKeys: ['uploadImage'], // 下级菜单 key ,必填
},
{
key: 'group-video', // 必填,要以 group 开头
title: '视频', // 必填
iconSvg:
'', // 可选
menuKeys: ['uploadVideo'], // 下级菜单 key ,必填
},
'codeBlock',
],
};
const editorConfig = {
placeholder: '请输入内容...',
MENU_CONF: {
uploadImage: {
server: '/api/problem/uploadimag',
fieldName: 'files',
maxFileSize: 20 * 1024 * 1024,
meta: {
ifToken: '1',
},
metaWithUrl: true,
headers: {
token: localStorage.getItem('X-Auth-Token'),
},
onBeforeUpload() {
setButtonLoading(true);
message.loading({
content: '上传中',
duration: 0,
});
},
onSuccess(file, res) {
setButtonLoading(false);
message.destroy();
console.log(`${file.name} 上传成功`, res);
},
onError(file, err, res) {
// console.log(`${file.name} 上传出错`, err, res)
message.error(res.msg);
},
customInsert(res, insertFn) {
console.log(res);
// 从 res 中找到 url alt href ,然后插入图片
insertFn(res.data[0].filePath, res.data[0].fileName, res.data[0].filePath);
},
},
uploadVideo: {
server: '/api/problem/uploadimag',
fieldName: 'files',
maxFileSize: 200 * 1024 * 1024,
meta: {
ifToken: '1',
},
metaWithUrl: true,
headers: {
token: localStorage.getItem('X-Auth-Token'),
},
timeout: 15 * 1000,
onBeforeUpload() {
console.log(messageApi, 'shipinzou');
setButtonLoading(true);
message.loading({
content: '上传中',
duration: 0,
});
},
onSuccess(file, res) {
setButtonLoading(false);
message.destroy();
console.log(`${file.name} 上传成功`, res);
},
onError(file, err, res) {
// console.log(`${file.name} 上传出错`, err, res)
message.error(res.msg);
},
customInsert(res, insertFn) {
console.log(res);
// 从 res 中找到 url alt href ,然后插入图片
insertFn(res.data[0].filePath, res.data[0].fileName, res.data[0].filePath);
},
},
},
EXTEND_CONF: {
mentionConfig: {
showModal, // 必须
hideModal, // 必须
},
},
};
function showModal(editor) {
// 获取光标位置,定位 modal
const domSelection = document.getSelection();
const domRange = domSelection.getRangeAt(0);
if (domRange == null) return;
const selectionRect = domRange.getBoundingClientRect();
// 获取编辑区域 DOM 节点的位置,以辅助定位
const containerRect = editor.getEditableContainer().getBoundingClientRect();
// 显示 modal 弹框,并定位
// PS:modal 需要自定义,如 或 Vue React 组件
setIsModalVisible(true);
console.log(selectionRect, containerRect, '展示');
// 当触发某事件(如点击一个按钮)时,插入 mention 节点
}
function insertMention(id, name) {
const mentionNode = {
type: 'mention', // 必须是 'mention'
value: name, // 文本
info: { id }, // 其他信息,自定义
children: [{ text: '' }], // 必须有一个空 text 作为 children
};
editor.restoreSelection(); // 恢复选区
editor.deleteBackward('character'); // 删除 '@'
editor.insertNode(mentionNode); // 插入 mention
editor.move(1); // 移动光标
}
function hideModal(editor) {
setIsModalVisible(false);
console.log(editor, '隐藏');
// 隐藏 modal
}
// 及时销毁 editor
useEffect(() => {
return () => {
if (editor == null) return;
editor.destroy();
// editor.MENU_CONF['uploadImage'] =
setEditor(null);
};
}, [editor]);
function extractDataInfoValues(inputString) {
const regex = /data-info="([^"]*)"/g;
const dataInfoValues = [];
let match;
while ((match = regex.exec(inputString)) !== null) {
const decodedValue = decodeURIComponent(match[1]);
dataInfoValues.push(JSON.parse(decodedValue).id);
}
return dataInfoValues;
}
function handleText() {
// console.log(editor.getHtml(), html, editor.getText(), 'sdsdsds');
const ids = extractDataInfoValues(html);
if (editor.isEmpty()) {
message.error('内容不可为空');
return;
}
let commentType = '';
switch (props.type) {
case '1':
commentType = 'reason';
break;
case '2':
commentType = 'tempProject';
break;
case '3':
commentType = 'longProject';
break;
case '4':
commentType = 'validateProject';
break;
case '5':
commentType = 'validateSummary';
break;
case '6':
commentType = 'reviewRecords';
break;
case '7':
commentType = 'proConclution';
break;
}
let param = {
commentType: commentType,
content: html,
problemId: props.id,
parentId: commentParentId,
ids: ids,
};
wsPost({
url: '/api/problem/insertComment',
data: param,
handler: res => {
const { code, data, msg } = res;
switch (code) {
case 20000: {
if (editor == null) return;
editor.clear();
setCommentVis(false);
message.success('新增成功');
props.getQuery();
break;
}
default:
message.error(msg);
break;
}
},
});
}
function extractContent(inputString, startSymbol, endSymbol) {
const regex = new RegExp(`${startSymbol}(.*?)${endSymbol}(?!\\S)`, 'g');
const matches = inputString.matchAll(regex);
const result = Array.from(matches, match => match[1]);
return result;
}
function printHtml() {
if (editor == null) return;
}
const addCommpent = id => {
setCommentParentId(id);
setCommentVis(true);
};
const handleClose = () => {
setCommentVis(false);
editor.clear();
setEditor(null);
};
const changeEditor = editor => {
setHtml(editor.getHtml()), console.log(editor.getHtml(), editor.getText(), 'xiugai');
};
return (
<>
{commentVis && { width: '100%', height: '350px' }} />}
{commentVis && (
{ border: '1px solid #ccc', zIndex: 100, marginTop: '15px', position: 'fixed', bottom: '0', width: 'calc(100vw - 250px)', minHeight: '300px' }}>
{ position: 'absolute', right: '10px', bottom: '10px', zIndex: 2 }}>
{ borderBottom: '1px solid #ccc' }} />
{
changeEditor(editor);
}}
mode="default"
style={{ height: '300px' }}
/>
{isModalVisible && }
)}
{/* { marginTop: '15px' }}>{html} */}
{/* 渲染html */}
{/* {__html: `'hello world.




'`}}>
*/}
>
);
});
export default Comments;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
评论递归ArticleListContent,jsx
import { Avatar, List, Space, Card } from 'antd';
import React, { useEffect, useState } from 'react';
import moment from 'moment';
import './index.less';
import { fakeList } from '../List';
import { LikeOutlined, LoadingOutlined, MessageOutlined, StarOutlined } from '@ant-design/icons';
const getMarginLeftNum = num => {
return 30 * num;
};
const GetContent = props => {
const [loading, setLoading] = useState(false);
const IconText = ({ icon, text, id, num }) => {
if (num <= 1) {
return (
{
props.addCommpent(id);
}}
>
{React.createElement(icon)}
{text}
);
}
return ;
};
console.log(props.num, 'props.num');
return (
{props.item.map((o, index) => {
return (
{ marginLeft: getMarginLeftNum(props.num + 1) }}>
(
]}>
{ __html: item.content }} />
{moment(item.createDate).format('YYYY-MM-DD HH:mm')}
)}
/>
{o.children && }
);
})}
);
};
const ArticleListContent = props => {
const [loading, setLoading] = useState(false);
const IconText = ({ icon, text, id }) => (
{
props.addCommpent(id);
}}
>
{React.createElement(icon)}
{text}
);
return (
{ minHeight: '200px' }}>
{!props.data && { fontSize: '18px', fontWeight: '500', color: '#8d8989', display: 'flex', alignItems: 'center', justifyContent: 'center', paddingTop: '50px' }}>暂无内容}
{props.data &&
props.data.map((item, index) => {
item, 'item';
if (!item.children) {
return (
(
]}>
{ __html: item.content }} />
{moment(item.createDate).format('YYYY-MM-DD HH:mm')}
)}
/>
);
}
return (
(
]}>
{ __html: item.content }} />
{moment(item.createDate).format('YYYY-MM-DD HH:mm')}
)}
/>
{item.children && }
);
})}
);
};
export default ArticleListContent;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
@功能自定义的组件 personModal.jsx
import { Modal, Form, Input, Select, message } from 'antd';
import { ModalForm, ProFormTextArea } from '@ant-design/pro-components';
import { wsPost, wsGet } from '@models/BaseModel';
import React, { ReactDOM, useEffect, useRef, useState } from 'react';
const { Option } = Select;
export default function CsModal(props) {
const selectRef = useRef();
const [personList, setPersonList] = useState([]); //人员
const [topPosition, setTopPosition] = useState('');
const [leftPosition, setLeftPosition] = useState('');
useEffect(() => {
// 获取光标位置
const domSelection = document.getSelection();
const domRange = domSelection?.getRangeAt(0);
if (domRange == null) return;
const rect = document.getElementById('editcontent').getBoundingClientRect();
const rect1 = domRange.getBoundingClientRect();
// // 定位 modal
console.log(rect, rect1, 'top left');
setTopPosition(`${rect1.top - rect.top - 5}px`);
setLeftPosition(`${rect1.left - rect.left + 10}px`);
// focus input
selectRef.current.focus();
wsGet({
url: '/api/problem/getUsers',
handler: res => {
const { code, data, msg } = res;
switch (code) {
case 20000: {
setPersonList(data);
break;
}
default:
message.error(msg);
break;
}
},
});
}, []);
const onChangeSelect = e => {
let name = personList.find(item => item.externalId === e);
props.insertMention(e, name.name);
props.hideModal();
};
return (
);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
实现效果


-
相关阅读:
ElasticSearch8 - 基础概念和映射
Linux学习笔记——FTP服务器的使用
Nuttx学习笔记(一)
大学生简单个人静态HTML网页设计作品 HTML+CSS制作我的家乡杭州 DIV布局个人介绍网页模板代码 DW学生个人网站制作成品下载 HTML5期末大作业
docker compose 修改masterLab上传限制问题
Python练习之列表
SpringBoot 整合WebFlux
基于JAVA汽车站车辆运管系统计算机毕业设计源码+数据库+lw文档+系统+部署
Echart基础入门、知识点全总结、自适应,在 Vue项目中使用
华为认证 | 安全HCIP和数通HCIP,该怎么选?
-
原文地址:https://blog.csdn.net/qq_44295192/article/details/132608378