[new feature] Uploader: support isImage flag (#4072)

This commit is contained in:
neverland 2019-08-08 20:28:55 +08:00 committed by GitHub
parent b004ae11c0
commit c012b29a20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 1 deletions

View File

@ -42,7 +42,10 @@ export default {
data() {
return {
fileList: [
{ url: 'https://img.yzcdn.cn/vant/cat.jpeg' }
{ url: 'https://img.yzcdn.cn/vant/cat.jpeg' },
// Uploader 根据文件后缀来判断是否为图片文件
// 如果图片 URL 中不包含类型信息,可以添加 isImage 标记来声明
{ url: 'https://cloud-image', isImage: true }
]
}
}

View File

@ -10,5 +10,6 @@ test('isImageFile', () => {
expect(isImageFile({ file: { type: 'application/pdf' } })).toBeFalsy();
expect(isImageFile({ content: 'data:image/xxx' })).toBeTruthy();
expect(isImageFile({ content: 'data:application/xxx' })).toBeFalsy();
expect(isImageFile({ isImage: true })).toBeTruthy();
expect(isImageFile({})).toBeFalsy();
});

View File

@ -30,6 +30,7 @@ export type FileListItem = {
url?: string;
file?: File;
content?: string; // dataUrl
isImage?: boolean;
};
const IMAGE_EXT = ['jpeg', 'jpg', 'gif', 'png', 'svg'];
@ -39,6 +40,12 @@ export function isImageUrl(url: string): boolean {
}
export function isImageFile(item: FileListItem): boolean {
// some special urls cannot be recognized
// user can add `isImage` flag to mark it as an image url
if (item.isImage) {
return true;
}
if (item.file && item.file.type) {
return item.file.type.indexOf('image') === 0;
}