|
|
|
@ -14,15 +14,15 @@ |
|
|
|
import { watch, ref } from 'vue'; |
|
|
|
import FileList from './FileList.vue'; |
|
|
|
import { BasicModal, useModalInner } from '@/components/Modal'; |
|
|
|
import { previewProps } from '../props'; |
|
|
|
import { FileBasicColumn, PreviewFileItem } from '../types/typing'; |
|
|
|
import { handleFnKey, previewProps } from '../props'; |
|
|
|
import { BaseFileItem, FileBasicColumn, PreviewFileItem } from '../types/typing'; |
|
|
|
import { downloadByUrl } from '@/utils/file/download'; |
|
|
|
import { createPreviewColumns, createPreviewActionColumn } from './data'; |
|
|
|
import { useI18n } from '@/hooks/web/useI18n'; |
|
|
|
import { isArray, isFunction } from '@/utils/is'; |
|
|
|
import { BasicColumn } from '@/components/Table'; |
|
|
|
import { useMessage } from '@/hooks/web/useMessage'; |
|
|
|
|
|
|
|
import { buildUUID } from '@/utils/uuid'; |
|
|
|
const { createMessage } = useMessage(); |
|
|
|
|
|
|
|
const props = defineProps(previewProps); |
|
|
|
@ -35,7 +35,7 @@ |
|
|
|
const [register] = useModalInner(); |
|
|
|
const { t } = useI18n(); |
|
|
|
|
|
|
|
const fileListRef = ref<PreviewFileItem[] | Array<any>>([]); |
|
|
|
const fileListRef = ref<BaseFileItem[] | Array<any>>([]); |
|
|
|
watch( |
|
|
|
() => props.previewColumns, |
|
|
|
() => { |
|
|
|
@ -64,14 +64,15 @@ |
|
|
|
fileListRef.value = value |
|
|
|
.filter((item) => !!item) |
|
|
|
.map((item) => { |
|
|
|
if (typeof item != 'string') { |
|
|
|
console.error('return value should be string'); |
|
|
|
if (typeof item != 'object') { |
|
|
|
console.error('return value should be object'); |
|
|
|
return; |
|
|
|
} |
|
|
|
return { |
|
|
|
url: item, |
|
|
|
type: item.split('.').pop() || '', |
|
|
|
name: item.split('/').pop() || '', |
|
|
|
uid: item?.uid, |
|
|
|
url: item?.url, |
|
|
|
type: item?.url?.split('.').pop() || '', |
|
|
|
name: item?.url?.split('/').pop() || '', |
|
|
|
}; |
|
|
|
}); |
|
|
|
}, |
|
|
|
@ -79,28 +80,26 @@ |
|
|
|
); |
|
|
|
|
|
|
|
// 删除 |
|
|
|
function handleRemove(record: PreviewFileItem | Record<string, any>, urlKey = 'url') { |
|
|
|
const index = fileListRef.value.findIndex((item) => item[urlKey] === record[urlKey]); |
|
|
|
function handleRemove(obj: Record<handleFnKey, any>) { |
|
|
|
let { record = {}, valueKey = 'url', uidKey = 'uid' } = obj; |
|
|
|
const index = fileListRef.value.findIndex((item) => item[uidKey] === record[uidKey]); |
|
|
|
if (index !== -1) { |
|
|
|
const removed = fileListRef.value.splice(index, 1); |
|
|
|
emit('delete', removed[0][urlKey]); |
|
|
|
emit( |
|
|
|
'list-change', |
|
|
|
fileListRef.value.map((item) => item[urlKey]), |
|
|
|
); |
|
|
|
emit('delete', removed[0][uidKey]); |
|
|
|
emit('list-change', fileListRef.value, valueKey); |
|
|
|
} |
|
|
|
} |
|
|
|
// 添加 |
|
|
|
function handleAdd(record: PreviewFileItem | Record<string, any>, urlKey = 'url') { |
|
|
|
function handleAdd(obj: Record<handleFnKey, any>) { |
|
|
|
let { record = {}, valueKey = 'url', uidKey = 'uid' } = obj; |
|
|
|
const { maxNumber } = props; |
|
|
|
if (fileListRef.value.length + fileListRef.value.length > maxNumber) { |
|
|
|
return createMessage.warning(t('component.upload.maxNumber', [maxNumber])); |
|
|
|
} |
|
|
|
record[uidKey] = record[uidKey] ?? buildUUID(); |
|
|
|
record[valueKey] = record[valueKey]; |
|
|
|
fileListRef.value = [...fileListRef.value, record]; |
|
|
|
emit( |
|
|
|
'list-change', |
|
|
|
fileListRef.value.map((item) => item[urlKey]), |
|
|
|
); |
|
|
|
emit('list-change', fileListRef.value, valueKey); |
|
|
|
} |
|
|
|
// 下载 |
|
|
|
function handleDownload(record: PreviewFileItem) { |
|
|
|
|