mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-27 04:36:19 +08:00
c673cde7de
* feat: add onCancel and onEnd option for editable (#29615) * feature: add onCancel and onEnd option for editable * doc: editable * doc: add EN doc * optimize: code Co-authored-by: chenliang <chenliang9@xiaomi.com> * feat: add parent class for different notification types (#29634) close #29417 * refactor: Upload use origin behavior (#29737) * refactor: Fallback of events * test: Fix test case * fix: Start file update logic * fix: remove status update * test: Remove wrapTest * test: Fix test case * chore: bump rc-upload * docs: About desc * feat: tab support moreIcon (#29744) * feat: Tabs support moreIcon * docs: Tabs support moreIcon * style: lint * docs: add english document * Update components/tabs/index.zh-CN.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.en-US.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.zh-CN.md * Update components/tabs/index.en-US.md Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: jueinin <1014397160@qq.com> Co-authored-by: chenliang <chenliang9@xiaomi.com> Co-authored-by: 不吃猫的鱼 <michael2ib1989@gmail.com> Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: Tianyuan Zhang <tianyuan233.zhang@gmail.com> Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com>
116 lines
3.4 KiB
TypeScript
116 lines
3.4 KiB
TypeScript
import { RcFile, UploadFile } from './interface';
|
|
|
|
export function file2Obj(file: RcFile): UploadFile {
|
|
return {
|
|
...file,
|
|
lastModified: file.lastModified,
|
|
lastModifiedDate: file.lastModifiedDate,
|
|
name: file.name,
|
|
size: file.size,
|
|
type: file.type,
|
|
uid: file.uid,
|
|
percent: 0,
|
|
originFileObj: file,
|
|
};
|
|
}
|
|
|
|
/** Upload fileList. Replace file if exist or just push into it. */
|
|
export function updateFileList(file: UploadFile<any>, fileList: UploadFile<any>[]) {
|
|
const nextFileList = [...fileList];
|
|
const fileIndex = nextFileList.findIndex(({ uid }: UploadFile) => uid === file.uid);
|
|
if (fileIndex === -1) {
|
|
nextFileList.push(file);
|
|
} else {
|
|
nextFileList[fileIndex] = file;
|
|
}
|
|
return nextFileList;
|
|
}
|
|
|
|
export function getFileItem(file: RcFile, fileList: UploadFile[]) {
|
|
const matchKey = file.uid !== undefined ? 'uid' : 'name';
|
|
return fileList.filter(item => item[matchKey] === file[matchKey])[0];
|
|
}
|
|
|
|
export function removeFileItem(file: UploadFile, fileList: UploadFile[]) {
|
|
const matchKey = file.uid !== undefined ? 'uid' : 'name';
|
|
const removed = fileList.filter(item => item[matchKey] !== file[matchKey]);
|
|
if (removed.length === fileList.length) {
|
|
return null;
|
|
}
|
|
return removed;
|
|
}
|
|
|
|
// ==================== Default Image Preview ====================
|
|
const extname = (url: string = '') => {
|
|
const temp = url.split('/');
|
|
const filename = temp[temp.length - 1];
|
|
const filenameWithoutSuffix = filename.split(/#|\?/)[0];
|
|
return (/\.[^./\\]*$/.exec(filenameWithoutSuffix) || [''])[0];
|
|
};
|
|
|
|
const isImageFileType = (type: string): boolean => type.indexOf('image/') === 0;
|
|
|
|
export const isImageUrl = (file: UploadFile): boolean => {
|
|
if (file.type && !file.thumbUrl) {
|
|
return isImageFileType(file.type);
|
|
}
|
|
const url: string = (file.thumbUrl || file.url) as string;
|
|
const extension = extname(url);
|
|
if (
|
|
/^data:image\//.test(url) ||
|
|
/(webp|svg|png|gif|jpg|jpeg|jfif|bmp|dpg|ico)$/i.test(extension)
|
|
) {
|
|
return true;
|
|
}
|
|
if (/^data:/.test(url)) {
|
|
// other file types of base64
|
|
return false;
|
|
}
|
|
if (extension) {
|
|
// other file types which have extension
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
const MEASURE_SIZE = 200;
|
|
export function previewImage(file: File | Blob): Promise<string> {
|
|
return new Promise(resolve => {
|
|
if (!file.type || !isImageFileType(file.type)) {
|
|
resolve('');
|
|
return;
|
|
}
|
|
|
|
const canvas = document.createElement('canvas');
|
|
canvas.width = MEASURE_SIZE;
|
|
canvas.height = MEASURE_SIZE;
|
|
canvas.style.cssText = `position: fixed; left: 0; top: 0; width: ${MEASURE_SIZE}px; height: ${MEASURE_SIZE}px; z-index: 9999; display: none;`;
|
|
document.body.appendChild(canvas);
|
|
const ctx = canvas.getContext('2d');
|
|
const img = new Image();
|
|
img.onload = () => {
|
|
const { width, height } = img;
|
|
|
|
let drawWidth = MEASURE_SIZE;
|
|
let drawHeight = MEASURE_SIZE;
|
|
let offsetX = 0;
|
|
let offsetY = 0;
|
|
|
|
if (width > height) {
|
|
drawHeight = height * (MEASURE_SIZE / width);
|
|
offsetY = -(drawHeight - drawWidth) / 2;
|
|
} else {
|
|
drawWidth = width * (MEASURE_SIZE / height);
|
|
offsetX = -(drawWidth - drawHeight) / 2;
|
|
}
|
|
|
|
ctx!.drawImage(img, offsetX, offsetY, drawWidth, drawHeight);
|
|
const dataURL = canvas.toDataURL();
|
|
document.body.removeChild(canvas);
|
|
|
|
resolve(dataURL);
|
|
};
|
|
img.src = window.URL.createObjectURL(file);
|
|
});
|
|
}
|