545 lines
21 KiB
Vue
545 lines
21 KiB
Vue
<script setup lang="ts">
|
||
import { computed, reactive, ref } from 'vue'
|
||
import { NAlert, NButton, NFormItem, NInput, NPopconfirm, NSelect, NTag, NTooltip } from 'naive-ui'
|
||
import { RefreshCw, Search, Trash2, Upload } from '@lucide/vue'
|
||
import { AppDialog, AssetImage, EmptyState } from '../../components/ui'
|
||
import AppForm from '../../components/ui/AppForm.vue'
|
||
import WorkspacePage from '../../components/ui/WorkspacePage.vue'
|
||
import { useQuery } from '../../composables/useQuery'
|
||
import { fieldRule } from '../../lib/form-rules'
|
||
import { errorMessage } from '../../lib/http'
|
||
import { projectsApi } from '../projects/api'
|
||
import { projectAssetsApi } from './api'
|
||
import type { GlobalProjectAsset, ProjectAssetProject } from './types'
|
||
|
||
/** 后端当前允许的图片格式与大小。 */
|
||
const ACCEPTED_TYPES = new Set(['image/jpeg', 'image/png', 'image/webp'])
|
||
const MAX_FILE_SIZE = 20 * 1024 * 1024
|
||
const categoryPattern = /^[a-z0-9_-]+$/
|
||
const categoryLabels: Record<string, string> = {
|
||
overall: '综合',
|
||
character: '人物',
|
||
scene: '场景',
|
||
prop: '道具',
|
||
reference: '参考素材'
|
||
}
|
||
|
||
/** 全局接口一次返回全部图片及所属剧本,避免按项目产生 N 次请求。 */
|
||
const query = useQuery(ref('all-project-assets'), (_, signal) => projectAssetsApi.listAll(signal))
|
||
/** 上传需要列出包括尚无资产在内的全部剧本。 */
|
||
const projectsQuery = useQuery(ref('asset-project-options'), (_, signal) => projectsApi.list(signal))
|
||
const search = ref('')
|
||
const projectId = ref<string | null>(null)
|
||
const category = ref<string | null>(null)
|
||
const busy = ref(false)
|
||
const actionError = ref('')
|
||
const fileError = ref('')
|
||
const fileInput = ref<HTMLInputElement | null>(null)
|
||
const uploadOpen = ref(false)
|
||
const editOpen = ref(false)
|
||
const selectedFile = ref<File | null>(null)
|
||
const selectedAsset = ref<GlobalProjectAsset | null>(null)
|
||
const overflowTitles = reactive(new Set<string>())
|
||
const uploadForm = reactive({ projectId: '', name: '', category: 'reference' })
|
||
const editForm = reactive({ name: '', category: '' })
|
||
const assets = computed(() => query.data.value ?? [])
|
||
const projectOptions = computed(() => {
|
||
const projects = new Map<string, ProjectAssetProject>()
|
||
assets.value.forEach(asset => projects.set(asset.project.id, asset.project))
|
||
return [...projects.values()]
|
||
.toSorted((a, b) => projectName(a).localeCompare(projectName(b), 'zh-CN'))
|
||
.map(project => ({ label: projectName(project), value: project.id }))
|
||
})
|
||
const uploadProjectOptions = computed(() =>
|
||
(projectsQuery.data.value ?? [])
|
||
.toSorted((a, b) => projectName(a).localeCompare(projectName(b), 'zh-CN'))
|
||
.map(project => ({ label: projectName(project), value: project.id }))
|
||
)
|
||
const categoryOptions = computed(() =>
|
||
[...new Set(assets.value.map(asset => asset.category))]
|
||
.toSorted()
|
||
.map(value => ({ label: categoryLabel(value), value }))
|
||
)
|
||
const filteredAssets = computed(() => {
|
||
const keyword = search.value.trim().toLowerCase()
|
||
return assets.value.filter(asset => {
|
||
if (projectId.value && asset.projectId !== projectId.value) return false
|
||
if (category.value && asset.category !== category.value) return false
|
||
if (!keyword) return true
|
||
return [asset.name, asset.category, projectName(asset.project)].some(value =>
|
||
value.toLowerCase().includes(keyword)
|
||
)
|
||
})
|
||
})
|
||
const formRules = {
|
||
projectId: fieldRule(value => typeof value === 'string' && !!value, '请选择所属剧本'),
|
||
name: fieldRule(value => typeof value === 'string' && !!value.trim(), '请输入素材名称'),
|
||
category: fieldRule(
|
||
value => typeof value === 'string' && categoryPattern.test(value.trim()),
|
||
'分类只能包含小写字母、数字、下划线和连字符'
|
||
)
|
||
}
|
||
|
||
function projectName(project: Pick<ProjectAssetProject, 'title' | 'topic'>) {
|
||
return project.title?.trim() || project.topic
|
||
}
|
||
|
||
function categoryLabel(value: string) {
|
||
return categoryLabels[value] ?? value
|
||
}
|
||
|
||
function formatBytes(value: number) {
|
||
if (value < 1024) return `${value} B`
|
||
if (value < 1024 * 1024) return `${(value / 1024).toFixed(1)} KB`
|
||
return `${(value / 1024 / 1024).toFixed(1)} MB`
|
||
}
|
||
|
||
function formatDate(value: string) {
|
||
const date = new Date(value)
|
||
return Number.isNaN(date.getTime()) ? value : date.toLocaleString('zh-CN', { hour12: false })
|
||
}
|
||
|
||
function metadataOriginalName(value: unknown) {
|
||
if (!value || typeof value !== 'object' || Array.isArray(value)) return '未记录'
|
||
const originalName = (value as Record<string, unknown>).originalName
|
||
return typeof originalName === 'string' && originalName.trim() ? originalName : '未记录'
|
||
}
|
||
|
||
function clearFilters() {
|
||
search.value = ''
|
||
projectId.value = null
|
||
category.value = null
|
||
}
|
||
|
||
/** 同时刷新图片与上传所需的剧本选项。 */
|
||
async function refreshAll() {
|
||
await Promise.all([query.refresh(), projectsQuery.refresh()])
|
||
}
|
||
|
||
/** 先校验本地文件,再在当前页选择剧本并确认资料。 */
|
||
function selectFile(event: Event) {
|
||
const input = event.target as HTMLInputElement
|
||
const file = input.files?.[0] ?? null
|
||
input.value = ''
|
||
fileError.value = ''
|
||
actionError.value = ''
|
||
if (!file) return
|
||
if (!ACCEPTED_TYPES.has(file.type)) {
|
||
fileError.value = '仅支持 JPEG、PNG、WebP 图片。'
|
||
return
|
||
}
|
||
if (!file.size || file.size > MAX_FILE_SIZE) {
|
||
fileError.value = '上传图片不能为空且不能超过 20MB。'
|
||
return
|
||
}
|
||
selectedFile.value = file
|
||
uploadForm.projectId = projectId.value ?? ''
|
||
uploadForm.name = file.name.replace(/\.[^.]+$/, '') || file.name
|
||
uploadForm.category = 'reference'
|
||
uploadOpen.value = true
|
||
}
|
||
|
||
async function uploadAsset() {
|
||
const file = selectedFile.value
|
||
if (!file || busy.value) return
|
||
busy.value = true
|
||
actionError.value = ''
|
||
try {
|
||
await projectAssetsApi.upload(uploadForm.projectId, {
|
||
file,
|
||
name: uploadForm.name.trim(),
|
||
category: uploadForm.category.trim()
|
||
})
|
||
uploadOpen.value = false
|
||
selectedFile.value = null
|
||
await query.refresh()
|
||
} catch (error) {
|
||
actionError.value = errorMessage(error)
|
||
} finally {
|
||
busy.value = false
|
||
}
|
||
}
|
||
|
||
function openEdit(asset: GlobalProjectAsset) {
|
||
selectedAsset.value = asset
|
||
editForm.name = asset.name
|
||
editForm.category = asset.category
|
||
actionError.value = ''
|
||
editOpen.value = true
|
||
}
|
||
|
||
/** 只在标题真实溢出时启用 Tooltip,短标题不产生多余浮层。 */
|
||
function checkTitleOverflow(assetId: string, event: MouseEvent) {
|
||
const element = event.currentTarget as HTMLElement
|
||
if (element.scrollWidth > element.clientWidth) overflowTitles.add(assetId)
|
||
else overflowTitles.delete(assetId)
|
||
}
|
||
|
||
async function updateAsset() {
|
||
const asset = selectedAsset.value
|
||
if (!asset || busy.value) return
|
||
busy.value = true
|
||
actionError.value = ''
|
||
try {
|
||
await projectAssetsApi.update(asset.projectId, asset.id, {
|
||
name: editForm.name.trim(),
|
||
category: editForm.category.trim()
|
||
})
|
||
editOpen.value = false
|
||
selectedAsset.value = null
|
||
await query.refresh()
|
||
} catch (error) {
|
||
actionError.value = errorMessage(error)
|
||
} finally {
|
||
busy.value = false
|
||
}
|
||
}
|
||
|
||
async function removeAsset() {
|
||
const asset = selectedAsset.value
|
||
if (!asset || busy.value) return
|
||
busy.value = true
|
||
actionError.value = ''
|
||
try {
|
||
await projectAssetsApi.remove(asset.projectId, asset.id)
|
||
editOpen.value = false
|
||
selectedAsset.value = null
|
||
await query.refresh()
|
||
} catch (error) {
|
||
actionError.value = errorMessage(error)
|
||
} finally {
|
||
busy.value = false
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<WorkspacePage compact class="asset-library-page">
|
||
<template #header>
|
||
<div class="page-heading asset-library-heading">
|
||
<div>
|
||
<h1>资产库</h1>
|
||
<p class="page-description">汇总并管理所有剧本的人物、场景、道具和参考图片。</p>
|
||
</div>
|
||
<div class="flex items-center gap-2">
|
||
<input
|
||
ref="fileInput"
|
||
class="sr-only"
|
||
type="file"
|
||
accept="image/jpeg,image/png,image/webp"
|
||
aria-label="选择全局资产图片"
|
||
@change="selectFile"
|
||
/>
|
||
<NButton
|
||
type="primary"
|
||
:disabled="busy || !uploadProjectOptions.length"
|
||
@click="fileInput?.click()"
|
||
>
|
||
<template #icon><Upload :size="16" /></template>上传图片
|
||
</NButton>
|
||
<NButton
|
||
quaternary
|
||
class="icon-button"
|
||
:loading="query.loading.value || projectsQuery.loading.value"
|
||
aria-label="刷新资产库"
|
||
title="刷新资产库"
|
||
@click="refreshAll"
|
||
>
|
||
<template #icon><RefreshCw :size="16" /></template>
|
||
</NButton>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<NAlert v-if="fileError" type="error" :show-icon="false" class="mb-4">{{ fileError }}</NAlert>
|
||
<NAlert v-if="query.error.value" type="error" :show-icon="false" class="mb-4">
|
||
{{ query.error.value }}<NButton text class="ml-3" @click="query.refresh">重新连接</NButton>
|
||
</NAlert>
|
||
<NAlert v-if="projectsQuery.error.value" type="warning" :show-icon="false" class="mb-4">
|
||
剧本列表读取失败,暂时不能上传新图片。
|
||
<NButton text class="ml-3" @click="projectsQuery.refresh">重新读取</NButton>
|
||
</NAlert>
|
||
|
||
<div class="asset-library-filters">
|
||
<NInput
|
||
v-model:value="search"
|
||
clearable
|
||
placeholder="搜索图片、分类或所属剧本"
|
||
:input-props="{ 'aria-label': '搜索资产图片' }"
|
||
>
|
||
<template #prefix><Search :size="15" /></template>
|
||
</NInput>
|
||
<NSelect
|
||
v-model:value="projectId"
|
||
clearable
|
||
filterable
|
||
placeholder="全部剧本"
|
||
:options="projectOptions"
|
||
aria-label="筛选所属剧本"
|
||
/>
|
||
<NSelect
|
||
v-model:value="category"
|
||
clearable
|
||
placeholder="全部分类"
|
||
:options="categoryOptions"
|
||
aria-label="筛选资产分类"
|
||
/>
|
||
<span class="text-xs text-muted">{{ filteredAssets.length }} / {{ assets.length }} 张</span>
|
||
</div>
|
||
|
||
<div v-if="filteredAssets.length" class="asset-library-grid">
|
||
<article
|
||
v-for="asset in filteredAssets"
|
||
:key="asset.id"
|
||
class="asset-library-card"
|
||
role="button"
|
||
tabindex="0"
|
||
:aria-label="`查看 ${asset.name} 详情`"
|
||
@click="openEdit(asset)"
|
||
@keydown.enter.prevent="openEdit(asset)"
|
||
@keydown.space.prevent="openEdit(asset)"
|
||
>
|
||
<AssetImage :src="asset.publicUrl" :alt="asset.name" class="asset-library-image" />
|
||
<div class="asset-library-overlay">
|
||
<NTooltip placement="top-start" trigger="hover" :disabled="!overflowTitles.has(asset.id)">
|
||
<template #trigger>
|
||
<h2 class="asset-library-title" @mouseenter="checkTitleOverflow(asset.id, $event)">
|
||
{{ asset.name }}
|
||
</h2>
|
||
</template>
|
||
{{ asset.name }}
|
||
</NTooltip>
|
||
<div class="asset-library-meta">
|
||
<p class="truncate text-xs" :title="projectName(asset.project)">
|
||
{{ projectName(asset.project) }}
|
||
</p>
|
||
<NTag size="small" :bordered="false" class="asset-library-tag">
|
||
{{ categoryLabel(asset.category) }}
|
||
</NTag>
|
||
</div>
|
||
</div>
|
||
</article>
|
||
</div>
|
||
|
||
<EmptyState
|
||
v-else-if="query.data.value"
|
||
:title="assets.length ? '没有匹配的图片' : '资产库还是空的'"
|
||
:description="assets.length ? '调整搜索、剧本或分类筛选。' : '选择剧本并上传第一张图片。'"
|
||
>
|
||
<NButton v-if="assets.length" @click="clearFilters">清除筛选</NButton>
|
||
<NButton v-else type="primary" :disabled="!uploadProjectOptions.length" @click="fileInput?.click()">
|
||
上传第一张图片
|
||
</NButton>
|
||
</EmptyState>
|
||
<p v-else class="py-10 text-center text-sm text-muted" role="status">正在读取全部资产图片…</p>
|
||
|
||
<AppDialog
|
||
v-model:open="uploadOpen"
|
||
title="上传资产图片"
|
||
description="选择所属剧本并确认名称和分类,提交后仍停留在当前资产库。"
|
||
:busy="busy"
|
||
>
|
||
<NAlert v-if="actionError" type="error" :show-icon="false" class="mt-5">{{ actionError }}</NAlert>
|
||
<AppForm :model="uploadForm" :rules="formRules" :disabled="busy" class="mt-5" @submit="uploadAsset">
|
||
<p class="mb-4 text-sm">{{ selectedFile?.name }} · {{ formatBytes(selectedFile?.size ?? 0) }}</p>
|
||
<NFormItem path="projectId" label="所属剧本">
|
||
<NSelect v-model:value="uploadForm.projectId" filterable :options="uploadProjectOptions" />
|
||
</NFormItem>
|
||
<NFormItem path="name" label="素材名称"><NInput v-model:value="uploadForm.name" /></NFormItem>
|
||
<NFormItem path="category" label="分类标识">
|
||
<NInput v-model:value="uploadForm.category" placeholder="例如 character、scene、prop" />
|
||
</NFormItem>
|
||
<div class="mt-4 flex justify-end gap-3">
|
||
<NButton :disabled="busy" @click="uploadOpen = false">取消</NButton>
|
||
<NButton type="primary" attr-type="submit" :loading="busy">确认上传</NButton>
|
||
</div>
|
||
</AppForm>
|
||
</AppDialog>
|
||
|
||
<AppDialog
|
||
v-model:open="editOpen"
|
||
title="素材详情与管理"
|
||
description="查看文件详情,也可在当前页面修改名称和分类或删除素材。"
|
||
:busy="busy"
|
||
wide
|
||
>
|
||
<NAlert v-if="actionError" type="error" :show-icon="false" class="mt-5">{{ actionError }}</NAlert>
|
||
<AppForm :model="editForm" :rules="formRules" :disabled="busy" class="mt-5" @submit="updateAsset">
|
||
<div v-if="selectedAsset" class="asset-management-layout">
|
||
<AssetImage
|
||
:src="selectedAsset.publicUrl"
|
||
:alt="selectedAsset.name"
|
||
preview
|
||
object-fit="contain"
|
||
class="asset-management-preview"
|
||
/>
|
||
<div class="asset-management-fields">
|
||
<NFormItem label="所属剧本">
|
||
<NInput :value="projectName(selectedAsset.project)" disabled />
|
||
</NFormItem>
|
||
<NFormItem path="name" label="素材名称"><NInput v-model:value="editForm.name" /></NFormItem>
|
||
<NFormItem path="category" label="分类标识">
|
||
<NInput v-model:value="editForm.category" />
|
||
</NFormItem>
|
||
<dl class="asset-detail-grid">
|
||
<dt>原始文件名</dt>
|
||
<dd>{{ metadataOriginalName(selectedAsset.metadata) }}</dd>
|
||
<dt>文件类型</dt>
|
||
<dd>{{ selectedAsset.mimeType }} · {{ selectedAsset.extension.toUpperCase() }}</dd>
|
||
<dt>文件大小</dt>
|
||
<dd>{{ formatBytes(selectedAsset.size) }}</dd>
|
||
<dt>创建时间</dt>
|
||
<dd>{{ formatDate(selectedAsset.createdAt) }}</dd>
|
||
<dt>更新时间</dt>
|
||
<dd>{{ formatDate(selectedAsset.updatedAt) }}</dd>
|
||
</dl>
|
||
</div>
|
||
</div>
|
||
<div class="asset-management-footer">
|
||
<NPopconfirm
|
||
:positive-button-props="{ disabled: busy }"
|
||
positive-text="确认删除"
|
||
negative-text="取消"
|
||
@positive-click="removeAsset"
|
||
>
|
||
<template #trigger>
|
||
<NButton class="text-danger" :disabled="busy">
|
||
<template #icon><Trash2 :size="14" /></template>删除素材
|
||
</NButton>
|
||
</template>
|
||
删除后物理文件也会移除;正在被视觉风格引用的素材会由后端阻止删除。
|
||
</NPopconfirm>
|
||
<div class="flex gap-3">
|
||
<NButton :disabled="busy" @click="editOpen = false">取消</NButton>
|
||
<NButton type="primary" attr-type="submit" :loading="busy">保存修改</NButton>
|
||
</div>
|
||
</div>
|
||
</AppForm>
|
||
</AppDialog>
|
||
</WorkspacePage>
|
||
</template>
|
||
|
||
<style scoped>
|
||
@reference "../../styles/styles.css";
|
||
.asset-library-page {
|
||
container: asset-library / inline-size;
|
||
}
|
||
.asset-library-heading {
|
||
@apply flex items-start justify-between gap-4;
|
||
}
|
||
.asset-library-filters {
|
||
@apply grid grid-cols-[minmax(220px,_420px)_220px_180px_auto] items-center gap-3 mb-4 p-3 bg-(--app-subtle);
|
||
}
|
||
.asset-library-grid {
|
||
column-width: 240px;
|
||
column-gap: 16px;
|
||
}
|
||
.asset-library-card {
|
||
@apply relative inline-block w-full min-w-0 overflow-hidden bg-(--app-surface) align-top cursor-pointer outline-none;
|
||
break-inside: avoid;
|
||
margin-bottom: 16px;
|
||
}
|
||
.asset-library-card:focus-visible {
|
||
box-shadow: inset 0 0 0 2px var(--color-accent);
|
||
}
|
||
.asset-library-image {
|
||
@apply block aspect-auto bg-transparent;
|
||
}
|
||
.asset-library-image :deep(.reveal-image),
|
||
.asset-library-image :deep(.n-image) {
|
||
@apply block h-auto min-h-0 bg-transparent;
|
||
}
|
||
.asset-library-image :deep(.n-image img) {
|
||
@apply block w-full h-auto min-h-0 object-contain;
|
||
}
|
||
.asset-library-overlay {
|
||
@apply absolute inset-0 flex flex-col justify-end p-[10px] text-white;
|
||
background: linear-gradient(to top, rgb(0 0 0 / 0.84) 0, rgb(0 0 0 / 0.35) 42%, transparent 70%);
|
||
}
|
||
.asset-library-title {
|
||
@apply block w-full truncate text-sm font-semibold;
|
||
}
|
||
.asset-library-meta {
|
||
@apply flex min-w-0 items-center justify-between gap-2 mt-1.5;
|
||
}
|
||
.asset-library-meta p {
|
||
color: rgb(255 255 255 / 0.76);
|
||
}
|
||
.asset-library-tag {
|
||
@apply shrink-0 text-white bg-white/15 backdrop-blur-sm;
|
||
}
|
||
.asset-management-layout {
|
||
@apply grid items-start gap-5;
|
||
grid-template-columns: max-content minmax(0, 1fr);
|
||
}
|
||
.asset-management-preview {
|
||
--asset-detail-image-max-height: min(56dvh, 560px);
|
||
display: block;
|
||
width: fit-content;
|
||
max-width: 100%;
|
||
height: auto;
|
||
min-height: 0;
|
||
aspect-ratio: auto;
|
||
justify-self: start;
|
||
}
|
||
.asset-management-preview :deep(.reveal-image),
|
||
.asset-management-preview :deep(.n-image) {
|
||
display: block;
|
||
width: fit-content;
|
||
max-width: 100%;
|
||
height: auto;
|
||
min-height: 0;
|
||
}
|
||
.asset-management-preview :deep(.n-image img) {
|
||
display: block;
|
||
width: auto;
|
||
max-width: 100%;
|
||
height: auto;
|
||
max-height: var(--asset-detail-image-max-height);
|
||
object-fit: contain;
|
||
}
|
||
.asset-management-fields {
|
||
@apply min-w-0;
|
||
}
|
||
.asset-management-fields :deep(.n-form-item) {
|
||
@apply mb-4;
|
||
}
|
||
.asset-management-fields :deep(.n-form-item-label) {
|
||
@apply pb-2;
|
||
}
|
||
.asset-detail-grid {
|
||
@apply grid grid-cols-[88px_minmax(0,_1fr)] gap-x-4 gap-y-2 mt-1 p-3 bg-(--app-subtle) text-xs;
|
||
}
|
||
.asset-detail-grid dt {
|
||
@apply text-muted;
|
||
}
|
||
.asset-detail-grid dd {
|
||
@apply min-w-0 break-all text-ink;
|
||
}
|
||
.asset-management-footer {
|
||
@apply flex items-center justify-between gap-3 mt-5 pt-4;
|
||
}
|
||
@container asset-library (max-width: 840px) {
|
||
.asset-library-filters {
|
||
@apply grid-cols-2;
|
||
}
|
||
.asset-management-layout {
|
||
@apply grid-cols-1;
|
||
}
|
||
.asset-management-preview {
|
||
--asset-detail-image-max-height: min(42dvh, 420px);
|
||
}
|
||
}
|
||
@container asset-library (max-width: 560px) {
|
||
.asset-library-heading {
|
||
@apply items-start;
|
||
}
|
||
.asset-library-filters {
|
||
@apply grid-cols-1;
|
||
}
|
||
.asset-library-filters > span {
|
||
@apply justify-self-start;
|
||
}
|
||
}
|
||
</style>
|