Skip to content

Commit

Permalink
添加按钮加载时动画
Browse files Browse the repository at this point in the history
  • Loading branch information
LeafYeeXYZ committed May 11, 2024
1 parent 2d37b4b commit 29e8821
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/components/Prompt.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import '../styles/Prompt.css'
import { useRef } from 'react'
import { useRef, useState } from 'react'
import { SERVER } from '../config.json'
import getHash from '../libs/getHash.ts'
import getLoadingImage from '../libs/getLoadingImage.tsx'
import { blobToBase64 } from '../libs/base64_blob.ts'
import { DialogAction } from '../libs/useDialog.tsx'
import { Image } from './App.tsx'
import { flushSync } from 'react-dom'
import { LoadingOutlined } from '@ant-design/icons'

// 获取模型列表
type Models = {
Expand Down Expand Up @@ -58,6 +59,10 @@ function Prompt({ children, currentImages, setCurrentImages, dialogAction, langM
// 引用元素
const submitRef = useRef<HTMLButtonElement>(null)
const modelRef = useRef<HTMLSelectElement>(null)
// 按钮文本
const initialContent = <span>生成</span>
const loadingContent = <span>生成中 <LoadingOutlined /></span>
const [buttonContent, setButtonContent] = useState<React.JSX.Element>(initialContent)

// 翻译函数
async function translate(text: string): Promise<string> {
Expand Down Expand Up @@ -93,7 +98,7 @@ function Prompt({ children, currentImages, setCurrentImages, dialogAction, langM
// 禁用按钮
submitRef.current!.disabled = true
// 设置按钮文本
submitRef.current!.textContent = '生成中...'
flushSync(() => setButtonContent(loadingContent))
// 获取用户输入的提示词
let text = promptRef.current!.value
// 如果用户没有输入提示词,不发送请求
Expand Down Expand Up @@ -159,7 +164,7 @@ function Prompt({ children, currentImages, setCurrentImages, dialogAction, langM
} finally {
// 启用按钮
submitRef.current!.disabled = false
submitRef.current!.textContent = '生成'
setButtonContent(initialContent)
// 移除加载图片
setLoadingImage(null)
// 重置状态
Expand Down Expand Up @@ -194,7 +199,7 @@ function Prompt({ children, currentImages, setCurrentImages, dialogAction, langM
className='prompt-submit'
ref={submitRef}
onClick={event => handleSubmit(event, geneMode, langMode, currentImages)}
>生成</button>
>{buttonContent}</button>

</form>
)
Expand Down

0 comments on commit 29e8821

Please sign in to comment.