支持私聊附件多选拖拽和批量重试
This commit is contained in:
1 parent
ec2679b991
commit
2931383906
8 files changed
+166
-35
No files matched your search
@@ -481,11 +481,11 @@ 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" />
|
||||
<section class="group-drop-zone" aria-label="群附件发送区" :class="{ dragging: draggingFiles }"
|
||||
<section class="attachment-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="待发附件">
|
||||
<ul v-if="files.length" class="attachment-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>
|
||||
@@ -520,11 +520,6 @@ 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>
|
||||
@@ -217,7 +217,8 @@ export function mutateMessage(identity: ChatIdentity, user: string, peer: Peer,
|
||||
})
|
||||
}
|
||||
|
||||
export function send(identity: ChatIdentity, user: string, peer: Peer, content: string | File, call: Call, reply?: ChatLine) {
|
||||
export function send(identity: ChatIdentity, user: string, peer: Peer, content: string | File, call: Call,
|
||||
{ reply, onQueued }: { reply?: ChatLine; onQueued?: () => void } = {}) {
|
||||
return update(identity, async (state, save, factory) => {
|
||||
const current = (await directory(call, user)).find((item) => item.id === peer.id)
|
||||
if (!current || state.pinned[current.id] !== current.publicKey) throw new Error('发送前请核验对方设备安全码')
|
||||
@@ -257,6 +258,7 @@ export function send(identity: ChatIdentity, user: string, peer: Peer, content:
|
||||
state.lines.push({ id, peer: current.id, contact: user, text, attachment, quote: attachment ? undefined : quote, time: new Date().toISOString(), own: true })
|
||||
state.engine = base64(engine.snapshot())
|
||||
await save(state)
|
||||
onQueued?.()
|
||||
await flush(state, save, call)
|
||||
} finally {
|
||||
engine.free()
|
||||
|
||||
@@ -97,6 +97,12 @@ input { width: 100%; color: var(--text); background: var(--base); border: 1px so
|
||||
.media-preview img, .media-preview video { display: block; max-width: min(300px, 100%); max-height: 220px; border-radius: 5px; }
|
||||
.chat-compose { display: grid; gap: 8px; }
|
||||
.chat-compose button { justify-self: end; }
|
||||
.attachment-drop-zone { display: grid; gap: .5rem; padding: .65rem .8rem; border: 1px dashed var(--line); border-radius: .45rem; }
|
||||
.attachment-drop-zone.dragging { border-color: var(--green); background: var(--green-soft); }
|
||||
.attachment-drop-zone p { margin: 0; }
|
||||
.attachment-queue { display: grid; gap: .35rem; margin: 0; padding: 0; list-style: none; }
|
||||
.attachment-queue li { display: flex; align-items: baseline; justify-content: space-between; gap: .5rem; }
|
||||
.attachment-queue span { overflow-wrap: anywhere; }
|
||||
.chat-filters, .chat-quote, .message-actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: end; }
|
||||
.chat-filters label:first-child { flex: 1 1 180px; }
|
||||
.chat-checkbox { display: flex; align-items: center; gap: 6px; padding: 10px 4px; }
|
||||
|
||||
+95
-19
@@ -9,7 +9,7 @@ import { forwardContent } from '../chat/group-chat'
|
||||
import type { ForwardDestination } from '../chat/group-chat'
|
||||
import ForwardDialog from '../chat/ForwardDialog.vue'
|
||||
import type { Contact } from '../contacts'
|
||||
import { downloadAttachment } from '../chat/attachments'
|
||||
import { downloadAttachment, maxAttachmentSize } from '../chat/attachments'
|
||||
import type { Attachment } from '../chat/attachments'
|
||||
import VoiceRecorder from '../chat/VoiceRecorder.vue'
|
||||
import VoiceMessage from '../chat/VoiceMessage.vue'
|
||||
@@ -38,7 +38,11 @@ const unread = ref<Record<string, number>>({})
|
||||
const conversationQuery = ref('')
|
||||
const history = ref<HTMLOListElement>()
|
||||
const shareRead = ref(false)
|
||||
const pendingFile = ref<File>()
|
||||
const pendingVoice = ref<File>()
|
||||
const files = ref<File[]>([])
|
||||
const draggingFiles = ref(false)
|
||||
const batchProgress = ref('')
|
||||
const maxBatchFiles = 8
|
||||
const view = ref<Awaited<ReturnType<typeof synchronize>>>()
|
||||
const target = computed(() => typeof route.params.user === 'string' ? route.params.user : '')
|
||||
const activeContact = computed(() => contacts.value.find((item) => item.id === target.value && item.state === 'friend'))
|
||||
@@ -220,7 +224,7 @@ function submit() {
|
||||
if (editing.value) {
|
||||
await mutateMessage(session.identity, target.value, peer.value, { action: 'edit', id: editing.value.id, text: draft.value }, session.authorized)
|
||||
} else {
|
||||
await send(session.identity, target.value, peer.value, draft.value, session.authorized, reply.value)
|
||||
await send(session.identity, target.value, peer.value, draft.value, session.authorized, { reply: reply.value })
|
||||
}
|
||||
draft.value = ''
|
||||
reply.value = undefined
|
||||
@@ -244,29 +248,84 @@ function recall() {
|
||||
})
|
||||
}
|
||||
|
||||
function attach(event: Event) {
|
||||
function chooseFiles(event: Event) {
|
||||
const input = event.target as HTMLInputElement
|
||||
const file = input.files?.[0]
|
||||
if (input.files) addFiles(input.files)
|
||||
input.value = ''
|
||||
if (file) pendingFile.value = file
|
||||
}
|
||||
|
||||
function sendFile(file: File) {
|
||||
function addFiles(selection: FileList) {
|
||||
if (!trusted.value || batchProgress.value || editing.value || !selection.length) return
|
||||
error.value = ''
|
||||
if (files.value.length + selection.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(trusted.value && !busy.value && !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')) return
|
||||
event.preventDefault()
|
||||
addFiles(event.dataTransfer.files)
|
||||
}
|
||||
|
||||
function sendFiles() {
|
||||
const identity = session.identity
|
||||
const device = peer.value
|
||||
const contact = target.value
|
||||
const queue = files.value
|
||||
if (!identity || !device || !trusted.value || !queue.length) return
|
||||
void execute(async () => {
|
||||
const total = queue.length
|
||||
try {
|
||||
for (let index = 0; index < total && active; index++) {
|
||||
const file = queue[0]
|
||||
if (!file || files.value !== queue || selected.value !== device.id || target.value !== contact) break
|
||||
batchProgress.value = `正在发送 ${index + 1} / ${total}`
|
||||
await send(identity, contact, device, file, session.authorized, { onQueued: () => queue.shift() })
|
||||
}
|
||||
} catch (cause) {
|
||||
if (active && target.value === contact && selected.value === device.id) await refresh().catch(() => {})
|
||||
throw cause
|
||||
} finally {
|
||||
batchProgress.value = ''
|
||||
}
|
||||
if (active && target.value === contact && selected.value === device.id) await refresh()
|
||||
})
|
||||
}
|
||||
|
||||
function sendVoice(file: File) {
|
||||
pendingVoice.value = file
|
||||
}
|
||||
|
||||
watch([pendingVoice, busy], ([file, inProgress]) => {
|
||||
if (!file || inProgress) return
|
||||
pendingVoice.value = undefined
|
||||
void execute(async () => {
|
||||
if (!session.identity || !peer.value) return
|
||||
await send(session.identity, target.value, peer.value, file, session.authorized)
|
||||
await refresh()
|
||||
})
|
||||
}
|
||||
|
||||
function sendVoice(file: File) {
|
||||
pendingFile.value = file
|
||||
}
|
||||
|
||||
watch([pendingFile, busy], ([file, inProgress]) => {
|
||||
if (!file || inProgress) return
|
||||
pendingFile.value = undefined
|
||||
sendFile(file)
|
||||
})
|
||||
|
||||
function saveAttachment(attachment: Attachment) {
|
||||
@@ -321,7 +380,9 @@ watch(selected, () => {
|
||||
recalling.value = undefined
|
||||
forwarding.value = undefined
|
||||
forwardStatus.value = ''
|
||||
pendingFile.value = undefined
|
||||
pendingVoice.value = undefined
|
||||
files.value = []
|
||||
draggingFiles.value = false
|
||||
draft.value = ''
|
||||
scheduleReceipt()
|
||||
queueSeen()
|
||||
@@ -460,7 +521,22 @@ onBeforeUnmount(() => {
|
||||
<span>引用 {{ (reply.text || reply.attachment?.name || '').slice(0, 80) }}</span>
|
||||
<button type="button" class="plain" @click="reply = undefined">取消引用</button>
|
||||
</div>
|
||||
<label v-if="!editing">发送附件<input type="file" :disabled="busy" @change="attach"></label>
|
||||
<section v-if="!editing" class="attachment-drop-zone" aria-label="私聊附件发送区" :class="{ dragging: draggingFiles }"
|
||||
@dragover="dragFiles" @dragleave="leaveDropArea" @drop="dropFiles">
|
||||
<label>选择私聊附件 <input type="file" multiple :disabled="busy" @change="chooseFiles"></label>
|
||||
<p class="muted">也可拖入文件。每批最多 8 个,单个文件最大 8 MiB。</p>
|
||||
<ul v-if="files.length" class="attachment-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" @click="sendFiles">发送私聊附件</button>
|
||||
<button type="button" class="plain" :disabled="busy" @click="files = []">清空待发附件</button>
|
||||
</div>
|
||||
</section>
|
||||
<VoiceRecorder v-if="!editing" :key="selected" :disabled="busy" @send="sendVoice" @error="error = $event" />
|
||||
<label>消息<textarea v-model="draft" required rows="3" maxlength="4000"></textarea></label>
|
||||
<button class="primary" :disabled="busy || !draft.trim()">{{ editing ? '保存编辑' : '发送' }}</button>
|
||||
|
||||
Reference in new issue
Block a user