Skip to content

Instantly share code, notes, and snippets.

@BananaAcid
Created April 29, 2023 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BananaAcid/9c6362e8ac889db28c0c7b35b7d230d3 to your computer and use it in GitHub Desktop.
Save BananaAcid/9c6362e8ac889db28c0c7b35b7d230d3 to your computer and use it in GitHub Desktop.
add mock images to dropzone
/**
* Add mock images to a DropZone element
*
* @param dropzone ref to dropzone to add the files to
* @param amount the numeber of mock images to add
* @param callback will be called when a file was added (callback has no info param. Better: `.on("thumbnail", (mockFile, thumbnail) => { ... });` ref: https://github.com/dropzone/dropzone/blob/main/src/dropzone.js#L964:L965 )
* @param url url to get the mocjk images from with `%_RAND_%` to force generating different images, default: `https://source.unsplash.com/200x200/?nature,water&%_RAND_%`
*/
function setMockFiles(
dropzone: Dropzone,
amount: number = 3,
callback?: () => void,
url?: string
) {
// %_RAND_% for random number
let imageUrl =
url ?? "https://source.unsplash.com/200x200/?nature,water&%_RAND_%";
for (let i = 1; i <= amount; i++) {
let mockFile = { name: `Filename_${i}.jpg`, size: Math.random() * 1000000 };
// ref https://github.com/dropzone/dropzone/wiki/FAQ#how-to-show-files-already-stored-on-server
dropzone.displayExistingFile(
mockFile,
imageUrl.replace("%_RAND_%", Math.random().toString()),
/*callback*/ callback,
/*crossOrigin*/ "anonymous",
/*resizeThumbnail*/ true
);
}
}
@BananaAcid
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment