96 lines
14 KiB
Vue
96 lines
14 KiB
Vue
<template>
|
||
<section class="document-library" aria-label="项目文件列表">
|
||
<template v-if="documents.length">
|
||
<nav class="document-tabs" aria-label="文件分类">
|
||
<button type="button" :class="{active:!category}" :aria-pressed="!category" @click="category=''">全部 <span>{{ documents.length }}</span></button>
|
||
<button v-for="c in visibleCategories" :key="c[0]" type="button" :class="{active:category===c[0]}" :aria-pressed="category===c[0]" @click="category=c[0]">{{ c[1] }} <span>{{ counts[c[0]]||0 }}</span></button>
|
||
</nav>
|
||
<div class="library-filters">
|
||
<el-input v-model="keyword" clearable :prefix-icon="Search" maxlength="100" placeholder="搜索文件名或材料名称" aria-label="搜索文件"/>
|
||
<el-checkbox v-model="finalOnly">仅看最终版</el-checkbox>
|
||
<el-select v-model="sort" aria-label="文件排序"><el-option value="updated" label="最近上传优先"/><el-option value="name" label="按文件名称"/></el-select>
|
||
</div>
|
||
<div class="library-summary"><span>{{ filtered.length }} 份材料<span v-if="filtered.length!==documents.length"> / 共 {{ documents.length }} 份</span></span><span>{{ versionCount }} 个文件版本</span></div>
|
||
<template v-if="filtered.length">
|
||
<div class="library-header" aria-hidden="true"><span>文件</span><span>查看版本</span><span>操作</span></div>
|
||
<div class="library-list" role="list">
|
||
<article v-for="doc in pageRows" :key="doc.id" class="library-row" role="listitem" :aria-label="doc.title">
|
||
<div class="file-identity">
|
||
<div :class="['file-symbol',fileTone(file(doc))]" aria-hidden="true"><el-icon :size="20"><Document/></el-icon></div>
|
||
<div class="file-description">
|
||
<div class="file-name-line"><el-button v-if="file(doc)" link type="primary" class="file-name" :disabled="busy" :title="doc.title===file(doc).name?doc.title:`材料:${doc.title}\n文件:${file(doc).name}`" @click="emitAction(previewable(file(doc))?'preview':'download',doc)">{{ file(doc).name }}</el-button><strong v-else class="file-name" :title="doc.title">{{ doc.title }}</strong></div>
|
||
<div class="file-meta"><span v-if="!category" class="file-category">{{ categoryName(doc.category) }}</span><span v-if="file(doc)">{{ formatSize(file(doc).size) }}</span><span>{{ file(doc)?.createBy||doc.createBy||'—' }}</span><time :title="formatDateTime(file(doc)?.createTime||doc.createTime)">{{ formatTime(file(doc)?.createTime||doc.createTime) }}</time></div>
|
||
<div v-if="normalize(keyword)&&normalize(doc.title).includes(normalize(keyword))&&!normalize(file(doc)?.name).includes(normalize(keyword))" class="material-title" :title="doc.title">匹配材料名:{{ doc.title }}</div>
|
||
<div v-if="historicalMatch(doc)" class="history-match" :title="historicalMatch(doc)">匹配历史版本:{{ historicalMatch(doc) }}</div>
|
||
</div>
|
||
</div>
|
||
<div class="file-version">
|
||
<template v-if="finalOnly"><el-tag type="success" size="small" effect="plain">最终版 · 第 {{ file(doc)?.revision }} 版</el-tag></template>
|
||
<el-select v-else-if="doc.files?.length" :model-value="file(doc)?.id" :disabled="busy" :aria-label="`文件版本:${doc.title}`" popper-class="project-document-versions" @update:model-value="value=>emit('select-version',doc.id,value)">
|
||
<el-option v-for="version in doc.files" :key="version.id" :value="version.id" :label="versionLabel(doc,version)"><div class="version-choice"><span>{{ versionLabel(doc,version) }}</span><small :title="version.name">{{ version.name }}</small></div></el-option>
|
||
</el-select>
|
||
<span v-else class="pending-file">待上传文件</span>
|
||
<div v-if="!finalOnly" class="version-meta"><span v-if="doc.files?.length>1" class="version-count">共 {{ doc.files.length }} 个版本</span><el-button v-if="doc.finalFileId&&file(doc)?.id!==doc.finalFileId" link type="success" :disabled="busy" @click="emit('select-version',doc.id,doc.finalFileId)">查看最终版</el-button></div>
|
||
</div>
|
||
<div class="file-actions">
|
||
<el-button v-if="previewable(file(doc))" link type="primary" :disabled="busy" @click="emitAction('preview',doc)">预览</el-button>
|
||
<el-button v-if="file(doc)" link type="primary" :disabled="busy" @click="emitAction('download',doc)">下载</el-button>
|
||
<el-dropdown v-if="hasMore(doc)" trigger="click" :disabled="busy" @command="command=>emitAction(command,doc)"><el-button text :icon="MoreFilled" :disabled="busy" :aria-label="`更多操作:${doc.title}`"/><template #dropdown><el-dropdown-menu>
|
||
<el-dropdown-item v-if="writable(doc)" command="version">上传新版本</el-dropdown-item>
|
||
<el-dropdown-item v-if="manageable(doc)&&file(doc)" command="finalize" :disabled="file(doc).id===doc.finalFileId">设为最终版</el-dropdown-item>
|
||
<el-dropdown-item v-if="manageable(doc)&&doc.finalFileId" command="revoke">撤回最终版</el-dropdown-item>
|
||
<el-dropdown-item v-if="writable(doc)" command="remove" :divided="!!file(doc)">移除材料</el-dropdown-item>
|
||
</el-dropdown-menu></template></el-dropdown>
|
||
</div>
|
||
</article>
|
||
</div>
|
||
<el-pagination v-if="filtered.length>pageSize" class="library-pagination" v-model:current-page="page" :page-size="pageSize" :total="filtered.length" layout="prev, pager, next" :pager-count="5"/>
|
||
</template>
|
||
<el-empty v-else description="没有匹配的文件" :image-size="70"><el-button @click="reset">清除筛选</el-button></el-empty>
|
||
</template>
|
||
<el-empty v-else :description="emptyDescription" :image-size="88"/>
|
||
</section>
|
||
</template>
|
||
<script setup>
|
||
import { formatDateTime } from '@/utils/display-format'
|
||
import {computed,ref,watch} from 'vue'
|
||
import {Document,Search,MoreFilled} from '@element-plus/icons-vue'
|
||
const props=defineProps({documents:{type:Array,default:()=>[]},categories:{type:Array,default:()=>[]},selected:{type:Object,default:()=>({})},busy:Boolean,canWrite:Boolean,readOnly:Boolean,emptyDescription:{type:String,default:'暂无文件'}})
|
||
const emit=defineEmits(['select-version','action'])
|
||
const keyword=ref(''),category=ref(''),finalOnly=ref(false),sort=ref('updated'),page=ref(1),pageSize=20
|
||
const normalize=value=>String(value||'').normalize('NFKC').replace(/\s+/gu,'').toLocaleLowerCase()
|
||
const counts=computed(()=>props.documents.reduce((result,doc)=>{result[doc.category]=(result[doc.category]||0)+1;return result},{}))
|
||
const visibleCategories=computed(()=>{const known=new Set(props.categories.map(c=>c[0]));return [...props.categories,...Object.keys(counts.value).filter(key=>!known.has(key)).map(key=>[key,key])]})
|
||
function file(doc){return doc.files?.find(item=>item.id===(finalOnly.value?doc.finalFileId:props.selected[doc.id]))||(!finalOnly.value?(doc.files?.find(item=>item.id===doc.currentFileId)||doc.files?.[0]):undefined)}
|
||
function categoryName(value){return visibleCategories.value.find(c=>c[0]===value)?.[1]||value}
|
||
function latestTime(doc){return doc.files?.reduce((time,item)=>String(item.createTime||'')>time?String(item.createTime):time,String(doc.createTime||''))||String(doc.createTime||'')}
|
||
function sortName(doc){return doc.files?.find(item=>item.id===(finalOnly.value?doc.finalFileId:doc.currentFileId))?.name||doc.title||''}
|
||
const filtered=computed(()=>{
|
||
const q=normalize(keyword.value)
|
||
return props.documents.filter(doc=>(!category.value||doc.category===category.value)&&(!finalOnly.value||!!file(doc))&&(!q||normalize(doc.title).includes(q)||(finalOnly.value?[file(doc)]:doc.files||[]).some(item=>normalize(item?.name).includes(q))))
|
||
.sort((a,b)=>sort.value==='name'?sortName(a).localeCompare(sortName(b),'zh-CN')||Number(b.id)-Number(a.id):latestTime(b).localeCompare(latestTime(a))||Number(b.id)-Number(a.id))
|
||
})
|
||
const pageRows=computed(()=>filtered.value.slice((page.value-1)*pageSize,page.value*pageSize))
|
||
const versionCount=computed(()=>filtered.value.reduce((total,doc)=>total+(finalOnly.value?1:doc.files?.length||0),0))
|
||
function historicalMatch(doc){const q=normalize(keyword.value);if(!q||normalize(doc.title).includes(q)||normalize(file(doc)?.name).includes(q)||finalOnly.value)return '';return doc.files?.find(item=>normalize(item.name).includes(q))?.name||''}
|
||
function versionLabel(doc,item){return `第 ${item.revision} 版${item.id===doc.currentFileId?' · 最新':''}${item.id===doc.finalFileId?' · 最终版':''}`}
|
||
function previewable(item){return ['application/pdf','image/png','image/jpeg'].includes(item?.contentType)}
|
||
function fileTone(item){if(item?.contentType==='application/pdf')return 'pdf';if(item?.contentType?.startsWith('image/'))return 'image';return 'other'}
|
||
function formatTime(value){return formatDateTime(value).slice(0,16)}
|
||
function formatSize(value){const size=Number(value)||0;return size<1024*1024?`${Math.max(1,Math.round(size/1024))} KB`:`${(size/1024/1024).toFixed(2)} MB`}
|
||
function writable(doc){return props.canWrite&&!props.readOnly&&doc.canWrite}
|
||
function manageable(doc){return props.canWrite&&!props.readOnly&&doc.canManage}
|
||
function hasMore(doc){return writable(doc)||(manageable(doc)&&(file(doc)||doc.finalFileId))}
|
||
function emitAction(action,doc){if(props.busy)return;emit('action',action,doc,file(doc)?.id)}
|
||
function reset(){keyword.value='';category.value='';finalOnly.value=false;sort.value='updated';page.value=1}
|
||
watch([keyword,category,finalOnly,sort],()=>{page.value=1},{flush:'sync'})
|
||
watch(()=>filtered.value.length,length=>{page.value=Math.max(1,Math.min(page.value,Math.ceil(length/pageSize)))},{flush:'sync'})
|
||
</script>
|
||
<style scoped>
|
||
.document-library{container:document-library / inline-size;min-width:0}.document-tabs{display:flex;gap:22px;overflow-x:auto;border-bottom:1px solid var(--el-border-color-lighter);margin-bottom:14px}.document-tabs button{flex-shrink:0;font:inherit;font-size:13px;border:0;border-bottom:2px solid transparent;background:none;cursor:pointer;padding:10px 0;color:var(--el-text-color-secondary)}.document-tabs button.active{color:var(--el-color-primary);border-bottom-color:var(--el-color-primary);font-weight:600}.document-tabs button span{margin-left:4px;font-size:12px;color:var(--el-text-color-placeholder)}.document-tabs button:focus-visible{outline:2px solid var(--el-color-primary);outline-offset:-3px}.library-filters{display:flex;align-items:center;gap:14px;flex-wrap:wrap}.library-filters>.el-input{flex:1 1 220px;min-width:150px}.library-filters>.el-select{width:150px}.library-filters>.el-checkbox{margin:0}.library-summary{display:flex;justify-content:space-between;gap:10px;color:var(--el-text-color-secondary);font-size:12px;margin:14px 0 10px}.library-header,.library-row{display:grid;grid-template-columns:minmax(0,1fr) 170px 132px;gap:18px;align-items:center}.library-header{font-size:12px;color:var(--el-text-color-secondary);background:var(--el-fill-color-light);padding:10px 12px;border-radius:6px}.library-header>span:last-child{text-align:right}.library-list{max-height:480px;overflow-y:auto;scrollbar-gutter:stable}.library-row{padding:15px 12px;border-bottom:1px solid var(--el-border-color-lighter)}.library-row:hover{background:var(--el-fill-color-extra-light)}.file-identity{display:flex;align-items:flex-start;gap:10px;min-width:0}.file-symbol{width:34px;height:40px;border-radius:7px;display:grid;place-items:center;flex-shrink:0;background:var(--el-color-primary-light-9);color:var(--el-color-primary)}.file-symbol.pdf{background:var(--el-color-danger-light-9);color:var(--el-color-danger)}.file-symbol.image{background:var(--el-color-success-light-9);color:var(--el-color-success)}.file-description{min-width:0;flex:1}.file-name-line{display:flex;align-items:center;min-width:0}.file-name{display:block;max-width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:13px;line-height:1.5;text-align:left;padding:0;height:auto}.file-name :deep(span){display:block;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.file-meta{display:flex;align-items:baseline;flex-wrap:wrap;gap:3px 10px;margin-top:6px;color:var(--el-text-color-secondary);font-size:11px;line-height:1.5}.file-meta>span,.file-meta>time{white-space:nowrap}.file-category{color:var(--el-color-primary)}.material-title,.history-match{font-size:11px;color:var(--el-text-color-secondary);margin-top:5px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.history-match{color:var(--el-color-warning)}.file-version{min-width:0}.file-version .el-select{width:100%}.version-count{display:block;margin-top:5px;font-size:11px;color:var(--el-text-color-secondary)}.pending-file{color:var(--el-color-warning);font-size:12px}.file-actions{display:flex;align-items:center;justify-content:flex-end;gap:12px}.file-actions .el-button{margin:0;font-size:12px}.file-actions .el-dropdown>.el-button{padding:5px;min-height:26px}.library-pagination{justify-content:flex-end;margin-top:16px}.version-choice{display:flex;flex-direction:column;max-width:340px;line-height:1.5;padding:3px 0}.version-choice>small{font-size:11px;font-weight:400;color:var(--el-text-color-secondary);max-width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}:deep(.el-select-dropdown__item:has(.version-choice)){height:auto;min-height:48px}
|
||
@container document-library (max-width:620px){.library-header{display:none}.library-row{grid-template-columns:minmax(0,1fr) auto;gap:10px 12px;padding:14px 2px}.file-identity{grid-column:1;grid-row:1;gap:8px}.file-actions{grid-column:2;grid-row:1 / span 2;gap:10px;flex-wrap:wrap;max-width:94px}.file-version{grid-column:1;grid-row:2;display:flex;align-items:center;gap:8px;padding-left:42px}.file-version .el-select{width:158px;max-width:100%}.version-count{margin:0;white-space:nowrap}.library-summary>span:last-child{display:none}.library-filters{gap:8px 12px}.library-filters>.el-input{flex-basis:100%}.library-filters>.el-select{margin-left:auto}.library-list{max-height:56vh}.file-meta{gap:3px 8px}.file-symbol{width:30px;height:36px}.file-actions .el-dropdown{margin-left:auto}}
|
||
@container document-library (max-width:380px){.file-version{padding-left:0}.version-count{display:none}.file-actions{max-width:70px;gap:8px}.file-meta time{font-size:10px}}
|
||
.document-tabs{scrollbar-width:thin}@container document-library (max-width:620px){.library-list{max-height:none;overflow:visible;scrollbar-gutter:auto}}
|
||
.version-meta{display:flex;align-items:center;gap:8px;margin-top:5px;flex-wrap:wrap}.version-meta .version-count{margin:0}.version-meta .el-button{font-size:12px;margin:0;padding:0;min-height:0;height:auto}.file-meta{font-size:12px;color:var(--el-text-color-regular)}.version-count{font-size:12px}:global(.project-document-versions .el-select-dropdown__item){height:auto;min-height:48px}@container document-library (max-width:620px){.file-version{flex-wrap:wrap}.version-meta{margin:0}.file-meta time{font-size:12px}}
|
||
</style>
|