Skip to content

Commit e315024

Browse files
authored
fix(Upload): avoid leaving canvas after GIF preview (#59137)
1 parent d58fe29 commit e315024

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

‎components/upload/__tests__/uploadlist.test.tsx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,17 @@ describe('Upload List', () => {
11581158
unmount();
11591159
});
11601160

1161+
it('should not leave a canvas after generating a GIF preview', async () => {
1162+
const mockFile = new File([''], 'foo.gif', {
1163+
type: 'image/gif',
1164+
});
1165+
const canvasCount = document.body.querySelectorAll('canvas').length;
1166+
1167+
await previewImage(mockFile);
1168+
1169+
expect(document.body.querySelectorAll('canvas')).toHaveLength(canvasCount);
1170+
});
1171+
11611172
it("upload non image file shouldn't be converted to the base64", async () => {
11621173
const mockFile = new File([''], 'foo.7z', {
11631174
type: 'application/x-7z-compressed',

‎components/upload/utils.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ export function previewImage(file: File | Blob): Promise<string> {
9696
resolve('');
9797
return;
9898
}
99+
100+
if (file.type.startsWith('image/gif')) {
101+
const reader = new FileReader();
102+
reader.onload = () => {
103+
if (reader.result) {
104+
resolve(reader.result as string);
105+
}
106+
};
107+
reader.readAsDataURL(file);
108+
return;
109+
}
110+
99111
const canvas = document.createElement('canvas');
100112
canvas.width = MEASURE_SIZE;
101113
canvas.height = MEASURE_SIZE;
@@ -134,14 +146,6 @@ export function previewImage(file: File | Blob): Promise<string> {
134146
}
135147
};
136148
reader.readAsDataURL(file);
137-
} else if (file.type.startsWith('image/gif')) {
138-
const reader = new FileReader();
139-
reader.onload = () => {
140-
if (reader.result) {
141-
resolve(reader.result as string);
142-
}
143-
};
144-
reader.readAsDataURL(file);
145149
} else {
146150
img.src = window.URL.createObjectURL(file);
147151
}

0 commit comments

Comments
 (0)