Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
refactor: 优化
Browse files Browse the repository at this point in the history
  • Loading branch information
mys1024 committed Feb 5, 2023
1 parent ceec11b commit f99c29c
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { ElButton, ElMessage } from 'element-plus'
import { type Ref, nextTick, ref, watch } from 'vue'
import { storeToRefs } from 'pinia'
import MessageComponent from './Message.vue'
import type { Client } from '~/typings/app'
import { useChatStore } from '~/stores/chat'
import { useAccountStore } from '~/stores/account'
import { useFriendStore } from '~/stores/friend'
import { useNetworkStore } from '~/stores/network'
import { postFileMsg, postImageMsg, postTextMsg } from '~/api/message'
import { DISPLAY_MODE_ENABLE } from '~/config'
import MessageComponent from '~/components/message.vue'
const messageContainer = ref<HTMLDivElement>() as Ref<HTMLDivElement>
const imageInput = ref<HTMLInputElement>() as Ref<HTMLInputElement>
Expand All @@ -36,6 +36,11 @@ watch(selectedFriend, (friend) => {
text.value = textMap.get(friend.uid) || ''
})
chatStore.onAddNewMessage(async () => {
await nextTick()
messageContainer.value.scrollTo({ top: 1e9, behavior: 'smooth' })
})
function getHostAndPort(client: Client) {
if (networkInfo.value.ipv6 && client.ipv6)
return `[${client.ipv6}]:${client.port}`
Expand Down Expand Up @@ -92,8 +97,6 @@ async function sendText() {
data: text.value,
})
text.value = ''
await nextTick()
messageContainer.value.scrollTo({ top: 1e9, behavior: 'smooth' })
}
async function sendImage(event: Event) {
Expand Down Expand Up @@ -135,8 +138,6 @@ async function sendImage(event: Event) {
data: new Uint8Array(await image.arrayBuffer()),
})
imageInput.value.value = ''
await nextTick()
messageContainer.value.scrollTo({ top: 1e9, behavior: 'smooth' })
}
async function sendFile(event: Event) {
Expand Down Expand Up @@ -173,8 +174,6 @@ async function sendFile(event: Event) {
size: file.size,
})
fileInput.value.value = ''
await nextTick()
messageContainer.value.scrollTo({ top: 1e9, behavior: 'smooth' })
}
</script>

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/pages/main/chat.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts" setup>
import FriendList from '~/components/friend_list.vue'
import Chatroom from '~/components/chatroom.vue'
import FriendList from '~/components/FriendList.vue'
import Chatroom from '~/components/Chatroom/index.vue'
</script>

<template>
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/pages/main/friend_request.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ElButton } from 'element-plus'
import { useRouter } from 'vue-router'
import { useFriendStore } from '~/stores/friend'
import FriendRequestItem from '~/components/friend_request_item.vue'
import FriendRequestItem from '~/components/FriendRequestItem.vue'
const router = useRouter()
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/pages/main/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import Sidebar from '~/components/sidebar.vue'
import Sidebar from '~/components/Sidebar.vue'
</script>

<template>
Expand Down
9 changes: 9 additions & 0 deletions src/renderer/stores/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { useAccountStore } from './account'
import type { Friend, Message } from '~/typings/app'

export const useChatStore = defineStore('chat', () => {
const onAddNewMessageCallbacks: ((message: Message) => void)[] = []

const accountStore = useAccountStore()
const messagesMap = new Map<number, Message[]>()

Expand All @@ -30,6 +32,12 @@ export const useChatStore = defineStore('chat', () => {
messagesMap.set(friendUid, messages)
}
messages.push({ ...message })
for (const cb of onAddNewMessageCallbacks)
cb({ ...message })
}

function onAddNewMessage(cb: (message: Message) => void) {
onAddNewMessageCallbacks.push(cb)
}

window.electron.setNewTextMessageHandler((from, to, textMsg) => {
Expand All @@ -46,5 +54,6 @@ export const useChatStore = defineStore('chat', () => {
selectedFriend,
selectedMessages,
addNewMessage,
onAddNewMessage,
}
})

0 comments on commit f99c29c

Please sign in to comment.