增加群附件多选和拖拽批量发送

This commit is contained in:
xhgit committed 2026-11-15 17:23:40 +08:00
1 parent 47b76080c6
commit dfeb5bfefa
6 files changed
+138 -22

No files matched your search

+85 -17
View File
@@ -2,7 +2,7 @@
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useSession } from '../session'
import { APIError } from '../api'
import { downloadAttachment } from './attachments'
import { downloadAttachment, maxAttachmentSize } from './attachments'
import type { Attachment } from './attachments'
import MediaPreview from './MediaPreview.vue'
import ForwardDialog from './ForwardDialog.vue'
@@ -37,7 +37,10 @@ const editing = ref<GroupLine>()
const recalling = ref<GroupLine>()
const forwarding = ref<GroupLine>()
const forwardStatus = ref('')
const file = ref<File>()
const files = ref<File[]>([])
const maxBatchFiles = 8
const draggingFiles = ref(false)
const batchProgress = ref('')
const pendingVoice = ref<{ file: File; epoch: string }>()
const busy = ref(false)
const error = ref('')
@@ -191,19 +194,68 @@ function recall() {
})
}
function chooseFile(event: Event) {
function chooseFiles(event: Event) {
const input = event.target as HTMLInputElement
file.value = input.files?.[0]
if (input.files) addFiles(input.files)
input.value = ''
}
function sendFile() {
function addFiles(selection: FileList) {
if (!currentEpoch.value || busy.value || view.value?.hasPending || editing.value || !selection.length) return
error.value = ''
if (selection.length + files.value.length > maxBatchFiles) {
error.value = '每批最多发送 8 个群附件'
return
}
const incoming = Array.from(selection)
const oversized = incoming.find((item) => item.size > maxAttachmentSize)
if (oversized) {
error.value = oversized.name + ' 超过单文件 8 MiB 上限'
return
}
files.value.push(...incoming)
}
function dragFiles(event: DragEvent) {
if (!event.dataTransfer?.types.includes('Files')) return
event.preventDefault()
draggingFiles.value = Boolean(currentEpoch.value && !busy.value && !view.value?.hasPending && !editing.value)
event.dataTransfer.dropEffect = draggingFiles.value ? 'copy' : 'none'
}
function leaveDropArea(event: DragEvent) {
const area = event.currentTarget as HTMLElement
if (!(event.relatedTarget instanceof Node) || !area.contains(event.relatedTarget)) draggingFiles.value = false
}
function dropFiles(event: DragEvent) {
draggingFiles.value = false
if (event.dataTransfer?.types.includes('Files')) {
event.preventDefault()
addFiles(event.dataTransfer.files)
}
}
function sendFiles() {
const identity = session.identity
const selectedFile = file.value
if (identity && selectedFile) void run(async () => {
await sendGroupMessage(identity, props.group, selectedFile, session.authorized)
file.value = undefined
await refresh()
const epoch = currentEpoch.value
if (!identity || !epoch || !files.value.length) return
void run(async () => {
const total = files.value.length
try {
for (let index = 0; index < total && active; index++) {
const file = files.value[0]
if (!file || epoch !== currentEpoch.value) break
batchProgress.value = `正在发送 ${index + 1} / ${total}`
await sendGroupMessage(identity, props.group, file, session.authorized, epoch)
// Once the encrypted envelope is queued, that outbox owns retrying it.
files.value.shift()
await refresh()
if (view.value?.hasPending) break
}
} finally {
batchProgress.value = ''
}
})
}
@@ -252,6 +304,7 @@ watch(() => session.connection, (connection) => { if (connection === '实时连
watch(currentEpoch, (epoch, previous) => {
if (!previous || epoch === previous) return
announcementDraft.value = undefined
draggingFiles.value = false
if (editing.value || replying.value) draft.value = ''
editing.value = undefined
replying.value = undefined
@@ -286,7 +339,7 @@ onBeforeUnmount(() => {
</script>
<template>
<section class="group-chat" aria-label="群消息">
<section class="group-chat" aria-label="群消息" @dragover.prevent @drop.prevent>
<h3>群消息</h3>
<p v-if="error" class="error" role="alert">{{ error }}</p>
<p v-if="forwardStatus" role="status">{{ forwardStatus }}</p>
@@ -402,12 +455,22 @@ onBeforeUnmount(() => {
<div v-if="ready && !editing" class="group-chat-composer">
<VoiceRecorder v-if="currentEpoch" :key="currentEpoch" :epoch="currentEpoch" :disabled="busy || view?.hasPending || !!pendingVoice"
@send="sendVoice" @error="error = $event" />
<label>选择群附件 <input type="file" :disabled="busy || view?.hasPending" @change="chooseFile"></label>
<div v-if="file" class="message-actions">
<span>{{ file.name }}</span>
<button type="button" class="plain" :disabled="busy || view?.hasPending" @click="sendFile">发送群附件</button>
<button type="button" class="plain" :disabled="busy" @click="file = undefined">取消</button>
</div>
<section class="group-drop-zone" aria-label="群附件发送区" :class="{ dragging: draggingFiles }"
@dragover="dragFiles" @dragleave="leaveDropArea" @drop="dropFiles">
<label>选择群附件 <input type="file" multiple :disabled="busy || view?.hasPending" @change="chooseFiles"></label>
<p class="muted">也可拖入文件。每批最多 8 个,单个文件最大 8 MiB。</p>
<ul v-if="files.length" class="group-file-queue" aria-label="待发附件">
<li v-for="(item, index) in files" :key="item.name + '/' + index">
<span>{{ item.name }}</span>
<button type="button" class="plain" :aria-label="'移除 ' + item.name" :disabled="busy" @click="files.splice(index, 1)">移除</button>
</li>
</ul>
<p v-if="batchProgress" role="status">{{ batchProgress }}</p>
<div v-if="files.length" class="message-actions">
<button type="button" class="plain" :disabled="busy || view?.hasPending" @click="sendFiles">发送群附件</button>
<button type="button" class="plain" :disabled="busy" @click="files = []">清空待发附件</button>
</div>
</section>
</div>
</template>
</section>
@@ -431,6 +494,11 @@ onBeforeUnmount(() => {
.group-media li { min-width: 0; padding: .7rem; border: 1px solid var(--line); border-radius: .45rem; }
.group-chat-composer { display: grid; gap: .55rem; }
.group-chat-composer textarea { display: block; width: 100%; resize: vertical; font: inherit; }
.group-drop-zone { display: grid; gap: .5rem; padding: .65rem .8rem; border: 1px dashed var(--line); border-radius: .45rem; }
.group-drop-zone.dragging { border-color: var(--green); background: var(--green-soft); }
.group-file-queue { display: grid; gap: .35rem; margin: 0; padding: 0; list-style: none; }
.group-file-queue li { display: flex; align-items: baseline; justify-content: space-between; gap: .5rem; }
.group-file-queue span { overflow-wrap: anywhere; }
.group-chat button { justify-self: start; }
.media-preview :deep(img), .media-preview :deep(video) { display: block; max-width: 100%; max-height: 18rem; }
</style>