From 90e523b17ea4d88b7c86f6a1ff2eccb9fb2100d3 Mon Sep 17 00:00:00 2001 From: Vibe Studio Date: Fri, 16 Jan 2026 09:17:38 +0000 Subject: [PATCH] Update from Vibe Studio --- src/api/spellCheck.ts | 22 +- src/pages/spell-check/index.tsx | 348 ++++++++++---------------------- 2 files changed, 114 insertions(+), 256 deletions(-) diff --git a/src/api/spellCheck.ts b/src/api/spellCheck.ts index 89bef27..fd98e65 100644 --- a/src/api/spellCheck.ts +++ b/src/api/spellCheck.ts @@ -1,29 +1,15 @@ -export interface SpellCheckRequest { - user_input_text: string - auto_correct: boolean - custom_vocab: string[] -} - export interface DifyRequest { inputs: { - user_input_text: string - auto_correct: boolean - custom_vocab: string[] + user_input: string } query: string response_mode: string } -export function checkAndCorrectSpelling( - user_input_text: string, - auto_correct: boolean, - custom_vocab: string[] -) { +export function checkAndCorrectSpelling(user_input: string) { const requestBody: DifyRequest = { inputs: { - user_input_text, - auto_correct, - custom_vocab + user_input }, query: '1', response_mode: 'streaming' @@ -33,7 +19,7 @@ export function checkAndCorrectSpelling( method: 'POST', headers: { 'Content-Type': 'application/json', - 'Authorization': 'Bearer app-Dmsx84IAGk7rVCko5MWptmK3' + 'Authorization': 'Bearer app-8LtwQRbmrDRpiGFZqVg4SK3q' }, body: JSON.stringify(requestBody) }) diff --git a/src/pages/spell-check/index.tsx b/src/pages/spell-check/index.tsx index 0a8264d..81b184d 100644 --- a/src/pages/spell-check/index.tsx +++ b/src/pages/spell-check/index.tsx @@ -1,75 +1,36 @@ -import { useState } from 'react' -import { Button, Card, Form, Input, message, Space, Switch } from 'antd' +import { useState, useEffect, useRef } from 'react' +import { Button, Card, Form, Input, message, Typography } from 'antd' import { checkAndCorrectSpelling } from '@/api/spellCheck' -interface CorrectionResult { - original: string - corrected: string - position: number - suggestions?: string[] -} +const { Title } = Typography const SpellCheck = () => { const [form] = Form.useForm() const [isGenerating, setIsGenerating] = useState(false) - const [correctionResults, setCorrectionResults] = useState([]) - const [correctedText, setCorrectedText] = useState('') - const maxChars = 5000 + const [generatedResult, setGeneratedResult] = useState('') + const textareaRef = useRef(null) - const validateCustomVocab = (_: any, value: string) => { - if (!value) { - return Promise.resolve() + // 页面加载时自动聚焦 + useEffect(() => { + if (textareaRef.current) { + textareaRef.current.focus() } - try { - const parsed = JSON.parse(value) - if (!Array.isArray(parsed)) { - return Promise.reject(new Error('必须是JSON数组格式')) - } - if (!parsed.every(item => typeof item === 'string')) { - return Promise.reject(new Error('数组元素必须为字符串')) - } - return Promise.resolve() - } catch (error) { - return Promise.reject(new Error('JSON格式无效')) - } - } + }, []) const handleGenerate = async () => { try { const formValues = await form.validateFields() // 验证用户输入文本 - if (!formValues.user_input_text?.trim()) { + if (!formValues.user_input?.trim()) { message.warning('请输入需要检测错别字的文本') return } - // 验证文本长度 - if (formValues.user_input_text.length > maxChars) { - message.warning(`输入文本不能超过${maxChars}个字符`) - return - } - setIsGenerating(true) - setCorrectionResults([]) - setCorrectedText('') + setGeneratedResult('') - let customVocabArray: string[] = [] - if (formValues.custom_vocab) { - try { - customVocabArray = JSON.parse(formValues.custom_vocab) - } catch (error) { - message.error('自定义词汇库格式错误') - setIsGenerating(false) - return - } - } - - const response = await checkAndCorrectSpelling( - formValues.user_input_text, - formValues.auto_correct || false, - customVocabArray - ) + const response = await checkAndCorrectSpelling(formValues.user_input) if (!response.ok) { const errorText = await response.text() @@ -84,7 +45,6 @@ const SpellCheck = () => { const decoder = new TextDecoder('utf-8') let buffer = '' let fullContent = '' - let results: CorrectionResult[] = [] try { while (true) { @@ -112,22 +72,7 @@ const SpellCheck = () => { if (parsed.event === 'message' && parsed.answer) { fullContent += parsed.answer - // 尝试解析JSON结果 - try { - results = JSON.parse(fullContent) - setCorrectionResults(results) - if (formValues.auto_correct) { - let corrected = formValues.user_input_text - results.forEach(item => { - if (item.corrected && item.original) { - corrected = corrected.replace(item.original, item.corrected) - } - }) - setCorrectedText(corrected) - } - } catch (e) { - // 解析失败,继续等待 - } + setGeneratedResult(fullContent) } else if (parsed.event === 'error') { throw new Error(parsed.message || 'Dify API 返回错误') } @@ -140,24 +85,6 @@ const SpellCheck = () => { } finally { reader.releaseLock() } - - if (results.length === 0 && fullContent) { - try { - results = JSON.parse(fullContent) - setCorrectionResults(results) - if (formValues.auto_correct) { - let corrected = formValues.user_input_text - results.forEach(item => { - if (item.corrected && item.original) { - corrected = corrected.replace(item.original, item.corrected) - } - }) - setCorrectedText(corrected) - } - } catch (e) { - message.warning('检测完成,但结果格式异常') - } - } } catch (error) { console.error('检测错误:', error) message.error(error instanceof Error ? error.message : '检测失败,请稍后重试') @@ -168,173 +95,118 @@ const SpellCheck = () => { const handleReset = () => { form.resetFields() - setCorrectionResults([]) - setCorrectedText('') + setGeneratedResult('') message.info('已重置表单') + if (textareaRef.current) { + textareaRef.current.focus() + } } - const renderCorrectionResults = () => { - if (isGenerating) { - return ( -
- 正在检测中,请稍候... -
- ) + const handleCopy = async () => { + try { + await navigator.clipboard.writeText(generatedResult) + message.success('复制成功') + } catch (error) { + message.error('复制失败') } - - if (correctionResults.length === 0) { - return ( -
- 错别字检测结果将在这里显示 -
- ) - } - - return ( -
- {correctionResults.map((item, index) => ( -
-
- 位置 {item.position}: -
-
- 原词:{item.original} - - 建议:{item.corrected} -
- {item.suggestions && item.suggestions.length > 0 && ( -
- 其他建议:{item.suggestions.join('、')} -
- )} -
- ))} -
- ) } return ( -
+
{/* 页面标题区 */} -
-

- 错别字检测与纠正系统 -

+
+ + 错别字检测与修正 +
- - {/* 参数输入区 */} - -
- - - - - - - - - - -
- 格式要求:合法的JSON数组,元素为字符串 -
-
-
-
- - {/* 操作按钮区 */} -
- - -
+ + + + - {/* 内容展示区 */} - + + +
+ + {/* 内容展示区 */} + +
0 || isGenerating ? 'block' : 'none' + minHeight: '150px', + maxHeight: '60vh', + overflowY: 'auto', + padding: '20px', + backgroundColor: '#fafafa', + border: '1px solid #e8e8e8', + borderRadius: '6px', + fontSize: '14px', + lineHeight: '1.8', + whiteSpace: 'pre-wrap' }} > -
- {renderCorrectionResults()} -
- - - {correctedText && ( - -
- {correctedText} + {isGenerating && !generatedResult ? ( +
+ 正在检测中,请稍候...
- - )} - + ) : generatedResult ? ( +
+ {generatedResult} +
+ +
+
+ ) : ( +
+ 错别字检测结果将在这里显示 +
+ )} +
+
) }