220 lines
5.9 KiB
Vue
220 lines
5.9 KiB
Vue
<template>
|
||
<div class="media-upload" :class="{ text: listType === 'text' }">
|
||
<div v-if="listType === 'image-card'" class="image-grid">
|
||
<div v-for="(m, i) in items" :key="m.key" class="image-item previewable" @click="preview(i)">
|
||
<img v-if="isImage(m)" class="image-thumb" :src="m.url || m.key" :alt="m.name || m.key" loading="lazy" />
|
||
<div v-else class="file-thumb">{{ fileExt(m) }}</div>
|
||
<button class="remove" type="button" :title="t('common.action.delete')" @click.stop="remove(m.key)">×</button>
|
||
</div>
|
||
<div v-for="u in uploading" :key="u.id" class="image-item uploading">
|
||
<n-spin size="small" />
|
||
</div>
|
||
<label v-if="canUpload" class="image-item add">
|
||
<input type="file" :accept="accept" :multiple="allowMultiple" hidden @change="onPick" />
|
||
<n-icon :component="AddOutline" :size="22" />
|
||
<span>{{ t('common.action.upload') }}</span>
|
||
</label>
|
||
</div>
|
||
|
||
<div v-else class="file-list">
|
||
<div v-for="(m, i) in items" :key="m.key" class="file-row">
|
||
<span class="file-name" :title="m.name || m.key" @click="preview(i)">{{ m.name || m.key }}</span>
|
||
<n-button quaternary circle size="tiny" type="error" :title="t('common.action.delete')" @click="remove(m.key)">
|
||
×
|
||
</n-button>
|
||
</div>
|
||
<div v-for="u in uploading" :key="u.id" class="file-row uploading-row">
|
||
<n-spin size="small" />
|
||
<span>{{ t('common.action.upload') }}...</span>
|
||
</div>
|
||
<label v-if="canUpload" class="file-button">
|
||
<input type="file" :accept="accept" :multiple="allowMultiple" hidden @change="onPick" />
|
||
<n-button tag="span">
|
||
<template #icon><n-icon :component="CloudUploadOutline" /></template>
|
||
{{ t('common.action.upload') }}
|
||
</n-button>
|
||
</label>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, ref } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import { NButton, NIcon, NSpin } from 'naive-ui';
|
||
import { AddOutline, CloudUploadOutline } from '@vicons/ionicons5';
|
||
import { post } from '@/api/request';
|
||
import { message } from '@/utils/feedback';
|
||
import { openMediaViewer } from '@/composables/openMediaViewer';
|
||
|
||
interface MediaItem {
|
||
key: string;
|
||
url?: string;
|
||
name?: string;
|
||
type?: string;
|
||
}
|
||
|
||
const props = withDefaults(
|
||
defineProps<{ modelValue: MediaItem[]; type?: 'image' | 'video' | 'file'; max?: number }>(),
|
||
{ type: 'image', max: 8 },
|
||
);
|
||
const emit = defineEmits<{ (e: 'update:modelValue', v: MediaItem[]): void }>();
|
||
const { t } = useI18n();
|
||
|
||
const uploading = ref<{ id: number }[]>([]);
|
||
let seq = 0;
|
||
|
||
const items = computed(() => props.modelValue || []);
|
||
const listType = computed(() => (props.type === 'image' ? 'image-card' : 'text'));
|
||
const accept = computed(() =>
|
||
props.type === 'image' ? 'image/*' : props.type === 'video' ? 'video/*' : 'application/pdf,image/*',
|
||
);
|
||
const allowMultiple = computed(() => props.max > 1);
|
||
const canUpload = computed(() => items.value.length + uploading.value.length < props.max);
|
||
|
||
function isImage(m: MediaItem) {
|
||
return props.type === 'image' || /^image\//.test(m.type || '') || /\.(png|jpe?g|gif|webp|bmp|svg)$/i.test(m.url || m.key);
|
||
}
|
||
|
||
function fileExt(m: MediaItem) {
|
||
const name = m.name || m.key;
|
||
const ext = name.split('.').pop();
|
||
return ext && ext.length <= 5 ? ext.toUpperCase() : 'FILE';
|
||
}
|
||
|
||
async function onPick(e: Event) {
|
||
const input = e.target as HTMLInputElement;
|
||
const files = Array.from(input.files || []);
|
||
input.value = '';
|
||
for (const file of files) {
|
||
if (items.value.length + uploading.value.length >= props.max) {
|
||
message.warning(t('common.msg.maxReached', { n: props.max }));
|
||
break;
|
||
}
|
||
await uploadOne(file);
|
||
}
|
||
}
|
||
|
||
async function uploadOne(file: File) {
|
||
const holder = { id: ++seq };
|
||
uploading.value.push(holder);
|
||
try {
|
||
const fd = new FormData();
|
||
fd.append('file', file);
|
||
const res: any = await post(`/upload?type=${props.type}`, fd);
|
||
emit('update:modelValue', [
|
||
...items.value,
|
||
{ key: res.key, url: res.url, name: res.name || file.name, type: props.type },
|
||
]);
|
||
} catch {
|
||
message.error(t('common.msg.uploadFailed'));
|
||
} finally {
|
||
uploading.value = uploading.value.filter((u) => u.id !== holder.id);
|
||
}
|
||
}
|
||
|
||
function remove(key: string) {
|
||
emit('update:modelValue', items.value.filter((m) => m.key !== key));
|
||
}
|
||
|
||
function preview(index: number) {
|
||
void openMediaViewer(
|
||
items.value.map((m) => ({ url: m.url || m.key, name: m.name || m.key, type: m.type })),
|
||
index,
|
||
);
|
||
}
|
||
</script>
|
||
|
||
<style scoped>
|
||
.media-upload {
|
||
width: 100%;
|
||
}
|
||
.image-grid {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 10px;
|
||
}
|
||
.image-item {
|
||
position: relative;
|
||
width: 96px;
|
||
height: 96px;
|
||
border-radius: 8px;
|
||
overflow: hidden;
|
||
border: 1px solid var(--n-border-color, #e0e0e6);
|
||
background: #f4f7fc;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
}
|
||
.image-thumb {
|
||
width: 100%;
|
||
height: 100%;
|
||
object-fit: cover;
|
||
}
|
||
.previewable,
|
||
.file-name {
|
||
cursor: zoom-in;
|
||
}
|
||
.file-thumb {
|
||
font-size: 12px;
|
||
color: #6b7280;
|
||
}
|
||
.image-item.add,
|
||
.image-item.uploading {
|
||
flex-direction: column;
|
||
gap: 4px;
|
||
font-size: 12px;
|
||
color: #6b7280;
|
||
cursor: pointer;
|
||
border-style: dashed;
|
||
background: #fafcff;
|
||
}
|
||
.image-item.uploading {
|
||
cursor: default;
|
||
}
|
||
.remove {
|
||
position: absolute;
|
||
top: 2px;
|
||
right: 2px;
|
||
width: 20px;
|
||
height: 20px;
|
||
border: none;
|
||
border-radius: 50%;
|
||
background: rgba(0, 0, 0, 0.55);
|
||
color: #fff;
|
||
font-size: 14px;
|
||
line-height: 20px;
|
||
cursor: pointer;
|
||
padding: 0;
|
||
}
|
||
.remove:hover {
|
||
background: #d03050;
|
||
}
|
||
.file-list {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 8px;
|
||
align-items: flex-start;
|
||
}
|
||
.file-row {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
max-width: 100%;
|
||
min-height: 28px;
|
||
}
|
||
.file-name {
|
||
max-width: 360px;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
.uploading-row {
|
||
color: #6b7280;
|
||
font-size: 13px;
|
||
}
|
||
.file-button {
|
||
display: inline-flex;
|
||
}
|
||
</style>
|