mirror of
https://github.com/oobabooga/text-generation-webui.git
synced 2025-06-07 06:06:20 -04:00
Turn long pasted text into an attachment automatically
This commit is contained in:
parent
bf42b2c3a1
commit
7a81beb0c1
1 changed files with 40 additions and 0 deletions
40
js/main.js
40
js/main.js
|
@ -865,6 +865,46 @@ function navigateLastAssistantMessage(direction) {
|
|||
return false;
|
||||
}
|
||||
|
||||
//------------------------------------------------
|
||||
// Paste Handler for Long Text
|
||||
//------------------------------------------------
|
||||
|
||||
const MAX_PLAIN_TEXT_LENGTH = 2500;
|
||||
|
||||
function setupPasteHandler() {
|
||||
const textbox = document.querySelector("#chat-input textarea[data-testid=\"textbox\"]");
|
||||
const fileInput = document.querySelector("#chat-input input[data-testid=\"file-upload\"]");
|
||||
|
||||
if (!textbox || !fileInput) {
|
||||
setTimeout(setupPasteHandler, 500);
|
||||
return;
|
||||
}
|
||||
|
||||
textbox.addEventListener("paste", async (event) => {
|
||||
const text = event.clipboardData?.getData("text");
|
||||
|
||||
if (text && text.length > MAX_PLAIN_TEXT_LENGTH) {
|
||||
event.preventDefault();
|
||||
|
||||
const file = new File([text], "pasted_text.txt", {
|
||||
type: "text/plain",
|
||||
lastModified: Date.now()
|
||||
});
|
||||
|
||||
const dataTransfer = new DataTransfer();
|
||||
dataTransfer.items.add(file);
|
||||
fileInput.files = dataTransfer.files;
|
||||
fileInput.dispatchEvent(new Event("change", { bubbles: true }));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (document.readyState === "loading") {
|
||||
document.addEventListener("DOMContentLoaded", setupPasteHandler);
|
||||
} else {
|
||||
setupPasteHandler();
|
||||
}
|
||||
|
||||
//------------------------------------------------
|
||||
// Tooltips
|
||||
//------------------------------------------------
|
||||
|
|
Loading…
Add table
Reference in a new issue