Skip to content

Commit

Permalink
improve&refactor: abstract the common chat module and improve scrollT…
Browse files Browse the repository at this point in the history
…oBottom fn usage
  • Loading branch information
aermin committed Feb 27, 2019
1 parent bee0bef commit 4fd2c44
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 47 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

### 项目展示:

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

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

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

Expand Down
9 changes: 6 additions & 3 deletions src/components/ChatItem/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
margin: 36px 0;
.image-render {
background-color: #fff !important;
max-width: 80%;
img {
max-width: 80%;
max-height: 80%;
}
}
.msg-render {
max-width: 50%;
}
.other-file-render {
font-size: 16px;
color: $blue;
Expand All @@ -19,7 +23,6 @@
}
}
.msg-render {
max-width: 60%;
font-size: 14px;
word-wrap:break-word;
.invitingCard {
Expand Down Expand Up @@ -93,8 +96,8 @@
background-color: $my-msg-color;
border-radius: 6px;
}
.image-render img {
.image-render img {
float: right;
}
}
}
}
52 changes: 24 additions & 28 deletions src/components/GroupChat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Modal from '../Modal';
import './style.scss';
import PersonalInfo from '../PersonalInfo';
import notification from '../Notification';
import Chat from '../../modules/Chat';

class GroupChat extends Component {
constructor() {
Expand All @@ -25,6 +26,7 @@ class GroupChat extends Component {
personalInfo: {},
visible: false,
};
this._chat = new Chat();
}

sendMessage = (inputMsg = '', attachments = []) => {
Expand All @@ -49,20 +51,11 @@ class GroupChat extends Component {
};
this._sendByMe = true;
window.socket.emit('sendGroupMsg', data);
this.scrollToBottom();
this._chat.scrollToBottom();
updateAllChatContent({ allChatContent, newChatContent: data, action: 'send' });
updateHomePageList({ data, homePageList, myUserId: userId });
}

scrollToBottom(time = 0) {
const ulDom = document.getElementsByClassName('chat-content-list')[0];
if (ulDom) {
setTimeout(() => {
ulDom.scrollTop = ulDom.scrollHeight + 10000;
}, time);
}
}

joinGroup = () => {
const { userId } = this._userInfo;
const {
Expand Down Expand Up @@ -106,11 +99,6 @@ class GroupChat extends Component {
this.setState({ showGroupChatInfo: value });
}

clearUnreadHandle() {
const { homePageList, clearUnread, chatId } = this.props;
clearUnread({ homePageList, chatFromId: chatId });
}

shouldComponentUpdate(nextProps, nextState) {
const { relatedCurrentChat, chatId } = nextProps;
if (relatedCurrentChat || chatId !== this.props.chatId || this._sendByMe) {
Expand All @@ -127,19 +115,6 @@ class GroupChat extends Component {
return false;
}

componentDidMount() {
const { allChatContent, chatId } = this.props;
const chatItem = allChatContent.groupChat && allChatContent.groupChat.get(chatId);
this.clearUnreadHandle();
// (产品设计) 当查找没加过的群,点击去没群内容,请求出群内容,避免不了解而加错群
if (!chatItem) {
window.socket.emit('getOneGroupMsg', { groupId: chatId }, (groupMsgAndInfo) => {
this.setState({ groupMsgAndInfo });
});
}
this.scrollToBottom();
}

_showPersonalInfo(value) {
this.setState({ showPersonalInfo: value });
}
Expand All @@ -157,6 +132,27 @@ class GroupChat extends Component {
});
}

componentDidMount() {
const {
allChatContent, homePageList, clearUnread, chatId
} = this.props;
const chatItem = allChatContent.groupChat && allChatContent.groupChat.get(chatId);
this._chat.clearUnreadHandle({ homePageList, clearUnread, chatFromId: chatId });
// (产品设计) 当查找没加过的群,点击去没群内容,请求出群内容,避免不了解而加错群
if (!chatItem) {
window.socket.emit('getOneGroupMsg', { groupId: chatId }, (groupMsgAndInfo) => {
this.setState({ groupMsgAndInfo });
});
}
this._chat.scrollToBottom();
}

componentWillUpdate() {
if (this._chat.isScrollInBottom) {
this._chat.scrollToBottom();
}
}

render() {
const { chatId, allChatContent } = this.props;
const {
Expand Down
25 changes: 10 additions & 15 deletions src/components/PrivateChat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import InputArea from '../InputArea';
import ChatContentList from '../ChatContentList';
import PersonalInfo from '../PersonalInfo';
import notification from '../Notification';
import Chat from '../../modules/Chat';

export default class PrivateChat extends Component {
constructor() {
Expand All @@ -15,6 +16,7 @@ export default class PrivateChat extends Component {
this.state = {
showPersonalInfo: false
};
this._chat = new Chat();
}

sendMessage = (inputMsg = '', attachments = []) => {
Expand All @@ -38,7 +40,7 @@ export default class PrivateChat extends Component {
};
this._sendByMe = true;
window.socket.emit('sendPrivateMsg', data);
this.scrollToBottom();
this._chat.scrollToBottom();
updateAllChatContent({ allChatContent, newChatContent: data, action: 'send' });
const dataForHomePage = { ...data, name: location.search.split('=')[1] };
updateHomePageList({ data: dataForHomePage, homePageList, myUserId: userId });
Expand Down Expand Up @@ -66,27 +68,20 @@ export default class PrivateChat extends Component {
});
}

scrollToBottom(time = 0) {
const ulDom = document.getElementsByClassName('chat-content-list')[0];
if (ulDom) {
setTimeout(() => {
ulDom.scrollTop = ulDom.scrollHeight + 10000;
}, time);
}
}

_showPersonalInfo(value) {
this.setState({ showPersonalInfo: value });
}

clearUnreadHandle() {
componentDidMount() {
const { homePageList, clearUnread, chatId } = this.props;
clearUnread({ homePageList, chatFromId: chatId });
this._chat.clearUnreadHandle({ homePageList, clearUnread, chatFromId: chatId });
this._chat.scrollToBottom();
}

componentDidMount() {
this.clearUnreadHandle();
this.scrollToBottom();
componentWillUpdate() {
if (this._chat.isScrollInBottom) {
this._chat.scrollToBottom();
}
}

shouldComponentUpdate(nextProps, nextState) {
Expand Down
22 changes: 22 additions & 0 deletions src/modules/Chat/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default class Chat {
scrollToBottom(time = 0) {
const ulDom = document.getElementsByClassName('chat-content-list')[0];
if (ulDom) {
setTimeout(() => {
ulDom.scrollTop = ulDom.scrollHeight + 10000;
}, time);
}
}

clearUnreadHandle({ homePageList, clearUnread, chatId }) {
clearUnread({ homePageList, chatFromId: chatId });
}

get isScrollInBottom() {
const ulDom = document.getElementsByClassName('chat-content-list')[0];
if (ulDom) {
return (ulDom.scrollTop + ulDom.clientHeight) === ulDom.scrollHeight;
}
return false;
}
}

0 comments on commit 4fd2c44

Please sign in to comment.