fix(translate): improve pasting and file processing (#10060)

* fix(translate): prevent default paste behavior only when handling files

Only call event.preventDefault() when handling file pasting to allow text pasting by default. This fixes the issue where text pasting was being blocked unnecessarily.

* fix(translate): append new text to existing content instead of replacing

Ensure OCR and file reading operations append new content to existing text rather than replacing it entirely. This maintains user's previous input when processing additional files.
This commit is contained in:
Phantom 2025-09-09 12:42:26 +08:00 committed by GitHub
parent 56c1851848
commit 2495871c48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -488,7 +488,7 @@ const TranslatePage: FC = () => {
if (shouldOCR) {
try {
const ocrResult = await ocr(file)
setText(ocrResult.text)
setText(text + ocrResult.text)
} finally {
// do nothing when failed. because error should be handled inside
}
@ -520,7 +520,7 @@ const TranslatePage: FC = () => {
} else {
try {
const result = await window.api.fs.readText(file.path)
setText(result)
setText(text + result)
} catch (e) {
logger.error('Failed to read text file.', e as Error)
window.message.error(t('translate.files.error.unknown') + ': ' + formatErrorMessage(e))
@ -532,7 +532,7 @@ const TranslatePage: FC = () => {
}
}
},
[ocr, setText, t]
[ocr, setText, t, text]
)
// 点击上传文件按钮
@ -632,13 +632,12 @@ const TranslatePage: FC = () => {
// 粘贴上传文件
const onPaste = useCallback(
async (event: React.ClipboardEvent<HTMLTextAreaElement>) => {
event.preventDefault()
if (isProcessing) return
setIsProcessing(true)
logger.debug('event', event)
const text = event.clipboardData.getData('text')
if (!isEmpty(text)) {
setText(text)
const clipboardText = event.clipboardData.getData('text')
if (!isEmpty(clipboardText)) {
// depend default. this branch is only for preventing files when clipboard contains text
} else if (event.clipboardData.files && event.clipboardData.files.length > 0) {
event.preventDefault()
const files = event.clipboardData.files
@ -684,7 +683,7 @@ const TranslatePage: FC = () => {
}
setIsProcessing(false)
},
[getSingleFile, isProcessing, processFile, setText, t]
[getSingleFile, isProcessing, processFile, t]
)
return (
<Container