43
43
44
44
<template v-if =" channelId " >
45
45
<el-scrollbar >
46
- <div v-if =" messages?.prev" >正在加载更多消息……</div >
46
+ <div ref =" header" v-if =" messages?.prev"
47
+ class =" items-center flex justify-center py-2
48
+ bg-gray-700 bg-op-0 hover:bg-op-100 transition cursor-pointer"
49
+ @click =" appendHeader"
50
+ >正在加载更多消息……</div >
47
51
<div v-for =" (message, index) in messages?.data" :key =" message.id"
48
- class =" relative px-4 py-0 break-words
52
+ class =" relative px-4 py-0 break-words last:mb-2
49
53
bg-gray-700 bg-op-0 hover:bg-op-100 transition"
50
54
:class =" { 'mt-2': !isSuccessive(index) }"
51
55
@contextmenu.stop =" triggerMessage($event, message)" >
67
71
class =" ml-14 lh-relaxed"
68
72
></message-content >
69
73
</div >
70
- <div v-if =" messages?.next" >正在加载更多消息……</div >
74
+ <div ref =" footer" v-if =" messages?.next"
75
+ class =" items-center flex justify-center py-2
76
+ bg-gray-700 bg-op-0 hover:bg-op-100 transition cursor-pointer"
77
+ @click =" appendFooter"
78
+ >正在加载更多消息……</div >
71
79
</el-scrollbar >
72
80
<div class =" footer shrink-0" >
73
81
<chat-input class =" h-6 px-4 py-2" v-model =" input" @send =" handleSend" placeholder =" 发送消息" ></chat-input >
114
122
<script lang="ts" setup>
115
123
116
124
import { ref } from ' vue'
117
- import { useContext , useRpc , useMenu } from ' @cordisjs/client'
118
- import type { Data } from ' ../src '
125
+ import { useContext , useMenu } from ' @cordisjs/client'
126
+ import { useIntersectionObserver } from ' @vueuse/core '
119
127
import { Universal } from ' @satorijs/core'
120
128
import { ChatInput , MessageContent } from ' @satorijs/components-vue'
121
129
import Avatar from ' ./avatar.vue'
@@ -130,6 +138,8 @@ const channels = ref<Universal.Channel[]>([])
130
138
const messages = ref <Universal .TwoWayList <Universal .Message >>()
131
139
132
140
const input = ref (' ' )
141
+ const header = ref <HTMLDivElement >()
142
+ const footer = ref <HTMLDivElement >()
133
143
134
144
const showGuild = ref <Universal .Guild >()
135
145
const showChannel = ref <Universal .Channel >()
@@ -168,16 +178,47 @@ async function setGuild(guild?: Universal.Guild & { platform: string; assignees:
168
178
}
169
179
}
170
180
181
+ function useBot() {
182
+ return ctx .bots [ctx .chat .guilds .value [guildKey .value ! ].assignees [0 ]]
183
+ }
184
+
171
185
async function setChannel(channel : Universal .Channel ) {
172
186
channelId .value = channel .id
173
187
messages .value = undefined
174
- const result = await ctx . bots [ ctx . chat . guilds . value [ guildKey . value ! ]. assignees [ 0 ]] .getMessageList (channel .id )
188
+ const result = await useBot () .getMessageList (channel .id )
175
189
result .next = undefined
176
- if (channelId .value === channel .id ) messages .value = result
190
+ if (channelId .value !== channel .id ) return
191
+ messages .value = result
192
+ }
193
+
194
+ let headerTask: Promise <void > | undefined
195
+
196
+ function appendHeader() {
197
+ if (headerTask ) return
198
+ const id = channelId .value !
199
+ headerTask = useBot ().getMessageList (id , messages .value ! .prev , ' before' ).then ((result ) => {
200
+ headerTask = undefined
201
+ if (channelId .value !== id ) return
202
+ messages .value ! .data .unshift (... result .data )
203
+ messages .value ! .prev = result .prev
204
+ })
205
+ }
206
+
207
+ let footerTask: Promise <void > | undefined
208
+
209
+ function appendFooter() {
210
+ if (footerTask ) return
211
+ const id = channelId .value !
212
+ footerTask = useBot ().getMessageList (id , messages .value ! .next , ' after' ).then ((result ) => {
213
+ footerTask = undefined
214
+ if (channelId .value !== id ) return
215
+ messages .value ! .data .push (... result .data )
216
+ messages .value ! .next = result .next
217
+ })
177
218
}
178
219
179
220
function handleSend(content : string ) {
180
- return ctx . bots [ ctx . chat . guilds . value [ guildKey . value ! ]. assignees [ 0 ]] .sendMessage (channelId .value ! , content )
221
+ return useBot () .sendMessage (channelId .value ! , content )
181
222
}
182
223
183
224
function formatTime(date : Date ) {
0 commit comments