From 82396c42d6c7e6dfef789b049f3e1227418b4554 Mon Sep 17 00:00:00 2001 From: xunbu Date: Wed, 28 May 2025 19:31:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF=E6=9B=B4=E5=A4=9A=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 7 +-- docutranslate/app.py | 24 +++++----- docutranslate/static/index.html | 57 ++++++++++-------------- docutranslate/utils/markdown_splitter.py | 7 +-- 4 files changed, 44 insertions(+), 51 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index eb2bad9..12c8e9b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,8 +6,9 @@ - + + @@ -614,7 +615,7 @@ - + diff --git a/docutranslate/app.py b/docutranslate/app.py index a099724..6d761b6 100644 --- a/docutranslate/app.py +++ b/docutranslate/app.py @@ -223,17 +223,19 @@ async def handle_translate( content={"task_started": False, "message": "另一个翻译任务正在进行中,请稍后再试。"} ) - if not file or not file.filename: - return JSONResponse( - status_code=400, - content={"task_started": False, "message": "没有选择文件或文件无效。"} - ) - - if convert_engin == "mineru" and (not mineru_token or not mineru_token.strip()): - return JSONResponse( - status_code=400, - content={"task_started": False, "message": "使用 Mineru 引擎时必须提供有效的 Mineru Token。"} - ) + #可选的格式认证,这部分交给前端来写了 + # if not file or not file.filename: + # return JSONResponse( + # status_code=400, + # content={"task_started": False, "message": "没有选择文件或文件无效。"} + # ) + # if not file.filename.split(".")[-1] in ["md","txt"]: + # #需要填写 Mineru 引擎 + # if convert_engin == "mineru" and (not mineru_token or not mineru_token.strip()) : + # return JSONResponse( + # status_code=400, + # content={"task_started": False, "message": "使用 Mineru 引擎时必须提供有效的 Mineru Token。"} + # ) current_state["is_processing"] = True original_filename_for_init = file.filename or "uploaded_file" diff --git a/docutranslate/static/index.html b/docutranslate/static/index.html index 3c6cdf8..8edfb00 100644 --- a/docutranslate/static/index.html +++ b/docutranslate/static/index.html @@ -411,7 +411,7 @@
+ name="custom_prompt_translate" placeholder="翻译提示">
@@ -990,9 +990,9 @@ } } - form.addEventListener('submit', async function (event) { + submitButton.addEventListener('click', async function (event) { event.preventDefault(); - + console.log(fileInput) if (isTranslating) { await cancelTranslation(); return; @@ -1000,52 +1000,41 @@ [fileDropArea, mineruTokenInput, apikeyInput, modelInput, baseUrlInput].forEach(el => el.classList.remove('input-error')); fileNameDisplay.classList.remove('input-error-text'); - let firstErrorElement = null; - let currentStatusMsg = ''; - if (fileInput.files.length === 0) { - currentStatusMsg += '请选择一个文件进行翻译。'; + if (fileInput.files.length !== 1) { + statusMsg.textContent = '请选择一个文件进行翻译。'; + statusMsg.className = 'error-message'; fileNameDisplay.textContent = '请选择文件!'; fileNameDisplay.classList.add('input-error-text'); fileDropArea.classList.add('input-error'); fileDropPrompt.classList.remove('hidden'); - if (!firstErrorElement) firstErrorElement = fileDropArea; + return + } + console.log(convertEnginSelect.value === 'mineru' && (!mineruTokenInput.value.trim()) && (!["md", "txt"].includes(fileInput.files[0].name.split('.').pop()))) + if (convertEnginSelect.value === 'mineru' && !mineruTokenInput.value.trim() && (!["md", "txt"].includes(fileInput.files[0].name.split('.').pop()))) { + statusMsg.textContent = '使用 Mineru 引擎时,必须填写 Mineru Token。'; + statusMsg.className = 'error-message'; + mineruTokenInput.classList.add('input-error'); + return } - if (convertEnginSelect.value === 'mineru' && !mineruTokenInput.value.trim()) { - currentStatusMsg += (currentStatusMsg ? ' ' : '') + '使用 Mineru 引擎时,必须填写 Mineru Token。'; - mineruTokenInput.classList.add('input-error'); - if (!firstErrorElement) firstErrorElement = mineruTokenInput; - } if (!apikeyInput.value.trim()) { - currentStatusMsg += (currentStatusMsg ? ' ' : '') + 'API 密钥不能为空。'; + statusMsg.textContent = 'API 密钥不能为空。'; + statusMsg.className = 'error-message'; apikeyInput.classList.add('input-error'); - if (!firstErrorElement) firstErrorElement = apikeyInput; + return } if (!modelInput.value.trim()) { - currentStatusMsg += (currentStatusMsg ? ' ' : '') + '模型 ID 不能为空。'; + statusMsg.textContent = '模型 ID 不能为空。'; + statusMsg.className = 'error-message'; modelInput.classList.add('input-error'); - if (!firstErrorElement) firstErrorElement = modelInput; + return } if (platformSelect.value === 'custom' && !baseUrlInput.value.trim()) { - currentStatusMsg += (currentStatusMsg ? ' ' : '') + '自定义接口时,API 地址不能为空。'; - baseUrlInput.classList.add('input-error'); - if (!firstErrorElement) firstErrorElement = baseUrlInput; - } - - if (firstErrorElement) { - statusMsg.textContent = currentStatusMsg; + statusMsg.textContent = '自定义接口时,API 地址不能为空。'; statusMsg.className = 'error-message'; - firstErrorElement.focus(); - setTimeout(() => { - [fileDropArea, mineruTokenInput, apikeyInput, modelInput, baseUrlInput].forEach(el => el.classList.remove('input-error')); - fileNameDisplay.classList.remove('input-error-text'); - if (fileNameDisplay.textContent === '请选择文件!' && fileInput.files.length === 0) { - fileNameDisplay.textContent = '未选择文件'; - fileDropPrompt.classList.remove('hidden'); - } - }, 3000); - return; + baseUrlInput.classList.add('input-error'); + return } stopPolling(); diff --git a/docutranslate/utils/markdown_splitter.py b/docutranslate/utils/markdown_splitter.py index 44f27b6..ece4178 100644 --- a/docutranslate/utils/markdown_splitter.py +++ b/docutranslate/utils/markdown_splitter.py @@ -239,9 +239,10 @@ def split_markdown_text(markdown_text, max_block_size=5000): def join_markdown_texts(markdown_texts: list[str]) -> str: - result = "" - pre = "" - for text in markdown_texts: + if len(markdown_texts) == 0: return "" + result = markdown_texts[0] + pre = markdown_texts[0] + for text in markdown_texts[1:]: # 只有表格会收到多余空行的影响 if text.lstrip().startswith("|") and pre.rstrip().endswith("|"): result = result + "\n" + text