2022-02-24 01:58:58 +00:00
|
|
|
import type { UploadProps as RcUploadProps } from '../vc-upload';
|
2019-01-12 03:33:27 +00:00
|
|
|
import VcUpload from '../vc-upload';
|
|
|
|
import UploadList from './UploadList';
|
2022-02-24 01:58:58 +00:00
|
|
|
import type {
|
|
|
|
UploadType,
|
|
|
|
UploadListType,
|
|
|
|
UploadFile,
|
|
|
|
UploadChangeParam,
|
|
|
|
ShowUploadListInterface,
|
|
|
|
FileType,
|
|
|
|
} from './interface';
|
2021-12-17 07:32:32 +00:00
|
|
|
import { uploadProps } from './interface';
|
2022-02-24 01:58:58 +00:00
|
|
|
import { file2Obj, getFileItem, removeFileItem, updateFileList } from './utils';
|
|
|
|
import { useLocaleReceiver } from '../locale-provider/LocaleReceiver';
|
|
|
|
import defaultLocale from '../locale/default';
|
2022-03-17 07:39:19 +00:00
|
|
|
import type { CSSProperties } from 'vue';
|
2022-02-24 01:58:58 +00:00
|
|
|
import { computed, defineComponent, onMounted, ref, toRef } from 'vue';
|
|
|
|
import { flattenChildren, initDefaultProps } from '../_util/props-util';
|
|
|
|
import useMergedState from '../_util/hooks/useMergedState';
|
|
|
|
import devWarning from '../vc-util/devWarning';
|
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
import type { VueNode } from '../_util/type';
|
|
|
|
import classNames from '../_util/classNames';
|
|
|
|
import { useInjectFormItemContext } from '../form';
|
|
|
|
|
|
|
|
export const LIST_IGNORE = `__LIST_IGNORE_${Date.now()}__`;
|
2021-09-25 08:51:32 +00:00
|
|
|
|
2020-10-22 06:52:37 +00:00
|
|
|
export default defineComponent({
|
2018-04-13 08:19:50 +00:00
|
|
|
name: 'AUpload',
|
2019-02-01 09:23:00 +00:00
|
|
|
inheritAttrs: false,
|
2022-02-24 01:58:58 +00:00
|
|
|
props: initDefaultProps(uploadProps(), {
|
|
|
|
type: 'select' as UploadType,
|
2018-04-13 08:19:50 +00:00
|
|
|
multiple: false,
|
|
|
|
action: '',
|
|
|
|
data: {},
|
|
|
|
accept: '',
|
|
|
|
showUploadList: true,
|
2022-02-24 01:58:58 +00:00
|
|
|
listType: 'text' as UploadListType, // or picture
|
2018-04-13 08:19:50 +00:00
|
|
|
disabled: false,
|
|
|
|
supportServerRender: true,
|
|
|
|
}),
|
2022-02-24 01:58:58 +00:00
|
|
|
setup(props, { slots, attrs, expose }) {
|
2021-09-25 08:51:32 +00:00
|
|
|
const formItemContext = useInjectFormItemContext();
|
2022-02-24 01:58:58 +00:00
|
|
|
const [mergedFileList, setMergedFileList] = useMergedState(props.defaultFileList || [], {
|
|
|
|
value: toRef(props, 'fileList'),
|
|
|
|
postState: list => {
|
|
|
|
const timestamp = Date.now();
|
|
|
|
return (list ?? []).map((file, index) => {
|
|
|
|
if (!file.uid && !Object.isFrozen(file)) {
|
|
|
|
file.uid = `__AUTO__${timestamp}_${index}__`;
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const dragState = ref('drop');
|
|
|
|
|
|
|
|
const upload = ref();
|
|
|
|
onMounted(() => {
|
|
|
|
devWarning(
|
|
|
|
props.fileList !== undefined || attrs.value === undefined,
|
|
|
|
'Upload',
|
|
|
|
'`value` is not a valid prop, do you mean `fileList`?',
|
|
|
|
);
|
|
|
|
|
|
|
|
devWarning(
|
|
|
|
props.transformFile === undefined,
|
|
|
|
'Upload',
|
|
|
|
'`transformFile` is deprecated. Please use `beforeUpload` directly.',
|
|
|
|
);
|
|
|
|
devWarning(
|
|
|
|
props.remove === undefined,
|
|
|
|
'Upload',
|
|
|
|
'`remove` props is deprecated. Please use `remove` event.',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const onInternalChange = (
|
|
|
|
file: UploadFile,
|
|
|
|
changedFileList: UploadFile[],
|
|
|
|
event?: { percent: number },
|
|
|
|
) => {
|
|
|
|
let cloneList = [...changedFileList];
|
|
|
|
|
|
|
|
// Cut to match count
|
|
|
|
if (props.maxCount === 1) {
|
|
|
|
cloneList = cloneList.slice(-1);
|
|
|
|
} else if (props.maxCount) {
|
|
|
|
cloneList = cloneList.slice(0, props.maxCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
setMergedFileList(cloneList);
|
|
|
|
|
|
|
|
const changeInfo: UploadChangeParam<UploadFile> = {
|
|
|
|
file: file as UploadFile,
|
|
|
|
fileList: cloneList,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
changeInfo.event = event;
|
|
|
|
}
|
|
|
|
props['onUpdate:fileList']?.(changeInfo.fileList);
|
|
|
|
props.onChange?.(changeInfo);
|
|
|
|
formItemContext.onFieldChange();
|
2020-06-25 13:23:39 +00:00
|
|
|
};
|
2022-02-24 01:58:58 +00:00
|
|
|
|
|
|
|
const mergedBeforeUpload = async (file: FileType, fileListArgs: FileType[]) => {
|
|
|
|
const { beforeUpload, transformFile } = props;
|
|
|
|
|
|
|
|
let parsedFile: FileType | Blob | string = file;
|
|
|
|
if (beforeUpload) {
|
|
|
|
const result = await beforeUpload(file, fileListArgs);
|
|
|
|
|
|
|
|
if (result === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hack for LIST_IGNORE, we add additional info to remove from the list
|
|
|
|
delete (file as any)[LIST_IGNORE];
|
|
|
|
if ((result as any) === LIST_IGNORE) {
|
|
|
|
Object.defineProperty(file, LIST_IGNORE, {
|
|
|
|
value: true,
|
|
|
|
configurable: true,
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof result === 'object' && result) {
|
|
|
|
parsedFile = result as File;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transformFile) {
|
|
|
|
parsedFile = await transformFile(parsedFile as any);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedFile as File;
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2022-02-24 01:58:58 +00:00
|
|
|
|
|
|
|
const onBatchStart: RcUploadProps['onBatchStart'] = batchFileInfoList => {
|
|
|
|
// Skip file which marked as `LIST_IGNORE`, these file will not add to file list
|
|
|
|
const filteredFileInfoList = batchFileInfoList.filter(
|
|
|
|
info => !(info.file as any)[LIST_IGNORE],
|
|
|
|
);
|
|
|
|
|
|
|
|
// Nothing to do since no file need upload
|
|
|
|
if (!filteredFileInfoList.length) {
|
|
|
|
return;
|
2018-12-05 10:31:58 +00:00
|
|
|
}
|
2022-02-24 01:58:58 +00:00
|
|
|
|
|
|
|
const objectFileList = filteredFileInfoList.map(info => file2Obj(info.file as FileType));
|
|
|
|
|
|
|
|
// Concat new files with prev files
|
|
|
|
let newFileList = [...mergedFileList.value];
|
|
|
|
|
|
|
|
objectFileList.forEach(fileObj => {
|
|
|
|
// Replace file if exist
|
|
|
|
newFileList = updateFileList(fileObj, newFileList);
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2022-02-24 01:58:58 +00:00
|
|
|
objectFileList.forEach((fileObj, index) => {
|
|
|
|
// Repeat trigger `onChange` event for compatible
|
|
|
|
let triggerFileObj: UploadFile = fileObj;
|
|
|
|
|
|
|
|
if (!filteredFileInfoList[index].parsedFile) {
|
|
|
|
// `beforeUpload` return false
|
|
|
|
const { originFileObj } = fileObj;
|
|
|
|
let clone;
|
|
|
|
|
|
|
|
try {
|
|
|
|
clone = new File([originFileObj], originFileObj.name, {
|
|
|
|
type: originFileObj.type,
|
|
|
|
}) as any as UploadFile;
|
|
|
|
} catch (e) {
|
|
|
|
clone = new Blob([originFileObj], {
|
|
|
|
type: originFileObj.type,
|
|
|
|
}) as any as UploadFile;
|
|
|
|
clone.name = originFileObj.name;
|
|
|
|
clone.lastModifiedDate = new Date();
|
|
|
|
clone.lastModified = new Date().getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
clone.uid = fileObj.uid;
|
|
|
|
triggerFileObj = clone;
|
|
|
|
} else {
|
|
|
|
// Inject `uploading` status
|
|
|
|
fileObj.status = 'uploading';
|
|
|
|
}
|
|
|
|
|
|
|
|
onInternalChange(triggerFileObj, newFileList);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onSuccess = (response: any, file: FileType, xhr: any) => {
|
2018-04-13 08:19:50 +00:00
|
|
|
try {
|
|
|
|
if (typeof response === 'string') {
|
2019-01-12 03:33:27 +00:00
|
|
|
response = JSON.parse(response);
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
} catch (e) {
|
|
|
|
/* do nothing */
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2022-02-24 01:58:58 +00:00
|
|
|
|
2018-04-13 08:19:50 +00:00
|
|
|
// removed
|
2022-02-24 01:58:58 +00:00
|
|
|
if (!getFileItem(file, mergedFileList.value)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2022-02-24 01:58:58 +00:00
|
|
|
|
|
|
|
const targetItem = file2Obj(file);
|
2019-01-12 03:33:27 +00:00
|
|
|
targetItem.status = 'done';
|
2022-02-24 01:58:58 +00:00
|
|
|
targetItem.percent = 100;
|
2019-01-12 03:33:27 +00:00
|
|
|
targetItem.response = response;
|
2020-03-07 11:45:13 +00:00
|
|
|
targetItem.xhr = xhr;
|
2022-02-24 01:58:58 +00:00
|
|
|
|
|
|
|
const nextFileList = updateFileList(targetItem, mergedFileList.value);
|
|
|
|
|
|
|
|
onInternalChange(targetItem, nextFileList);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onProgress = (e: { percent: number }, file: FileType) => {
|
2018-04-13 08:19:50 +00:00
|
|
|
// removed
|
2022-02-24 01:58:58 +00:00
|
|
|
if (!getFileItem(file, mergedFileList.value)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2022-02-24 01:58:58 +00:00
|
|
|
|
|
|
|
const targetItem = file2Obj(file);
|
|
|
|
targetItem.status = 'uploading';
|
2019-01-12 03:33:27 +00:00
|
|
|
targetItem.percent = e.percent;
|
2022-02-24 01:58:58 +00:00
|
|
|
|
|
|
|
const nextFileList = updateFileList(targetItem, mergedFileList.value);
|
|
|
|
|
|
|
|
onInternalChange(targetItem, nextFileList, e);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onError = (error: Error, response: any, file: FileType) => {
|
2018-04-13 08:19:50 +00:00
|
|
|
// removed
|
2022-02-24 01:58:58 +00:00
|
|
|
if (!getFileItem(file, mergedFileList.value)) {
|
2019-01-12 03:33:27 +00:00
|
|
|
return;
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2022-02-24 01:58:58 +00:00
|
|
|
|
|
|
|
const targetItem = file2Obj(file);
|
2019-01-12 03:33:27 +00:00
|
|
|
targetItem.error = error;
|
|
|
|
targetItem.response = response;
|
|
|
|
targetItem.status = 'error';
|
2018-04-13 08:19:50 +00:00
|
|
|
|
2022-02-24 01:58:58 +00:00
|
|
|
const nextFileList = updateFileList(targetItem, mergedFileList.value);
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2022-02-24 01:58:58 +00:00
|
|
|
onInternalChange(targetItem, nextFileList);
|
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2022-02-24 01:58:58 +00:00
|
|
|
const handleRemove = (file: UploadFile) => {
|
|
|
|
let currentFile: UploadFile;
|
|
|
|
const mergedRemove = props.onRemove || props.remove;
|
|
|
|
Promise.resolve(typeof mergedRemove === 'function' ? mergedRemove(file) : mergedRemove).then(
|
|
|
|
ret => {
|
|
|
|
// Prevent removing file
|
|
|
|
if (ret === false) {
|
|
|
|
return;
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 01:58:58 +00:00
|
|
|
const removedFileList = removeFileItem(file, mergedFileList.value);
|
|
|
|
|
|
|
|
if (removedFileList) {
|
|
|
|
currentFile = { ...file, status: 'removed' };
|
|
|
|
mergedFileList.value?.forEach(item => {
|
|
|
|
const matchKey = currentFile.uid !== undefined ? 'uid' : 'name';
|
|
|
|
if (item[matchKey] === currentFile[matchKey] && !Object.isFrozen(item)) {
|
|
|
|
item.status = 'removed';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
upload.value?.abort(currentFile);
|
|
|
|
|
|
|
|
onInternalChange(currentFile, removedFileList);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onFileDrop = (e: DragEvent) => {
|
|
|
|
dragState.value = e.type;
|
|
|
|
if (e.type === 'drop') {
|
|
|
|
props.onDrop?.(e);
|
2018-04-13 08:19:50 +00:00
|
|
|
}
|
2022-02-24 01:58:58 +00:00
|
|
|
};
|
|
|
|
expose({
|
|
|
|
onBatchStart,
|
|
|
|
onSuccess,
|
|
|
|
onProgress,
|
|
|
|
onError,
|
|
|
|
fileList: mergedFileList,
|
|
|
|
upload,
|
|
|
|
});
|
|
|
|
|
|
|
|
const { prefixCls, direction } = useConfigInject('upload', props);
|
|
|
|
const [locale] = useLocaleReceiver(
|
|
|
|
'Upload',
|
|
|
|
defaultLocale.Upload,
|
|
|
|
computed(() => props.locale),
|
|
|
|
);
|
2022-03-23 08:53:37 +00:00
|
|
|
const renderUploadList = (button?: () => VueNode, buttonVisible?: boolean) => {
|
2020-03-07 11:45:13 +00:00
|
|
|
const {
|
2022-02-24 01:58:58 +00:00
|
|
|
removeIcon,
|
|
|
|
previewIcon,
|
|
|
|
downloadIcon,
|
2020-06-25 13:23:39 +00:00
|
|
|
previewFile,
|
|
|
|
onPreview,
|
2022-02-24 01:58:58 +00:00
|
|
|
onDownload,
|
|
|
|
disabled,
|
|
|
|
isImageUrl,
|
|
|
|
progress,
|
|
|
|
itemRender,
|
|
|
|
iconRender,
|
|
|
|
showUploadList,
|
|
|
|
} = props;
|
|
|
|
const { showDownloadIcon, showPreviewIcon, showRemoveIcon } =
|
|
|
|
typeof showUploadList === 'boolean' ? ({} as ShowUploadListInterface) : showUploadList;
|
|
|
|
return showUploadList ? (
|
|
|
|
<UploadList
|
|
|
|
listType={props.listType}
|
|
|
|
items={mergedFileList.value}
|
|
|
|
previewFile={previewFile}
|
|
|
|
onPreview={onPreview}
|
|
|
|
onDownload={onDownload}
|
|
|
|
onRemove={handleRemove}
|
|
|
|
showRemoveIcon={!disabled && showRemoveIcon}
|
|
|
|
showPreviewIcon={showPreviewIcon}
|
|
|
|
showDownloadIcon={showDownloadIcon}
|
|
|
|
removeIcon={removeIcon}
|
|
|
|
previewIcon={previewIcon}
|
|
|
|
downloadIcon={downloadIcon}
|
|
|
|
iconRender={iconRender}
|
|
|
|
locale={locale.value}
|
|
|
|
isImageUrl={isImageUrl}
|
|
|
|
progress={progress}
|
|
|
|
itemRender={itemRender}
|
2022-03-17 07:39:19 +00:00
|
|
|
appendActionVisible={buttonVisible}
|
2022-03-23 08:53:37 +00:00
|
|
|
appendAction={button}
|
|
|
|
v-slots={{ ...slots }}
|
2022-02-24 01:58:58 +00:00
|
|
|
/>
|
|
|
|
) : (
|
2022-03-23 08:53:37 +00:00
|
|
|
button?.()
|
2022-02-24 01:58:58 +00:00
|
|
|
);
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2022-02-24 01:58:58 +00:00
|
|
|
return () => {
|
|
|
|
const { listType, disabled, type } = props;
|
|
|
|
const rcUploadProps = {
|
|
|
|
onBatchStart,
|
|
|
|
onError,
|
|
|
|
onProgress,
|
|
|
|
onSuccess,
|
|
|
|
...(props as RcUploadProps),
|
|
|
|
id: props.id ?? formItemContext.id.value,
|
|
|
|
prefixCls: prefixCls.value,
|
|
|
|
beforeUpload: mergedBeforeUpload,
|
|
|
|
onChange: undefined,
|
|
|
|
};
|
|
|
|
delete (rcUploadProps as any).remove;
|
|
|
|
|
|
|
|
// Remove id to avoid open by label when trigger is hidden
|
|
|
|
// !children: https://github.com/ant-design/ant-design/issues/14298
|
|
|
|
// disabled: https://github.com/ant-design/ant-design/issues/16478
|
|
|
|
// https://github.com/ant-design/ant-design/issues/24197
|
|
|
|
if (!slots.default || disabled) {
|
|
|
|
delete rcUploadProps.id;
|
|
|
|
}
|
|
|
|
if (type === 'drag') {
|
|
|
|
const dragCls = classNames(
|
|
|
|
prefixCls.value,
|
|
|
|
{
|
|
|
|
[`${prefixCls.value}-drag`]: true,
|
|
|
|
[`${prefixCls.value}-drag-uploading`]: mergedFileList.value.some(
|
|
|
|
file => file.status === 'uploading',
|
|
|
|
),
|
|
|
|
[`${prefixCls.value}-drag-hover`]: dragState.value === 'dragover',
|
|
|
|
[`${prefixCls.value}-disabled`]: disabled,
|
|
|
|
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
|
|
|
|
},
|
|
|
|
attrs.class,
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
<div
|
|
|
|
class={dragCls}
|
|
|
|
onDrop={onFileDrop}
|
|
|
|
onDragover={onFileDrop}
|
|
|
|
onDragleave={onFileDrop}
|
|
|
|
style={attrs.style}
|
|
|
|
>
|
|
|
|
<VcUpload
|
|
|
|
{...rcUploadProps}
|
|
|
|
ref={upload}
|
|
|
|
class={`${prefixCls.value}-btn`}
|
|
|
|
v-slots={slots}
|
|
|
|
>
|
|
|
|
<div class={`${prefixCls}-drag-container`}>{slots.default?.()}</div>
|
|
|
|
</VcUpload>
|
|
|
|
</div>
|
|
|
|
{renderUploadList()}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
2018-04-13 08:19:50 +00:00
|
|
|
|
2022-02-24 01:58:58 +00:00
|
|
|
const uploadButtonCls = classNames(prefixCls.value, {
|
|
|
|
[`${prefixCls.value}-select`]: true,
|
|
|
|
[`${prefixCls.value}-select-${listType}`]: true,
|
|
|
|
[`${prefixCls.value}-disabled`]: disabled,
|
|
|
|
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2022-02-24 01:58:58 +00:00
|
|
|
const children = flattenChildren(slots.default?.());
|
2022-03-17 07:39:19 +00:00
|
|
|
const renderUploadButton = (uploadButtonStyle?: CSSProperties) => (
|
|
|
|
<div class={uploadButtonCls} style={uploadButtonStyle}>
|
2022-02-24 01:58:58 +00:00
|
|
|
<VcUpload {...rcUploadProps} ref={upload} v-slots={slots} />
|
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2019-03-28 11:51:27 +00:00
|
|
|
|
2022-02-24 01:58:58 +00:00
|
|
|
if (listType === 'picture-card') {
|
|
|
|
return (
|
|
|
|
<span class={classNames(`${prefixCls.value}-picture-card-wrapper`, attrs.class)}>
|
2022-03-23 08:53:37 +00:00
|
|
|
{renderUploadList(renderUploadButton, !!(children && children.length))}
|
2022-02-24 01:58:58 +00:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
2018-04-13 08:19:50 +00:00
|
|
|
return (
|
2022-02-24 01:58:58 +00:00
|
|
|
<span class={attrs.class}>
|
2022-03-17 07:39:19 +00:00
|
|
|
{renderUploadButton(children && children.length ? undefined : { display: 'none' })}
|
2022-02-24 01:58:58 +00:00
|
|
|
{renderUploadList()}
|
2018-04-13 08:19:50 +00:00
|
|
|
</span>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2022-02-24 01:58:58 +00:00
|
|
|
};
|
2018-04-13 08:19:50 +00:00
|
|
|
},
|
2020-10-22 06:52:37 +00:00
|
|
|
});
|