add_image_pasting

This commit is contained in:
Snow Kawashiro 2024-02-28 20:05:13 +08:00 committed by GitHub
parent 8b821ac0c9
commit bd19e97cf8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 40 additions and 0 deletions

View File

@ -1101,6 +1101,45 @@ function _Chat() {
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []); }, []);
const handlePaste = useCallback(
async (event: React.ClipboardEvent<HTMLTextAreaElement>) => {
const items = (event.clipboardData || window.clipboardData).items;
for (const item of items) {
if (item.kind === "file" && item.type.startsWith("image/")) {
event.preventDefault();
const file = item.getAsFile();
if (file) {
const images: string[] = [];
images.push(...attachImages);
images.push(
...(await new Promise<string[]>((res, rej) => {
setUploading(true);
const imagesData: string[] = [];
compressImage(file, 256 * 1024)
.then((dataUrl) => {
imagesData.push(dataUrl);
setUploading(false);
res(imagesData);
})
.catch((e) => {
setUploading(false);
rej(e);
});
})),
);
const imagesLength = images.length;
if (imagesLength > 3) {
images.splice(3, imagesLength - 3);
}
setAttachImages(images);
}
}
}
},
[attachImages],
);
async function uploadImage() { async function uploadImage() {
const images: string[] = []; const images: string[] = [];
images.push(...attachImages); images.push(...attachImages);
@ -1449,6 +1488,7 @@ function _Chat() {
onKeyDown={onInputKeyDown} onKeyDown={onInputKeyDown}
onFocus={scrollToBottom} onFocus={scrollToBottom}
onClick={scrollToBottom} onClick={scrollToBottom}
onPaste={handlePaste}
rows={inputRows} rows={inputRows}
autoFocus={autoFocus} autoFocus={autoFocus}
style={{ style={{