From 6c17277540f22612e1d310591fff86f482de81e0 Mon Sep 17 00:00:00 2001 From: one Date: Fri, 2 May 2025 11:27:16 +0800 Subject: [PATCH] perf(inputbar): improve long text paste (#5580) --- .../src/pages/home/Inputbar/Inputbar.tsx | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/renderer/src/pages/home/Inputbar/Inputbar.tsx b/src/renderer/src/pages/home/Inputbar/Inputbar.tsx index da17d02bc1..377c63c6d9 100644 --- a/src/renderer/src/pages/home/Inputbar/Inputbar.tsx +++ b/src/renderer/src/pages/home/Inputbar/Inputbar.tsx @@ -570,14 +570,10 @@ const Inputbar: FC = ({ assistant: _assistant, setActiveTopic, topic }) = const onPaste = useCallback( async (event: ClipboardEvent) => { - const clipboardText = event.clipboardData?.getData('text') - if (clipboardText) { - // Prioritize the text when pasting. - // handled by the default event - } else { - for (const file of event.clipboardData?.files || []) { - event.preventDefault() - + // 1. 文件/图片粘贴 + if (event.clipboardData?.files && event.clipboardData.files.length > 0) { + event.preventDefault() + for (const file of event.clipboardData.files) { if (file.path === '') { // 图像生成也支持图像编辑 if (file.type.startsWith('image/') && (isVisionModel(model) || isGenerateImageModel(model))) { @@ -608,23 +604,25 @@ const Inputbar: FC = ({ assistant: _assistant, setActiveTopic, topic }) = } } } + return } - if (pasteLongTextAsFile) { - const item = event.clipboardData?.items[0] - if (item && item.kind === 'string' && item.type === 'text/plain') { - item.getAsString(async (pasteText) => { - if (pasteText.length > pasteLongTextThreshold) { - const tempFilePath = await window.api.file.create('pasted_text.txt') - await window.api.file.write(tempFilePath, pasteText) - const selectedFile = await window.api.file.get(tempFilePath) - selectedFile && setFiles((prevFiles) => [...prevFiles, selectedFile]) - setText(text) - setTimeout(() => resizeTextArea(), 50) - } - }) - } + // 2. 文本粘贴 + const clipboardText = event.clipboardData?.getData('text') + if (pasteLongTextAsFile && clipboardText && clipboardText.length > pasteLongTextThreshold) { + // 长文本直接转文件,阻止默认粘贴 + event.preventDefault() + + const tempFilePath = await window.api.file.create('pasted_text.txt') + await window.api.file.write(tempFilePath, clipboardText) + const selectedFile = await window.api.file.get(tempFilePath) + selectedFile && setFiles((prevFiles) => [...prevFiles, selectedFile]) + setText(text) // 保持输入框内容不变 + setTimeout(() => resizeTextArea(), 50) + return } + + // 短文本走默认粘贴行为 }, [model, pasteLongTextAsFile, pasteLongTextThreshold, resizeTextArea, supportExts, t, text] )