Skip to content

Commit 4fd2c44

Browse files
committed
improve&refactor: abstract the common chat module and improve scrollToBottom fn usage
1 parent bee0bef commit 4fd2c44

File tree

5 files changed

+65
-47
lines changed

5 files changed

+65
-47
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
### 项目展示:
99

10-
github对gif图有限制,我就直接截图了,具体详情建议直接[线上体验](https://im.aermin.top)给个star呀~
10+
github对gif图有限制,我就直接截图了,具体详情建议直接[线上体验](https://im.aermin.top)***可以的话给个star支持一下*** 😄
1111

1212
![image](https://user-images.githubusercontent.com/24861316/53351929-e1d33300-395c-11e9-84a9-0a9fd793b5a1.png)
1313

@@ -153,6 +153,8 @@ ps: 本地发表情和发文件和github登录无法使用,需要自己去gith
153153
- [x] 发表情
154154
- [x] 发文件
155155
- [x] 下载文件
156+
- [ ] 图片方法查看
157+
- [ ] @某人/Enter快捷键发送信息
156158
- [ ] 支持Markdown
157159
- [ ] 支持Quote
158160

src/components/ChatItem/style.scss

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
margin: 36px 0;
66
.image-render {
77
background-color: #fff !important;
8+
max-width: 80%;
89
img {
910
max-width: 80%;
1011
max-height: 80%;
1112
}
1213
}
14+
.msg-render {
15+
max-width: 50%;
16+
}
1317
.other-file-render {
1418
font-size: 16px;
1519
color: $blue;
@@ -19,7 +23,6 @@
1923
}
2024
}
2125
.msg-render {
22-
max-width: 60%;
2326
font-size: 14px;
2427
word-wrap:break-word;
2528
.invitingCard {
@@ -93,8 +96,8 @@
9396
background-color: $my-msg-color;
9497
border-radius: 6px;
9598
}
96-
.image-render img {
99+
.image-render img {
97100
float: right;
98-
}
101+
}
99102
}
100103
}

src/components/GroupChat/index.js

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Modal from '../Modal';
1212
import './style.scss';
1313
import PersonalInfo from '../PersonalInfo';
1414
import notification from '../Notification';
15+
import Chat from '../../modules/Chat';
1516

1617
class GroupChat extends Component {
1718
constructor() {
@@ -25,6 +26,7 @@ class GroupChat extends Component {
2526
personalInfo: {},
2627
visible: false,
2728
};
29+
this._chat = new Chat();
2830
}
2931

3032
sendMessage = (inputMsg = '', attachments = []) => {
@@ -49,20 +51,11 @@ class GroupChat extends Component {
4951
};
5052
this._sendByMe = true;
5153
window.socket.emit('sendGroupMsg', data);
52-
this.scrollToBottom();
54+
this._chat.scrollToBottom();
5355
updateAllChatContent({ allChatContent, newChatContent: data, action: 'send' });
5456
updateHomePageList({ data, homePageList, myUserId: userId });
5557
}
5658

57-
scrollToBottom(time = 0) {
58-
const ulDom = document.getElementsByClassName('chat-content-list')[0];
59-
if (ulDom) {
60-
setTimeout(() => {
61-
ulDom.scrollTop = ulDom.scrollHeight + 10000;
62-
}, time);
63-
}
64-
}
65-
6659
joinGroup = () => {
6760
const { userId } = this._userInfo;
6861
const {
@@ -106,11 +99,6 @@ class GroupChat extends Component {
10699
this.setState({ showGroupChatInfo: value });
107100
}
108101

109-
clearUnreadHandle() {
110-
const { homePageList, clearUnread, chatId } = this.props;
111-
clearUnread({ homePageList, chatFromId: chatId });
112-
}
113-
114102
shouldComponentUpdate(nextProps, nextState) {
115103
const { relatedCurrentChat, chatId } = nextProps;
116104
if (relatedCurrentChat || chatId !== this.props.chatId || this._sendByMe) {
@@ -127,19 +115,6 @@ class GroupChat extends Component {
127115
return false;
128116
}
129117

130-
componentDidMount() {
131-
const { allChatContent, chatId } = this.props;
132-
const chatItem = allChatContent.groupChat && allChatContent.groupChat.get(chatId);
133-
this.clearUnreadHandle();
134-
// (产品设计) 当查找没加过的群,点击去没群内容,请求出群内容,避免不了解而加错群
135-
if (!chatItem) {
136-
window.socket.emit('getOneGroupMsg', { groupId: chatId }, (groupMsgAndInfo) => {
137-
this.setState({ groupMsgAndInfo });
138-
});
139-
}
140-
this.scrollToBottom();
141-
}
142-
143118
_showPersonalInfo(value) {
144119
this.setState({ showPersonalInfo: value });
145120
}
@@ -157,6 +132,27 @@ class GroupChat extends Component {
157132
});
158133
}
159134

135+
componentDidMount() {
136+
const {
137+
allChatContent, homePageList, clearUnread, chatId
138+
} = this.props;
139+
const chatItem = allChatContent.groupChat && allChatContent.groupChat.get(chatId);
140+
this._chat.clearUnreadHandle({ homePageList, clearUnread, chatFromId: chatId });
141+
// (产品设计) 当查找没加过的群,点击去没群内容,请求出群内容,避免不了解而加错群
142+
if (!chatItem) {
143+
window.socket.emit('getOneGroupMsg', { groupId: chatId }, (groupMsgAndInfo) => {
144+
this.setState({ groupMsgAndInfo });
145+
});
146+
}
147+
this._chat.scrollToBottom();
148+
}
149+
150+
componentWillUpdate() {
151+
if (this._chat.isScrollInBottom) {
152+
this._chat.scrollToBottom();
153+
}
154+
}
155+
160156
render() {
161157
const { chatId, allChatContent } = this.props;
162158
const {

src/components/PrivateChat/index.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import InputArea from '../InputArea';
55
import ChatContentList from '../ChatContentList';
66
import PersonalInfo from '../PersonalInfo';
77
import notification from '../Notification';
8+
import Chat from '../../modules/Chat';
89

910
export default class PrivateChat extends Component {
1011
constructor() {
@@ -15,6 +16,7 @@ export default class PrivateChat extends Component {
1516
this.state = {
1617
showPersonalInfo: false
1718
};
19+
this._chat = new Chat();
1820
}
1921

2022
sendMessage = (inputMsg = '', attachments = []) => {
@@ -38,7 +40,7 @@ export default class PrivateChat extends Component {
3840
};
3941
this._sendByMe = true;
4042
window.socket.emit('sendPrivateMsg', data);
41-
this.scrollToBottom();
43+
this._chat.scrollToBottom();
4244
updateAllChatContent({ allChatContent, newChatContent: data, action: 'send' });
4345
const dataForHomePage = { ...data, name: location.search.split('=')[1] };
4446
updateHomePageList({ data: dataForHomePage, homePageList, myUserId: userId });
@@ -66,27 +68,20 @@ export default class PrivateChat extends Component {
6668
});
6769
}
6870

69-
scrollToBottom(time = 0) {
70-
const ulDom = document.getElementsByClassName('chat-content-list')[0];
71-
if (ulDom) {
72-
setTimeout(() => {
73-
ulDom.scrollTop = ulDom.scrollHeight + 10000;
74-
}, time);
75-
}
76-
}
77-
7871
_showPersonalInfo(value) {
7972
this.setState({ showPersonalInfo: value });
8073
}
8174

82-
clearUnreadHandle() {
75+
componentDidMount() {
8376
const { homePageList, clearUnread, chatId } = this.props;
84-
clearUnread({ homePageList, chatFromId: chatId });
77+
this._chat.clearUnreadHandle({ homePageList, clearUnread, chatFromId: chatId });
78+
this._chat.scrollToBottom();
8579
}
8680

87-
componentDidMount() {
88-
this.clearUnreadHandle();
89-
this.scrollToBottom();
81+
componentWillUpdate() {
82+
if (this._chat.isScrollInBottom) {
83+
this._chat.scrollToBottom();
84+
}
9085
}
9186

9287
shouldComponentUpdate(nextProps, nextState) {

src/modules/Chat/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default class Chat {
2+
scrollToBottom(time = 0) {
3+
const ulDom = document.getElementsByClassName('chat-content-list')[0];
4+
if (ulDom) {
5+
setTimeout(() => {
6+
ulDom.scrollTop = ulDom.scrollHeight + 10000;
7+
}, time);
8+
}
9+
}
10+
11+
clearUnreadHandle({ homePageList, clearUnread, chatId }) {
12+
clearUnread({ homePageList, chatFromId: chatId });
13+
}
14+
15+
get isScrollInBottom() {
16+
const ulDom = document.getElementsByClassName('chat-content-list')[0];
17+
if (ulDom) {
18+
return (ulDom.scrollTop + ulDom.clientHeight) === ulDom.scrollHeight;
19+
}
20+
return false;
21+
}
22+
}

0 commit comments

Comments
 (0)