Files
Artveris/apps/admin/src/components/MediaSorter.vue
T

271 lines
6.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="media-sorter">
<div class="grid">
<div v-for="(m, i) in list" :key="m.key" class="item">
<div
class="cell previewable"
:class="{ dragging: dragIndex === i, over: overIndex === i }"
draggable="true"
@click="preview(i)"
@dragstart="onDragStart(i)"
@dragover.prevent="onDragOver(i)"
@drop="onDrop(i)"
@dragend="onDragEnd"
>
<video v-if="isVideo(m)" class="thumb" :src="mediaSrc(m)" muted preload="metadata" />
<img v-else class="thumb" :src="mediaSrc(m)" :alt="m.name" loading="lazy" />
<div v-if="isVideo(m)" class="play"><n-icon :component="PlayCircleOutline" :size="26" /></div>
<div class="order">{{ i + 1 }}</div>
<button class="del" type="button" :title="t('common.action.delete')" @click.stop="remove(i)">×</button>
</div>
<n-input
v-if="captionable"
size="small"
:value="m.name"
:placeholder="t('pages.artwork.imageCaption')"
class="caption-input"
@update:value="(v) => updateCaption(i, v)"
/>
</div>
<div v-for="u in uploading" :key="u.id" class="item">
<div class="cell uploading"><n-spin size="small" /></div>
</div>
<label v-if="list.length + uploading.length < max" class="item">
<span class="cell add">
<input ref="inputRef" type="file" accept="image/*,video/*" multiple hidden @change="onPick" />
<n-icon :component="AddOutline" :size="22" />
<span>{{ t('common.action.upload') }}</span>
</span>
</label>
</div>
<div class="hint">{{ captionable ? t('pages.artwork.mediaHintCaption') : t('pages.artwork.mediaHint') }}</div>
</div>
</template>
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { NIcon, NSpin, NInput } from 'naive-ui';
import { AddOutline, PlayCircleOutline } from '@vicons/ionicons5';
import { post } from '@/api/request';
import { message } from '@/utils/feedback';
import { openMediaViewer } from '@/composables/openMediaViewer';
import { resolveMediaUrl } from '@/utils/mediaUrl';
interface MediaItem {
key: string;
url?: string;
name?: string;
type?: string;
}
const props = withDefaults(
defineProps<{ modelValue: MediaItem[]; max?: number; captionable?: boolean }>(),
{ max: 12, captionable: false },
);
const emit = defineEmits<{ (e: 'update:modelValue', v: MediaItem[]): void }>();
const { t } = useI18n();
const inputRef = ref<HTMLInputElement | null>(null);
const list = computed<MediaItem[]>(() => props.modelValue || []);
const uploading = ref<{ id: number }[]>([]);
let seq = 0;
function updateCaption(i: number, v: string) {
const arr = list.value.map((m, idx) => (idx === i ? { ...m, name: v } : m));
emit('update:modelValue', arr);
}
function mediaSrc(m: MediaItem) {
return resolveMediaUrl(m.url || m.key);
}
function isVideo(m: MediaItem) {
return m.type === 'video' || /\.(mp4|webm|mov|m4v|ogg)$/i.test(mediaSrc(m));
}
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 (list.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 type = file.type.startsWith('video/') ? 'video' : 'image';
const holder = { id: ++seq };
uploading.value.push(holder);
try {
const fd = new FormData();
fd.append('file', file);
const res: any = await post(`/upload?type=${type}`, fd);
emit('update:modelValue', [
...list.value,
{ key: res.key, url: resolveMediaUrl(res.url), name: props.captionable ? '' : res.name, type },
]);
} catch {
message.error(t('common.msg.uploadFailed'));
} finally {
uploading.value = uploading.value.filter((u) => u.id !== holder.id);
}
}
function remove(i: number) {
emit('update:modelValue', list.value.filter((_, idx) => idx !== i));
}
function toViewerItem(m: MediaItem) {
return { url: mediaSrc(m), name: m.name || m.key, type: m.type };
}
function preview(index: number) {
const current = list.value[index];
if (!current) return;
if (isVideo(current)) {
void openMediaViewer(list.value.map(toViewerItem), index);
return;
}
const imageList = list.value.filter((m) => !isVideo(m));
const imageIndex = imageList.findIndex((m) => m === current);
void openMediaViewer(imageList.map(toViewerItem), Math.max(imageIndex, 0));
}
const dragIndex = ref<number | null>(null);
const overIndex = ref<number | null>(null);
function onDragStart(i: number) {
dragIndex.value = i;
}
function onDragOver(i: number) {
overIndex.value = i;
}
function onDrop(i: number) {
const from = dragIndex.value;
if (from === null || from === i) return;
const arr = [...list.value];
const [moved] = arr.splice(from, 1);
arr.splice(i, 0, moved);
emit('update:modelValue', arr);
onDragEnd();
}
function onDragEnd() {
dragIndex.value = null;
overIndex.value = null;
}
</script>
<style scoped>
.grid {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.item {
display: flex;
flex-direction: column;
gap: 4px;
}
.caption-input {
width: 96px;
}
.cell {
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;
}
.cell .thumb {
width: 100%;
height: 100%;
object-fit: cover;
background: #000;
}
.cell.dragging {
opacity: 0.4;
}
.cell.over {
outline: 2px dashed var(--brand, #138a72);
outline-offset: -2px;
}
.cell[draggable='true'] {
cursor: grab;
}
.previewable {
cursor: zoom-in;
}
.play {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
background: rgba(0, 0, 0, 0.28);
pointer-events: none;
}
.order {
position: absolute;
left: 4px;
bottom: 4px;
min-width: 18px;
height: 18px;
padding: 0 5px;
border-radius: 9px;
background: rgba(0, 0, 0, 0.6);
color: #fff;
font-size: 11px;
line-height: 18px;
text-align: center;
}
.del {
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;
}
.del:hover {
background: #d03050;
}
.cell.add,
.cell.uploading {
flex-direction: column;
gap: 4px;
font-size: 12px;
color: #6b7280;
cursor: pointer;
border-style: dashed;
background: #fafcff;
}
.cell.uploading {
cursor: default;
}
.hint {
margin-top: 8px;
font-size: 12px;
color: #9ca3af;
}
</style>