images_upload_handler: (
blobInfo: { blob: () => Blob; filename: () => string | undefined },
success: (arg0: any) => void,
failure: (arg0: string) => void
) => {
const xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.open("POST", "http://localhost");
xhr.onload = () => {
if (xhr.status < 200 || xhr.status >= 300) {
failure("HTTP Error: " + xhr.status);
return;
}
const json = JSON.parse(xhr.responseText);
success(json.location); // URL of the uploaded image
};
const formData = new FormData();
formData.append("file", blobInfo.blob(), blobInfo.filename());
xhr.send(formData);
},
file_picker_callback: (
callback: (arg0: string) => void,
value: any,
meta: { filetype: string }
) => {
if (meta.filetype === "image") {
// Open a file picker dialog
const input = document.createElement("input");
input.setAttribute("type", "file");
input.setAttribute("accept", "image/*");
input.onchange = () => {
// Check if input.files is not null and has at least one file
if (input.files && input.files.length > 0) {
const file = input.files[0];
const reader = new FileReader();
reader.onload = (e) => {
// Use type assertion to ensure e.target is defined
const target = e.target as FileReader;
if (target.result) {
// Call the callback with the image URL
callback(target.result as string);
} else {
console.error("Failed to read file.");
}
};
reader.readAsDataURL(file);
} else {
// Handle the case where no file was selected
console.warn("No file selected or file input is null.");
}
};
input.click();
}
},