You've already forked bilibili-subtitle
提问优化
This commit is contained in:
@@ -40,11 +40,11 @@ import useTranslate from '../hooks/useTranslate'
|
|||||||
import {getSummarize} from '../util/biz_util'
|
import {getSummarize} from '../util/biz_util'
|
||||||
import {openUrl} from '@kky002/kky-util'
|
import {openUrl} from '@kky002/kky-util'
|
||||||
import Markdown from '../components/Markdown'
|
import Markdown from '../components/Markdown'
|
||||||
import {random} from 'lodash-es'
|
|
||||||
import useKeyService from '../hooks/useKeyService'
|
import useKeyService from '../hooks/useKeyService'
|
||||||
|
|
||||||
const Body = () => {
|
const Body = () => {
|
||||||
const dispatch = useAppDispatch()
|
const dispatch = useAppDispatch()
|
||||||
|
const inputting = useAppSelector(state => state.env.inputting)
|
||||||
const noVideo = useAppSelector(state => state.env.noVideo)
|
const noVideo = useAppSelector(state => state.env.noVideo)
|
||||||
const autoTranslate = useAppSelector(state => state.env.autoTranslate)
|
const autoTranslate = useAppSelector(state => state.env.autoTranslate)
|
||||||
const autoScroll = useAppSelector(state => state.env.autoScroll)
|
const autoScroll = useAppSelector(state => state.env.autoScroll)
|
||||||
@@ -56,7 +56,7 @@ const Body = () => {
|
|||||||
const translateEnable = useAppSelector(state => state.env.envData.translateEnable)
|
const translateEnable = useAppSelector(state => state.env.envData.translateEnable)
|
||||||
const summarizeEnable = useAppSelector(state => state.env.envData.summarizeEnable)
|
const summarizeEnable = useAppSelector(state => state.env.envData.summarizeEnable)
|
||||||
const {addSummarizeTask, addAskTask} = useTranslate()
|
const {addSummarizeTask, addAskTask} = useTranslate()
|
||||||
const infos = useAppSelector(state => state.env.infos)
|
// const infos = useAppSelector(state => state.env.infos)
|
||||||
const askFold = useAppSelector(state => state.env.askFold)
|
const askFold = useAppSelector(state => state.env.askFold)
|
||||||
const askQuestion = useAppSelector(state => state.env.askQuestion)
|
const askQuestion = useAppSelector(state => state.env.askQuestion)
|
||||||
const askContent = useAppSelector(state => state.env.askContent)
|
const askContent = useAppSelector(state => state.env.askContent)
|
||||||
@@ -71,7 +71,7 @@ const Body = () => {
|
|||||||
const title = useAppSelector(state => state.env.title)
|
const title = useAppSelector(state => state.env.title)
|
||||||
const fontSize = useAppSelector(state => state.env.envData.fontSize)
|
const fontSize = useAppSelector(state => state.env.envData.fontSize)
|
||||||
const searchText = useAppSelector(state => state.env.searchText)
|
const searchText = useAppSelector(state => state.env.searchText)
|
||||||
const recommendIdx = useMemo(() => random(0, 3), [])
|
// const recommendIdx = useMemo(() => random(0, 3), [])
|
||||||
const showSearchInput = useMemo(() => {
|
const showSearchInput = useMemo(() => {
|
||||||
return (segments != null && segments.length > 0) && (envData.searchEnabled ? envData.searchEnabled : (envData.askEnabled ?? ASK_ENABLED_DEFAULT))
|
return (segments != null && segments.length > 0) && (envData.searchEnabled ? envData.searchEnabled : (envData.askEnabled ?? ASK_ENABLED_DEFAULT))
|
||||||
}, [envData.askEnabled, envData.searchEnabled, segments])
|
}, [envData.askEnabled, envData.searchEnabled, segments])
|
||||||
@@ -79,7 +79,7 @@ const Body = () => {
|
|||||||
let placeholder = ''
|
let placeholder = ''
|
||||||
if (envData.searchEnabled) {
|
if (envData.searchEnabled) {
|
||||||
if (envData.askEnabled??ASK_ENABLED_DEFAULT) {
|
if (envData.askEnabled??ASK_ENABLED_DEFAULT) {
|
||||||
placeholder = '搜索或提问字幕内容'
|
placeholder = '搜索或提问字幕内容(按Enter提问)'
|
||||||
} else {
|
} else {
|
||||||
placeholder = '搜索字幕内容'
|
placeholder = '搜索字幕内容'
|
||||||
}
|
}
|
||||||
@@ -254,7 +254,16 @@ const Body = () => {
|
|||||||
|
|
||||||
{/* search */}
|
{/* search */}
|
||||||
{showSearchInput && <div className='px-2 py-1 flex flex-col relative'>
|
{showSearchInput && <div className='px-2 py-1 flex flex-col relative'>
|
||||||
<input type='text' className='input input-xs bg-base-200' placeholder={searchPlaceholder} value={searchText} onChange={onSearchTextChange}/>
|
<input type='text' className='input input-xs bg-base-200' placeholder={searchPlaceholder} value={searchText} onChange={onSearchTextChange} onKeyDown={e => {
|
||||||
|
// enter
|
||||||
|
if (e.key === 'Enter') {
|
||||||
|
if (!inputting) {
|
||||||
|
e.preventDefault()
|
||||||
|
e.stopPropagation()
|
||||||
|
onAsk()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}}/>
|
||||||
{searchText && <button className='absolute top-1 right-2 btn btn-ghost btn-xs btn-circle text-base-content/75' onClick={onClearSearchText}><AiOutlineCloseCircle/></button>}
|
{searchText && <button className='absolute top-1 right-2 btn btn-ghost btn-xs btn-circle text-base-content/75' onClick={onClearSearchText}><AiOutlineCloseCircle/></button>}
|
||||||
</div>}
|
</div>}
|
||||||
|
|
||||||
|
@@ -1,14 +1,39 @@
|
|||||||
import {useEffect} from 'react'
|
import {useEffect} from 'react'
|
||||||
import {useMemoizedFn} from 'ahooks/es'
|
import {useMemoizedFn} from 'ahooks/es'
|
||||||
import {useAppSelector} from './redux'
|
import {useAppDispatch, useAppSelector} from './redux'
|
||||||
import useSubtitle from './useSubtitle'
|
import useSubtitle from './useSubtitle'
|
||||||
|
import {setInputting} from '../redux/envReducer'
|
||||||
|
|
||||||
const useKeyService = () => {
|
const useKeyService = () => {
|
||||||
|
const dispatch = useAppDispatch()
|
||||||
|
const inputting = useAppSelector(state => state.env.inputting)
|
||||||
const curIdx = useAppSelector(state => state.env.curIdx)
|
const curIdx = useAppSelector(state => state.env.curIdx)
|
||||||
const data = useAppSelector(state => state.env.data)
|
const data = useAppSelector(state => state.env.data)
|
||||||
const {move} = useSubtitle()
|
const {move} = useSubtitle()
|
||||||
|
|
||||||
|
// 输入中
|
||||||
|
useEffect(() => {
|
||||||
|
const onInputtingStart = (e: CompositionEvent) => {
|
||||||
|
dispatch(setInputting(true))
|
||||||
|
}
|
||||||
|
const onInputtingEnd = (e: CompositionEvent) => {
|
||||||
|
dispatch(setInputting(false))
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('compositionstart', onInputtingStart)
|
||||||
|
document.addEventListener('compositionend', onInputtingEnd)
|
||||||
|
return () => {
|
||||||
|
document.removeEventListener('compositionstart', onInputtingStart)
|
||||||
|
document.removeEventListener('compositionend', onInputtingEnd)
|
||||||
|
}
|
||||||
|
}, [dispatch])
|
||||||
|
|
||||||
const onKeyDown = useMemoizedFn((e: KeyboardEvent) => {
|
const onKeyDown = useMemoizedFn((e: KeyboardEvent) => {
|
||||||
|
// 当前在输入中(如中文输入法)
|
||||||
|
if (inputting) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 有按其他控制键时,不触发
|
// 有按其他控制键时,不触发
|
||||||
if (e.ctrlKey || e.metaKey || e.shiftKey) {
|
if (e.ctrlKey || e.metaKey || e.shiftKey) {
|
||||||
return
|
return
|
||||||
|
@@ -45,6 +45,11 @@ interface EnvState {
|
|||||||
askError?: string
|
askError?: string
|
||||||
askContent?: string
|
askContent?: string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否输入中(中文)
|
||||||
|
*/
|
||||||
|
inputting: boolean
|
||||||
|
|
||||||
searchText: string
|
searchText: string
|
||||||
searchResult: Set<number>
|
searchResult: Set<number>
|
||||||
}
|
}
|
||||||
@@ -71,6 +76,8 @@ const initialState: EnvState = {
|
|||||||
data: import.meta.env.VITE_ENV === 'web-dev' ? getDevData() : undefined,
|
data: import.meta.env.VITE_ENV === 'web-dev' ? getDevData() : undefined,
|
||||||
transResults: {},
|
transResults: {},
|
||||||
|
|
||||||
|
inputting: false,
|
||||||
|
|
||||||
searchText: '',
|
searchText: '',
|
||||||
searchResult: new Set(),
|
searchResult: new Set(),
|
||||||
}
|
}
|
||||||
@@ -288,6 +295,9 @@ export const slice = createSlice({
|
|||||||
setFold: (state, action: PayloadAction<boolean>) => {
|
setFold: (state, action: PayloadAction<boolean>) => {
|
||||||
state.fold = action.payload
|
state.fold = action.payload
|
||||||
},
|
},
|
||||||
|
setInputting: (state, action: PayloadAction<boolean>) => {
|
||||||
|
state.inputting = action.payload
|
||||||
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -335,6 +345,7 @@ export const {
|
|||||||
setFold,
|
setFold,
|
||||||
setSearchText,
|
setSearchText,
|
||||||
setSearchResult,
|
setSearchResult,
|
||||||
|
setInputting,
|
||||||
} = slice.actions
|
} = slice.actions
|
||||||
|
|
||||||
export default slice.reducer
|
export default slice.reducer
|
||||||
|
Reference in New Issue
Block a user