358 lines
9.6 KiB
Vue
358 lines
9.6 KiB
Vue
<template>
|
|
<div class="showcase-editor">
|
|
<div class="showcase-list">
|
|
<div
|
|
v-for="(item, index) in list"
|
|
:key="item.key"
|
|
class="showcase-row"
|
|
:class="{ over: overIndex === index, readonly: disabled }"
|
|
@dragover.prevent="overIndex = index"
|
|
@drop="onDrop(index)"
|
|
>
|
|
<button type="button" class="showcase-preview" @click="preview(index)">
|
|
<img :src="mediaSrc(item)" :alt="item.name || t('pages.artwork.showcase')" loading="lazy" />
|
|
<span class="showcase-order">{{ index + 1 }}</span>
|
|
</button>
|
|
|
|
<div class="showcase-fields">
|
|
<n-input
|
|
size="small"
|
|
:value="item.name"
|
|
:maxlength="120"
|
|
:disabled="disabled"
|
|
:placeholder="t('pages.artwork.showcaseImageTitle')"
|
|
@update:value="(value) => updateItem(index, 'name', value)"
|
|
/>
|
|
<n-input
|
|
size="small"
|
|
type="textarea"
|
|
:value="item.description"
|
|
:maxlength="1000"
|
|
:disabled="disabled"
|
|
:autosize="{ minRows: 2, maxRows: 5 }"
|
|
:placeholder="t('pages.artwork.showcaseImageDescription')"
|
|
@update:value="(value) => updateItem(index, 'description', value)"
|
|
/>
|
|
</div>
|
|
|
|
<div v-if="!disabled" class="showcase-actions">
|
|
<n-tooltip>
|
|
<template #trigger>
|
|
<n-button
|
|
quaternary
|
|
circle
|
|
size="small"
|
|
:disabled="index === 0"
|
|
:aria-label="t('common.action.moveUp')"
|
|
@click="move(index, -1)"
|
|
>
|
|
<template #icon><n-icon :component="ArrowUpOutline" /></template>
|
|
</n-button>
|
|
</template>
|
|
{{ t('common.action.moveUp') }}
|
|
</n-tooltip>
|
|
<n-tooltip>
|
|
<template #trigger>
|
|
<n-button
|
|
quaternary
|
|
circle
|
|
size="small"
|
|
:disabled="index === list.length - 1"
|
|
:aria-label="t('common.action.moveDown')"
|
|
@click="move(index, 1)"
|
|
>
|
|
<template #icon><n-icon :component="ArrowDownOutline" /></template>
|
|
</n-button>
|
|
</template>
|
|
{{ t('common.action.moveDown') }}
|
|
</n-tooltip>
|
|
<n-tooltip>
|
|
<template #trigger>
|
|
<n-button
|
|
quaternary
|
|
circle
|
|
size="small"
|
|
type="error"
|
|
:aria-label="t('common.action.delete')"
|
|
@click="remove(index)"
|
|
>
|
|
<template #icon><n-icon :component="TrashOutline" /></template>
|
|
</n-button>
|
|
</template>
|
|
{{ t('common.action.delete') }}
|
|
</n-tooltip>
|
|
<button
|
|
type="button"
|
|
class="drag-handle"
|
|
draggable="true"
|
|
:title="t('pages.artwork.showcaseDrag')"
|
|
:aria-label="t('pages.artwork.showcaseDrag')"
|
|
@dragstart="dragIndex = index"
|
|
@dragend="onDragEnd"
|
|
>
|
|
⋮⋮
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div v-for="holder in uploading" :key="holder.id" class="showcase-row showcase-uploading">
|
|
<div class="showcase-loading"><n-spin size="small" /></div>
|
|
<span>{{ t('common.action.upload') }}...</span>
|
|
</div>
|
|
</div>
|
|
|
|
<label v-if="!disabled && list.length + uploading.length < max" class="showcase-add">
|
|
<input type="file" accept="image/jpeg,image/png,image/webp,image/gif" multiple hidden @change="onPick" />
|
|
<n-icon :component="AddOutline" :size="24" />
|
|
<span>{{ t('pages.artwork.addShowcaseImages') }}</span>
|
|
</label>
|
|
<div class="showcase-hint">{{ t('pages.artwork.showcaseHint', { n: max }) }}</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { computed, ref } from 'vue';
|
|
import { useI18n } from 'vue-i18n';
|
|
import { NButton, NIcon, NInput, NSpin, NTooltip } from 'naive-ui';
|
|
import { AddOutline, ArrowDownOutline, ArrowUpOutline, TrashOutline } from '@vicons/ionicons5';
|
|
import { post } from '@/api/request';
|
|
import { openMediaViewer } from '@/composables/openMediaViewer';
|
|
import { message } from '@/utils/feedback';
|
|
import { resolveMediaUrl } from '@/utils/mediaUrl';
|
|
|
|
interface ShowcaseImage {
|
|
key: string;
|
|
url?: string;
|
|
name?: string;
|
|
type?: string;
|
|
description?: string;
|
|
}
|
|
|
|
const props = withDefaults(
|
|
defineProps<{ modelValue: ShowcaseImage[]; max?: number; disabled?: boolean }>(),
|
|
{ max: 30, disabled: false },
|
|
);
|
|
const emit = defineEmits<{ (event: 'update:modelValue', value: ShowcaseImage[]): void }>();
|
|
const { t } = useI18n();
|
|
|
|
const list = computed(() => props.modelValue || []);
|
|
const uploading = ref<Array<{ id: number }>>([]);
|
|
const dragIndex = ref<number | null>(null);
|
|
const overIndex = ref<number | null>(null);
|
|
let uploadSequence = 0;
|
|
|
|
function mediaSrc(item: ShowcaseImage) {
|
|
return resolveMediaUrl(item.url || item.key);
|
|
}
|
|
|
|
function updateItem(index: number, field: 'name' | 'description', value: string) {
|
|
emit(
|
|
'update:modelValue',
|
|
list.value.map((item, itemIndex) => (itemIndex === index ? { ...item, [field]: value } : item)),
|
|
);
|
|
}
|
|
|
|
function move(index: number, offset: number) {
|
|
const target = index + offset;
|
|
if (target < 0 || target >= list.value.length) return;
|
|
const next = [...list.value];
|
|
const [item] = next.splice(index, 1);
|
|
next.splice(target, 0, item);
|
|
emit('update:modelValue', next);
|
|
}
|
|
|
|
function remove(index: number) {
|
|
emit('update:modelValue', list.value.filter((_, itemIndex) => itemIndex !== index));
|
|
}
|
|
|
|
function preview(index: number) {
|
|
void openMediaViewer(
|
|
list.value.map((item) => ({ url: mediaSrc(item), name: item.name || item.key, type: 'image' })),
|
|
index,
|
|
);
|
|
}
|
|
|
|
function onDrop(target: number) {
|
|
const source = dragIndex.value;
|
|
if (source === null || source === target) return onDragEnd();
|
|
const next = [...list.value];
|
|
const [item] = next.splice(source, 1);
|
|
next.splice(target, 0, item);
|
|
emit('update:modelValue', next);
|
|
onDragEnd();
|
|
}
|
|
|
|
function onDragEnd() {
|
|
dragIndex.value = null;
|
|
overIndex.value = null;
|
|
}
|
|
|
|
async function onPick(event: Event) {
|
|
const input = event.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 holder = { id: ++uploadSequence };
|
|
uploading.value.push(holder);
|
|
try {
|
|
const body = new FormData();
|
|
body.append('file', file);
|
|
const result: any = await post('/upload?type=image', body);
|
|
emit('update:modelValue', [
|
|
...list.value,
|
|
{
|
|
key: result.key,
|
|
url: resolveMediaUrl(result.url),
|
|
name: '',
|
|
description: '',
|
|
type: 'image',
|
|
},
|
|
]);
|
|
} catch {
|
|
message.error(t('common.msg.uploadFailed'));
|
|
} finally {
|
|
uploading.value = uploading.value.filter((item) => item.id !== holder.id);
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<style scoped>
|
|
.showcase-list {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 10px;
|
|
}
|
|
.showcase-row {
|
|
display: grid;
|
|
grid-template-columns: 132px minmax(0, 1fr) auto;
|
|
gap: 12px;
|
|
align-items: center;
|
|
padding: 10px;
|
|
border: 1px solid var(--n-border-color, #e0e0e6);
|
|
border-radius: 8px;
|
|
background: var(--n-color, #fff);
|
|
transition: border-color 0.15s ease;
|
|
}
|
|
.showcase-row.over {
|
|
border-color: var(--brand, #138a72);
|
|
}
|
|
.showcase-row.readonly {
|
|
grid-template-columns: 132px minmax(0, 1fr);
|
|
}
|
|
.showcase-preview {
|
|
position: relative;
|
|
width: 132px;
|
|
height: 104px;
|
|
padding: 0;
|
|
border: 0;
|
|
border-radius: 6px;
|
|
overflow: hidden;
|
|
background: #eef2f6;
|
|
cursor: zoom-in;
|
|
}
|
|
.showcase-preview img {
|
|
width: 100%;
|
|
height: 100%;
|
|
display: block;
|
|
object-fit: contain;
|
|
}
|
|
.showcase-order {
|
|
position: absolute;
|
|
left: 6px;
|
|
bottom: 6px;
|
|
min-width: 22px;
|
|
height: 22px;
|
|
padding: 0 6px;
|
|
border-radius: 4px;
|
|
background: rgba(0, 0, 0, 0.62);
|
|
color: #fff;
|
|
font-size: 12px;
|
|
line-height: 22px;
|
|
}
|
|
.showcase-fields {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 8px;
|
|
min-width: 0;
|
|
}
|
|
.showcase-actions {
|
|
display: grid;
|
|
grid-template-columns: repeat(2, 32px);
|
|
gap: 2px;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
.drag-handle {
|
|
width: 32px;
|
|
height: 32px;
|
|
padding: 0;
|
|
border: 0;
|
|
border-radius: 6px;
|
|
background: transparent;
|
|
color: #8b95a5;
|
|
font-size: 18px;
|
|
cursor: grab;
|
|
}
|
|
.drag-handle:hover {
|
|
background: rgba(19, 138, 114, 0.08);
|
|
}
|
|
.showcase-add {
|
|
min-height: 88px;
|
|
margin-top: 10px;
|
|
border: 1px dashed var(--n-border-color, #d8dce5);
|
|
border-radius: 8px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
gap: 8px;
|
|
color: #667085;
|
|
cursor: pointer;
|
|
background: #fafcff;
|
|
}
|
|
.showcase-add:hover {
|
|
color: var(--brand, #138a72);
|
|
border-color: currentColor;
|
|
}
|
|
.showcase-uploading {
|
|
grid-template-columns: 40px minmax(0, 1fr);
|
|
min-height: 64px;
|
|
color: #8b95a5;
|
|
}
|
|
.showcase-loading {
|
|
display: flex;
|
|
justify-content: center;
|
|
}
|
|
.showcase-hint {
|
|
margin-top: 8px;
|
|
color: #98a2b3;
|
|
font-size: 12px;
|
|
}
|
|
@media (max-width: 640px) {
|
|
.showcase-row {
|
|
grid-template-columns: 104px minmax(0, 1fr);
|
|
align-items: start;
|
|
}
|
|
.showcase-preview {
|
|
width: 104px;
|
|
height: 104px;
|
|
}
|
|
.showcase-actions {
|
|
grid-column: 1 / -1;
|
|
display: flex;
|
|
justify-content: flex-end;
|
|
}
|
|
.showcase-row.readonly {
|
|
grid-template-columns: 104px minmax(0, 1fr);
|
|
}
|
|
}
|
|
</style>
|