@@ -97,6 +97,7 @@ export function ChatInput({ chatId }: { chatId?: number }) {
|
|||||||
handleDragLeave,
|
handleDragLeave,
|
||||||
handleDrop,
|
handleDrop,
|
||||||
clearAttachments,
|
clearAttachments,
|
||||||
|
handlePaste,
|
||||||
} = useAttachments();
|
} = useAttachments();
|
||||||
|
|
||||||
// Use the hook to fetch the proposal
|
// Use the hook to fetch the proposal
|
||||||
@@ -309,6 +310,7 @@ export function ChatInput({ chatId }: { chatId?: number }) {
|
|||||||
value={inputValue}
|
value={inputValue}
|
||||||
onChange={(e) => setInputValue(e.target.value)}
|
onChange={(e) => setInputValue(e.target.value)}
|
||||||
onKeyPress={handleKeyPress}
|
onKeyPress={handleKeyPress}
|
||||||
|
onPaste={handlePaste}
|
||||||
placeholder="Ask Dyad to build..."
|
placeholder="Ask Dyad to build..."
|
||||||
className="flex-1 p-2 focus:outline-none overflow-y-auto min-h-[40px] max-h-[200px]"
|
className="flex-1 p-2 focus:outline-none overflow-y-auto min-h-[40px] max-h-[200px]"
|
||||||
style={{ resize: "none" }}
|
style={{ resize: "none" }}
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ export function HomeChatInput({
|
|||||||
handleDragLeave,
|
handleDragLeave,
|
||||||
handleDrop,
|
handleDrop,
|
||||||
clearAttachments,
|
clearAttachments,
|
||||||
|
handlePaste,
|
||||||
} = useAttachments();
|
} = useAttachments();
|
||||||
|
|
||||||
const adjustHeight = () => {
|
const adjustHeight = () => {
|
||||||
@@ -103,6 +104,7 @@ export function HomeChatInput({
|
|||||||
value={inputValue}
|
value={inputValue}
|
||||||
onChange={(e) => setInputValue(e.target.value)}
|
onChange={(e) => setInputValue(e.target.value)}
|
||||||
onKeyPress={handleKeyPress}
|
onKeyPress={handleKeyPress}
|
||||||
|
onPaste={handlePaste}
|
||||||
placeholder="Ask Dyad to build..."
|
placeholder="Ask Dyad to build..."
|
||||||
className="flex-1 p-2 focus:outline-none overflow-y-auto min-h-[40px] max-h-[200px]"
|
className="flex-1 p-2 focus:outline-none overflow-y-auto min-h-[40px] max-h-[200px]"
|
||||||
style={{ resize: "none" }}
|
style={{ resize: "none" }}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useState, useRef } from "react";
|
import React, { useState, useRef } from "react";
|
||||||
|
|
||||||
export function useAttachments() {
|
export function useAttachments() {
|
||||||
const [attachments, setAttachments] = useState<File[]>([]);
|
const [attachments, setAttachments] = useState<File[]>([]);
|
||||||
@@ -45,6 +45,50 @@ export function useAttachments() {
|
|||||||
setAttachments([]);
|
setAttachments([]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const addAttachments = (files: File[]) => {
|
||||||
|
setAttachments((attachments) => [...attachments, ...files]);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlePaste = async (e: React.ClipboardEvent) => {
|
||||||
|
const clipboardData = e.clipboardData;
|
||||||
|
if (!clipboardData) return;
|
||||||
|
|
||||||
|
const items = Array.from(clipboardData.items);
|
||||||
|
const imageItems = items.filter((item) => item.type.startsWith("image/"));
|
||||||
|
|
||||||
|
if (imageItems.length > 0) {
|
||||||
|
e.preventDefault(); // Prevent default paste behavior for images
|
||||||
|
|
||||||
|
const imageFiles: File[] = [];
|
||||||
|
// Generate base timestamp once to avoid collisions
|
||||||
|
const baseTimestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
||||||
|
|
||||||
|
for (let i = 0; i < imageItems.length; i++) {
|
||||||
|
const item = imageItems[i];
|
||||||
|
const file = item.getAsFile();
|
||||||
|
if (file) {
|
||||||
|
// Create a more descriptive filename with timestamp and counter
|
||||||
|
const extension = file.type.split("/")[1] || "png";
|
||||||
|
const filename =
|
||||||
|
imageItems.length === 1
|
||||||
|
? `pasted-image-${baseTimestamp}.${extension}`
|
||||||
|
: `pasted-image-${baseTimestamp}-${i + 1}.${extension}`;
|
||||||
|
|
||||||
|
const newFile = new File([file], filename, {
|
||||||
|
type: file.type,
|
||||||
|
});
|
||||||
|
imageFiles.push(newFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (imageFiles.length > 0) {
|
||||||
|
addAttachments(imageFiles);
|
||||||
|
// Show a brief toast or indication that image was pasted
|
||||||
|
console.log(`Pasted ${imageFiles.length} image(s) from clipboard`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
attachments,
|
attachments,
|
||||||
fileInputRef,
|
fileInputRef,
|
||||||
@@ -56,5 +100,6 @@ export function useAttachments() {
|
|||||||
handleDragLeave,
|
handleDragLeave,
|
||||||
handleDrop,
|
handleDrop,
|
||||||
clearAttachments,
|
clearAttachments,
|
||||||
|
handlePaste,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user