You've already forked bilibili-subtitle
fix消息通信
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import { setAuthor, setCtime, setCurFetched, setCurInfo, setData, setInfos, setTitle, setUrl } from '@/redux/envReducer'
|
||||
import { useAppDispatch } from './redux'
|
||||
import { useAppDispatch, useAppSelector } from './redux'
|
||||
import { AllAPPMessages, AllExtensionMessages, AllInjectMessages } from '@/message-typings'
|
||||
import { DEFAULT_USE_PORT } from '@/consts/const'
|
||||
import { useMessaging, useMessagingService } from '@kky002/kky-message'
|
||||
import { useMemoizedFn } from 'ahooks'
|
||||
|
||||
const useMessageService = () => {
|
||||
const dispatch = useAppDispatch()
|
||||
|
||||
//methods
|
||||
const envData = useAppSelector((state) => state.env.envData)
|
||||
|
||||
// methods
|
||||
const methodsFunc: () => {
|
||||
[K in AllAPPMessages['method']]: (params: Extract<AllAPPMessages, { method: K }>['params'], context: MethodContext) => Promise<any>
|
||||
} = useMemoizedFn(() => ({
|
||||
@@ -28,8 +28,8 @@ const useMessageService = () => {
|
||||
},
|
||||
}))
|
||||
|
||||
useMessagingService(DEFAULT_USE_PORT, methodsFunc)
|
||||
useMessagingService(!!envData.sidePanel, methodsFunc)
|
||||
}
|
||||
|
||||
export default useMessageService
|
||||
export const useMessage = useMessaging<AllExtensionMessages, AllInjectMessages>
|
||||
export const useMessage = useMessaging<AllExtensionMessages, AllInjectMessages>
|
||||
|
@@ -9,18 +9,18 @@ const useSubtitle = () => {
|
||||
const reviewAction = useAppSelector(state => state.env.reviewAction)
|
||||
const reviewActions = useAppSelector(state => state.env.tempData.reviewActions)
|
||||
const {sendInject} = useMessage(!!envData.sidePanel)
|
||||
|
||||
|
||||
const move = useCallback((time: number, togglePause: boolean) => {
|
||||
sendInject(null, 'MOVE', {time, togglePause})
|
||||
|
||||
//review action
|
||||
// review action
|
||||
if (reviewed === undefined && !reviewAction) {
|
||||
dispatch(setReviewAction(true))
|
||||
dispatch(setTempData({
|
||||
reviewActions: (reviewActions ?? 0) + 1
|
||||
}))
|
||||
}
|
||||
}, [dispatch, reviewAction, reviewActions, reviewed])
|
||||
}, [dispatch, reviewAction, reviewActions, reviewed, sendInject])
|
||||
|
||||
const scrollIntoView = useCallback((ref: React.RefObject<HTMLDivElement>) => {
|
||||
ref.current?.scrollIntoView({behavior: 'smooth', block: 'center'})
|
||||
|
@@ -45,7 +45,7 @@ const useSubtitleService = () => {
|
||||
const reviewActions = useAppSelector(state => state.env.tempData.reviewActions)
|
||||
const {sendInject} = useMessage(!!envData.sidePanel)
|
||||
|
||||
//如果reviewActions达到15次,则设置reviewed为false
|
||||
// 如果reviewActions达到15次,则设置reviewed为false
|
||||
useEffect(() => {
|
||||
if (reviewed === undefined && reviewActions && reviewActions >= 15) {
|
||||
dispatch(setTempData({
|
||||
@@ -74,7 +74,7 @@ const useSubtitleService = () => {
|
||||
dispatch(setCurInfo(infos[0]))
|
||||
dispatch(setCurFetched(false))
|
||||
}
|
||||
}, [curInfo, dispatch, envData.autoExpand, envReady, fold, infos])
|
||||
}, [curInfo, dispatch, envData.autoExpand, envReady, fold, infos, envData.sidePanel])
|
||||
// 获取
|
||||
useEffect(() => {
|
||||
if (curInfo && !curFetched) {
|
||||
@@ -86,15 +86,17 @@ const useSubtitleService = () => {
|
||||
dispatch(setCurFetched(true))
|
||||
dispatch(setData(data))
|
||||
|
||||
console.log('subtitle', data)
|
||||
console.debug('subtitle', data)
|
||||
})
|
||||
}
|
||||
}, [curFetched, curInfo])
|
||||
}, [curFetched, curInfo, dispatch, sendInject])
|
||||
|
||||
useAsyncEffect(async () => {
|
||||
// 初始获取列表
|
||||
sendInject(null, 'REFRESH_VIDEO_INFO', {force: true})
|
||||
}, [])
|
||||
if (envReady) {
|
||||
sendInject(null, 'REFRESH_VIDEO_INFO', {force: true})
|
||||
}
|
||||
}, [envReady, sendInject])
|
||||
|
||||
useAsyncEffect(async () => {
|
||||
// 更新设置信息
|
||||
@@ -103,11 +105,11 @@ const useSubtitleService = () => {
|
||||
if (envData.sidePanel) {
|
||||
// get screen height
|
||||
dispatch(setTotalHeight(window.innerHeight))
|
||||
}else {
|
||||
} else {
|
||||
dispatch(setTotalHeight(Math.min(Math.max(info.totalHeight, TOTAL_HEIGHT_MIN), TOTAL_HEIGHT_MAX)))
|
||||
}
|
||||
})
|
||||
}, [envData.sidePanel, infos])
|
||||
}, [envData.sidePanel, infos, sendInject])
|
||||
|
||||
// 更新当前位置
|
||||
useEffect(() => {
|
||||
@@ -212,7 +214,7 @@ const useSubtitleService = () => {
|
||||
} else {
|
||||
sendInject(null, 'HIDE_TRANS', {})
|
||||
}
|
||||
}, [autoTranslate, curIdx, hideOnDisableAutoTranslate, transResults])
|
||||
}, [autoTranslate, curIdx, hideOnDisableAutoTranslate, sendInject, transResults])
|
||||
}
|
||||
|
||||
export default useSubtitleService
|
||||
|
@@ -139,10 +139,10 @@ const useTranslate = () => {
|
||||
dispatch(addTaskId(task.id))
|
||||
}
|
||||
}
|
||||
}, [data?.body, envData, language.name, title, dispatch])
|
||||
}, [data?.body, envData, language.name, title, dispatch, sendExtension])
|
||||
|
||||
const addSummarizeTask = useCallback(async (type: SummaryType, segment: Segment) => {
|
||||
//review action
|
||||
// review action
|
||||
if (reviewed === undefined && !reviewAction) {
|
||||
dispatch(setReviewAction(true))
|
||||
dispatch(setTempData({
|
||||
@@ -208,7 +208,7 @@ const useTranslate = () => {
|
||||
const task = await sendExtension(null, 'ADD_TASK', {taskDef})
|
||||
dispatch(addTaskId(task.id))
|
||||
}
|
||||
}, [dispatch, envData, summarizeLanguage.name, title])
|
||||
}, [dispatch, envData, reviewAction, reviewActions, reviewed, sendExtension, summarizeLanguage.name, title])
|
||||
|
||||
const addAskTask = useCallback(async (id: string, segment: Segment, question: string) => {
|
||||
if (segment.text.length >= SUMMARIZE_THRESHOLD) {
|
||||
@@ -265,7 +265,7 @@ const useTranslate = () => {
|
||||
const task = await sendExtension(null, 'ADD_TASK', {taskDef})
|
||||
dispatch(addTaskId(task.id))
|
||||
}
|
||||
}, [dispatch, envData, summarizeLanguage.name, title])
|
||||
}, [dispatch, envData, sendExtension, summarizeLanguage.name, title])
|
||||
|
||||
const handleTranslate = useMemoizedFn((task: Task, content: string) => {
|
||||
let map: {[key: string]: string} = {}
|
||||
@@ -355,7 +355,7 @@ const useTranslate = () => {
|
||||
} else {
|
||||
dispatch(delTaskId(taskId))
|
||||
}
|
||||
}, [dispatch, envData.aiType, handleAsk, handleSummarize, handleTranslate])
|
||||
}, [dispatch, envData.aiType, handleAsk, handleSummarize, handleTranslate, sendExtension])
|
||||
|
||||
return {getFetch, getTask, addTask, addSummarizeTask, addAskTask}
|
||||
}
|
||||
|
Reference in New Issue
Block a user