fix: 用户上传的图片也添加到画布中

发送消息时将用户上传的图片立即添加到画布

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Your Name 2026-02-01 12:38:05 +08:00
parent e84b002302
commit ccf610bcf8

View File

@ -268,13 +268,33 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
const parts: any[] = []; const parts: any[] = [];
// 添加文件部分(作为 file 类型的 part) // 添加文件部分(作为 file 类型的 part)
attachments.forEach(a => { // 同时将用户上传的图片添加到画布
attachments.forEach((a, index) => {
parts.push({ parts.push({
type: "file" as const, type: "file" as const,
mediaType: a.contentType, mediaType: a.contentType,
name: a.name, name: a.name,
url: a.url, url: a.url,
}); });
// 将用户上传的图片添加到画布
if (a.contentType?.startsWith('image/')) {
const existingNodes = nodes.length;
const x = 100 + ((existingNodes + index) % 4) * 320;
const y = 100 + Math.floor((existingNodes + index) / 4) * 320;
addNode({
type: 'image',
position: { x, y },
size: { width: 300, height: 300 },
data: {
src: a.url,
status: 'completed',
prompt: '用户上传',
},
connections: [],
});
}
}); });
// 添加文本部分 // 添加文本部分
@ -289,9 +309,13 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
parts: parts, parts: parts,
}); });
if (attachments.length > 0) {
toast.success(`已将 ${attachments.length} 张图片添加到画布`);
}
setInput(""); setInput("");
setAttachments([]); setAttachments([]);
}, [input, attachments, selectedModelId, sendMessage]); }, [input, attachments, selectedModelId, sendMessage, addNode, nodes.length]);
// 处理文件选择 // 处理文件选择
const handleFileSelect = async (e: React.ChangeEvent<HTMLInputElement>) => { const handleFileSelect = async (e: React.ChangeEvent<HTMLInputElement>) => {