|
|
|
|
|
|
|
|
|
|
|
|
|
function setPasteUploader() { |
|
input = user_input_tb.querySelector("textarea") |
|
let paste_files = []; |
|
if (input) { |
|
input.addEventListener("paste", async function (e) { |
|
const clipboardData = e.clipboardData || window.clipboardData; |
|
const items = clipboardData.items; |
|
if (items) { |
|
for (i = 0; i < items.length; i++) { |
|
if (items[i].kind === "file") { |
|
const file = items[i].getAsFile(); |
|
|
|
paste_files.push(file); |
|
e.preventDefault(); |
|
} |
|
} |
|
if (paste_files.length > 0) { |
|
|
|
await upload_files(paste_files); |
|
paste_files = []; |
|
} |
|
} |
|
}); |
|
} |
|
} |
|
|
|
var hintArea; |
|
function setDragUploader() { |
|
input = chatbotArea; |
|
if (input) { |
|
const dragEvents = ["dragover", "dragenter"]; |
|
const leaveEvents = ["dragleave", "dragend", "drop"]; |
|
|
|
const onDrag = function (e) { |
|
e.preventDefault(); |
|
e.stopPropagation(); |
|
if (!chatbotArea.classList.contains("with-file")) { |
|
chatbotArea.classList.add("dragging"); |
|
draggingHint(); |
|
} else { |
|
statusDisplayMessage(clearFileHistoryMsg_i18n, 2000); |
|
} |
|
}; |
|
|
|
const onLeave = function (e) { |
|
e.preventDefault(); |
|
e.stopPropagation(); |
|
chatbotArea.classList.remove("dragging"); |
|
if (hintArea) { |
|
hintArea.remove(); |
|
} |
|
}; |
|
|
|
dragEvents.forEach(event => { |
|
input.addEventListener(event, onDrag); |
|
}); |
|
|
|
leaveEvents.forEach(event => { |
|
input.addEventListener(event, onLeave); |
|
}); |
|
|
|
input.addEventListener("drop", async function (e) { |
|
const files = e.dataTransfer.files; |
|
await upload_files(files); |
|
}); |
|
} |
|
} |
|
|
|
async function upload_files(files) { |
|
const uploadInputElement = gradioApp().querySelector("#upload-index-file > .center.flex input[type=file]"); |
|
let totalSizeMb = 0 |
|
if (files && files.length > 0) { |
|
|
|
if (uploadInputElement) { |
|
for (let i = 0; i < files.length; i++) { |
|
|
|
totalSizeMb += files[i].size / 1024 / 1024; |
|
} |
|
|
|
if (totalSizeMb > 20) { |
|
|
|
|
|
} |
|
|
|
|
|
let event = new Event("change"); |
|
Object.defineProperty(event, "target", {value: uploadInputElement, enumerable: true}); |
|
Object.defineProperty(event, "currentTarget", {value: uploadInputElement, enumerable: true}); |
|
Object.defineProperty(uploadInputElement, "files", {value: files, enumerable: true}); |
|
uploadInputElement.dispatchEvent(event); |
|
|
|
} else { |
|
statusDisplayMessage(clearFileHistoryMsg_i18n, 3000); |
|
return; |
|
} |
|
} |
|
} |
|
|
|
function draggingHint() { |
|
hintArea = chatbotArea.querySelector(".dragging-hint"); |
|
if (hintArea) { |
|
return; |
|
} |
|
hintArea = document.createElement("div"); |
|
hintArea.classList.add("dragging-hint"); |
|
hintArea.innerHTML = `<div class="dragging-hint-text"><p>${dropUploadMsg_i18n}</p></div>`; |
|
chatbotArea.appendChild(hintArea); |
|
} |
|
|