Skip to content

Commit c2c62d7

Browse files
authored
DumpData: Fix implicit download (#17038)
For backwards compatibility. I missed this case in #17015. Fixes specific CreateScreenshotWithRenderTarget usage in Sandbox.
1 parent 5d6f663 commit c2c62d7

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

packages/dev/core/src/Misc/dumpTools.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,32 @@ export async function DumpDataAsync(
198198
ctx.transferFromImageBitmap(imageBitmap);
199199
}
200200

201-
// Download the result
202-
if (toArrayBuffer) {
203-
Tools.ToBlob(
204-
resources.canvas,
205-
(blob) => {
206-
const fileReader = new FileReader();
207-
fileReader.onload = (event: any) => {
208-
const arrayBuffer = event.target!.result as ArrayBuffer;
209-
resolve(arrayBuffer);
210-
};
211-
fileReader.readAsArrayBuffer(blob!);
212-
},
213-
mimeType,
214-
quality
215-
);
216-
} else {
217-
Tools.EncodeScreenshotCanvasData(resources.canvas, resolve, mimeType, fileName, quality);
218-
}
201+
Tools.ToBlob(
202+
resources.canvas,
203+
(blob) => {
204+
if (!blob) {
205+
throw new Error("DumpData: Failed to convert canvas to blob.");
206+
}
207+
208+
if (fileName !== undefined) {
209+
Tools.DownloadBlob(blob, fileName);
210+
}
211+
212+
const fileReader = new FileReader();
213+
fileReader.onload = (event: any) => {
214+
const result = event.target!.result as string | ArrayBuffer;
215+
resolve(result);
216+
};
217+
218+
if (toArrayBuffer) {
219+
fileReader.readAsArrayBuffer(blob);
220+
} else {
221+
fileReader.readAsDataURL(blob);
222+
}
223+
},
224+
mimeType,
225+
quality
226+
);
219227
});
220228
}
221229

@@ -226,7 +234,7 @@ export async function DumpDataAsync(
226234
* @param data the data array
227235
* @param successCallback defines the callback triggered once the data are available
228236
* @param mimeType defines the mime type of the result
229-
* @param fileName defines the filename to download. If present, the result will automatically be downloaded
237+
* @param fileName The name of the file to download. If present, the result will automatically be downloaded. If not defined, and `successCallback` is also not defined, the result will automatically be downloaded with an auto-generated file name.
230238
* @param invertY true to invert the picture in the Y dimension
231239
* @param toArrayBuffer true to convert the data to an ArrayBuffer (encoded as `mimeType`) instead of a base64 string
232240
* @param quality The quality of the image if lossy mimeType is used (e.g. image/jpeg, image/webp). See {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob | HTMLCanvasElement.toBlob()}'s `quality` parameter.
@@ -242,6 +250,11 @@ export function DumpData(
242250
toArrayBuffer = false,
243251
quality?: number
244252
): void {
253+
// For back-compat: if no fileName and no callback, force download the result
254+
if (fileName === undefined && !successCallback) {
255+
fileName = "";
256+
}
257+
245258
// eslint-disable-next-line @typescript-eslint/no-floating-promises
246259
DumpDataAsync(width, height, data, mimeType, fileName, invertY, toArrayBuffer, quality)
247260
// eslint-disable-next-line github/no-then

0 commit comments

Comments
 (0)