Skip to content

Commit

Permalink
Add handlePaste function to handle pasting images in chat
Browse files Browse the repository at this point in the history
  • Loading branch information
Vergil Wong committed Feb 29, 2024
1 parent 8b821ac commit ddaec04
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,49 @@ function _Chat() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const handlePaste = useCallback(
async (event: React.ClipboardEvent<HTMLTextAreaElement>) => {
const currentModel = chatStore.currentSession().mask.modelConfig.model;
if (!isVisionModel(currentModel)) {
return;
}
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, chatStore],
);

async function uploadImage() {
const images: string[] = [];
images.push(...attachImages);
Expand Down Expand Up @@ -1449,6 +1492,7 @@ function _Chat() {
onKeyDown={onInputKeyDown}
onFocus={scrollToBottom}
onClick={scrollToBottom}
onPaste={handlePaste}
rows={inputRows}
autoFocus={autoFocus}
style={{
Expand Down

0 comments on commit ddaec04

Please sign in to comment.