Let users copy images next to download on shared attachment cards, preview toolbars, and list cells so they can paste into chat or docs without saving files first. Co-authored-by: Cursor <cursoragent@cursor.com>
341 lines
9.0 KiB
Vue
341 lines
9.0 KiB
Vue
<template>
|
|
<div v-if="items.length" class="pl-plan-document-attachment-cell">
|
|
<div
|
|
v-for="item in items"
|
|
:key="item.key"
|
|
class="pl-plan-document-attachment-cell__item"
|
|
:title="item.fullLabel"
|
|
>
|
|
<div class="pl-plan-document-attachment-cell__thumb">
|
|
<div
|
|
class="pl-plan-document-attachment-cell__preview"
|
|
:title="item.isImage || item.isPdf ? '点击预览' : '点击查看'"
|
|
@click.stop="handlePreview(item)"
|
|
>
|
|
<el-image
|
|
v-if="item.isImage && !usePlaceholderThumb && mediaActive"
|
|
class="pl-plan-document-attachment-cell__image pl-plan-document-attachment-cell__image--passive"
|
|
fit="cover"
|
|
lazy
|
|
:src="displaySrc(item.url)"
|
|
:preview-src-list="[]"
|
|
>
|
|
<template #error>
|
|
<div class="pl-plan-document-attachment-cell__placeholder is-clickable is-common-thumb">
|
|
<el-icon :size="18" color="#10b981"><Picture /></el-icon>
|
|
</div>
|
|
</template>
|
|
</el-image>
|
|
<div
|
|
v-else
|
|
class="pl-plan-document-attachment-cell__placeholder is-clickable"
|
|
:class="{ 'is-common-thumb': usePlaceholderThumb }"
|
|
>
|
|
<el-icon :size="18" :color="usePlaceholderThumb ? '#10b981' : item.isPdf ? '#0284c7' : '#10b981'">
|
|
<Picture v-if="usePlaceholderThumb || item.isImage" />
|
|
<Document v-else />
|
|
</el-icon>
|
|
<span v-if="!usePlaceholderThumb && item.ext" class="pl-plan-document-attachment-cell__ext">{{
|
|
item.ext
|
|
}}</span>
|
|
</div>
|
|
</div>
|
|
<el-icon
|
|
class="pl-plan-document-attachment-cell__copy"
|
|
:title="item.isImage ? '复制图片' : '复制链接'"
|
|
@click.stop="handleCopy(item)"
|
|
>
|
|
<DocumentCopy />
|
|
</el-icon>
|
|
<el-icon
|
|
class="pl-plan-document-attachment-cell__download"
|
|
title="下载"
|
|
@click.stop="handleDownload(item)"
|
|
>
|
|
<Download />
|
|
</el-icon>
|
|
<span class="pl-plan-document-attachment-cell__caption">{{ item.label }}</span>
|
|
</div>
|
|
</div>
|
|
|
|
<AttachmentPreviewDialog
|
|
v-model="previewVisible"
|
|
:file-url="previewUrl"
|
|
:file-name="previewName"
|
|
:resolve-url="resolvePhotoDisplayUrl"
|
|
/>
|
|
</div>
|
|
<span v-else class="pl-plan-document-attachment-cell__none">—</span>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { computed, ref } from 'vue'
|
|
import { Document, DocumentCopy, Download, Picture } from '@element-plus/icons-vue'
|
|
import { ElMessage } from 'element-plus'
|
|
import type { PlanDocumentAttachmentItem } from '@/api/pl'
|
|
import AttachmentPreviewDialog from '@/components/business/attachment-preview-dialog/index.vue'
|
|
import {
|
|
downloadAttachmentFile,
|
|
openAttachmentPreviewInNewTab
|
|
} from '@/utils/download-attachment'
|
|
import {
|
|
copyAttachmentImageToClipboard,
|
|
copyTextToClipboard
|
|
} from '@/utils/clipboard'
|
|
import { resolvePhotoDisplayUrl } from '@/utils/pl-photo'
|
|
|
|
const props = withDefaults(
|
|
defineProps<{
|
|
attachments?: PlanDocumentAttachmentItem[] | null
|
|
/** KeepAlive 离开页时置 false,暂停缩略图请求 */
|
|
mediaActive?: boolean
|
|
/** 列表态仅用统一占位缩略图,点击预览/下载时再加载真实文件 */
|
|
usePlaceholderThumb?: boolean
|
|
}>(),
|
|
{
|
|
attachments: () => [],
|
|
mediaActive: true,
|
|
usePlaceholderThumb: false
|
|
}
|
|
)
|
|
|
|
type ItemDef = {
|
|
key: string
|
|
label: string
|
|
fullLabel: string
|
|
url: string
|
|
isImage: boolean
|
|
isPdf: boolean
|
|
ext: string
|
|
}
|
|
|
|
const previewVisible = ref(false)
|
|
const previewUrl = ref('')
|
|
const previewName = ref('')
|
|
|
|
function isImageUrl(url: string) {
|
|
const path = String(url || '')
|
|
.split('?')[0]
|
|
.toLowerCase()
|
|
return /\.(png|jpe?g|gif|webp|bmp|svg)$/i.test(path)
|
|
}
|
|
|
|
function isPdfUrl(url: string) {
|
|
const path = String(url || '')
|
|
.split('?')[0]
|
|
.toLowerCase()
|
|
return /\.pdf$/i.test(path)
|
|
}
|
|
|
|
function fileExt(url: string) {
|
|
const path = String(url || '')
|
|
.split('?')[0]
|
|
.toLowerCase()
|
|
const match = path.match(/\.([a-z0-9]+)$/i)
|
|
return match ? match[1].toUpperCase() : ''
|
|
}
|
|
|
|
function fileNameFromUrl(url: string) {
|
|
return (
|
|
String(url || '')
|
|
.split('?')[0]
|
|
.split('/')
|
|
.pop() || '附件'
|
|
)
|
|
}
|
|
|
|
function displaySrc(url: string) {
|
|
return resolvePhotoDisplayUrl(url) || url
|
|
}
|
|
|
|
async function handlePreview(item: ItemDef) {
|
|
if (!item.url) return
|
|
if (item.isImage || item.isPdf) {
|
|
previewUrl.value = item.url
|
|
previewName.value = item.fullLabel || fileNameFromUrl(item.url)
|
|
previewVisible.value = true
|
|
return
|
|
}
|
|
await openAttachmentPreviewInNewTab(
|
|
{ fileUrl: item.url, fileName: fileNameFromUrl(item.url) },
|
|
{
|
|
fullFileUrl: resolvePhotoDisplayUrl,
|
|
kind: 'other'
|
|
}
|
|
)
|
|
}
|
|
|
|
async function handleDownload(item: ItemDef) {
|
|
if (!item.url) {
|
|
ElMessage.error('无法获取文件下载地址')
|
|
return
|
|
}
|
|
try {
|
|
await downloadAttachmentFile(
|
|
{ fileUrl: item.url, fileName: fileNameFromUrl(item.url) },
|
|
{ fullFileUrl: resolvePhotoDisplayUrl }
|
|
)
|
|
ElMessage.success('下载成功')
|
|
} catch {
|
|
ElMessage.error('下载失败,请稍后重试')
|
|
}
|
|
}
|
|
|
|
async function handleCopy(item: ItemDef) {
|
|
if (!item.url) {
|
|
ElMessage.warning('暂无可复制内容')
|
|
return
|
|
}
|
|
const file = { fileUrl: item.url, fileName: fileNameFromUrl(item.url) }
|
|
if (item.isImage) {
|
|
const ok = await copyAttachmentImageToClipboard(file, {
|
|
fullFileUrl: resolvePhotoDisplayUrl
|
|
})
|
|
if (ok) ElMessage.success('已复制到剪贴板')
|
|
else ElMessage.warning('复制失败,请允许剪贴板权限或改用下载')
|
|
return
|
|
}
|
|
const href = resolvePhotoDisplayUrl(item.url) || item.url
|
|
const ok = await copyTextToClipboard(href)
|
|
if (ok) ElMessage.success('已复制链接')
|
|
else ElMessage.warning('复制失败,请允许剪贴板权限或改用下载')
|
|
}
|
|
|
|
const items = computed<ItemDef[]>(() => {
|
|
const list = props.attachments || []
|
|
return list
|
|
.filter((item) => String(item?.url || '').trim())
|
|
.map((item, index) => {
|
|
const url = String(item.url).trim()
|
|
const fullLabel = String(item.label || '附件').trim() || '附件'
|
|
const label = fullLabel.length > 6 ? `${fullLabel.slice(0, 5)}…` : fullLabel
|
|
return {
|
|
key: `${url}-${index}`,
|
|
label,
|
|
fullLabel,
|
|
url,
|
|
isImage: isImageUrl(url),
|
|
isPdf: isPdfUrl(url),
|
|
ext: fileExt(url)
|
|
}
|
|
})
|
|
})
|
|
</script>
|
|
|
|
<style scoped lang="scss">
|
|
.pl-plan-document-attachment-cell {
|
|
display: flex;
|
|
align-items: flex-start;
|
|
gap: 8px;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.pl-plan-document-attachment-cell__item {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
width: 72px;
|
|
}
|
|
|
|
.pl-plan-document-attachment-cell__thumb {
|
|
position: relative;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 72px;
|
|
height: 52px;
|
|
overflow: hidden;
|
|
border: 1px solid var(--border, #d9e8e2);
|
|
border-radius: 8px;
|
|
background: var(--table-head, #f8fafc);
|
|
}
|
|
|
|
.pl-plan-document-attachment-cell__preview {
|
|
width: 100%;
|
|
height: 100%;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.pl-plan-document-attachment-cell__image {
|
|
display: block;
|
|
width: 100%;
|
|
height: 100%;
|
|
|
|
&--passive {
|
|
pointer-events: none;
|
|
}
|
|
}
|
|
|
|
.pl-plan-document-attachment-cell__placeholder {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 100%;
|
|
height: 100%;
|
|
background: var(--primary-bg, #ecfdf5);
|
|
|
|
&.is-clickable {
|
|
cursor: pointer;
|
|
}
|
|
|
|
&.is-common-thumb {
|
|
background: var(--primary-bg, #ecfdf5);
|
|
}
|
|
}
|
|
|
|
.pl-plan-document-attachment-cell__ext {
|
|
margin-top: 2px;
|
|
font-size: 10px;
|
|
line-height: 1;
|
|
color: #64748b;
|
|
}
|
|
|
|
.pl-plan-document-attachment-cell__download,
|
|
.pl-plan-document-attachment-cell__copy {
|
|
position: absolute;
|
|
top: 2px;
|
|
z-index: 2;
|
|
width: 18px;
|
|
height: 18px;
|
|
border-radius: 50%;
|
|
background: #fff;
|
|
border: 1px solid #e2e8f0;
|
|
color: #1e293b;
|
|
cursor: pointer;
|
|
font-size: 11px;
|
|
|
|
&:hover {
|
|
color: #1ca851;
|
|
border-color: #1ca851;
|
|
background: #e6f8ed;
|
|
}
|
|
}
|
|
|
|
.pl-plan-document-attachment-cell__download {
|
|
right: 2px;
|
|
}
|
|
|
|
.pl-plan-document-attachment-cell__copy {
|
|
right: 22px;
|
|
}
|
|
|
|
.pl-plan-document-attachment-cell__caption {
|
|
position: absolute;
|
|
right: 0;
|
|
bottom: 0;
|
|
left: 0;
|
|
padding: 2px 3px;
|
|
background: rgba(15, 23, 42, 0.62);
|
|
color: #fff;
|
|
font-size: 11px;
|
|
line-height: 1.2;
|
|
text-align: center;
|
|
word-break: keep-all;
|
|
}
|
|
|
|
.pl-plan-document-attachment-cell__none {
|
|
color: var(--mint-text);
|
|
}
|
|
</style>
|